feat(postgres/sqlite): update API to be fully async#148
Merged
vdice merged 1 commit intospinframework:mainfrom Apr 13, 2026
Merged
feat(postgres/sqlite): update API to be fully async#148vdice merged 1 commit intospinframework:mainfrom
vdice merged 1 commit intospinframework:mainfrom
Conversation
1572eaf to
5a0a3ae
Compare
1 task
itowlson
approved these changes
Apr 13, 2026
| rows = result.rows | ||
|
|
||
| with await Connection.open_default() as db: | ||
| await db.execute("INSERT INTO todos (description, due) VALUES (?, ?)", [Value_Text("Try out Spin SQLite"), Value_Text("Friday")]) |
Contributor
There was a problem hiding this comment.
In principle this should presumably check the returned error future? (I am not quite sure when/where SQLite would produce the error in this case!)
Collaborator
There was a problem hiding this comment.
FWIW, here's what I did to make the spin-sqlite example work on an empty database, but never got around to PR'ing:
from spin_sdk import http, sqlite
from spin_sdk.http import Request, Response
from spin_sdk.sqlite import Value_Integer, Value_Text
from spin_sdk.util import collect
class HttpHandler(http.Handler):
async def handle_request(self, request: Request) -> Response:
with await sqlite.open_default() as db:
_, stream, result = await db.execute(
"CREATE TABLE IF NOT EXISTS example (id INTEGER NOT NULL PRIMARY KEY, value TEXT NOT NULL)",
[]
)
await collect((stream, result))
insert = "INSERT INTO example (id, value) VALUES (?, ?) ON CONFLICT (id) DO UPDATE SET value=excluded.value"
_, stream, result = await db.execute(insert, [Value_Integer(1), Value_Text("foo")])
await collect((stream, result))
_, stream, result = await db.execute(insert, [Value_Integer(2), Value_Text("bar")])
await collect((stream, result))
columns, stream, result = await db.execute("SELECT * FROM example WHERE id > (?);", [Value_Integer(1)])
rows = await collect((stream, result))
assert columns == ["id", "value"]
assert len(rows) == 1
assert isinstance(rows[0].values[0], Value_Integer)
assert rows[0].values[0].value == 2
assert isinstance(rows[0].values[1], Value_Text)
assert rows[0].values[1].value == "bar"
return Response(
200,
{"content-type": "text/plain"},
bytes(str(rows), "utf-8")
)
Contributor
Author
There was a problem hiding this comment.
Thanks @dicej, I've updated the example.
| """ | ||
| Open a connection with a Postgres database. | ||
|
|
||
| The connection_string is the Postgres URL connection string. |
Contributor
There was a problem hiding this comment.
I believe we support both the URL form and the connection string form.
Suggested change
| The connection_string is the Postgres URL connection string. | |
| The connection_string is the Postgres URL or connection string. |
Signed-off-by: Vaughn Dice <vdice@akamai.com> Co-authored-by: Joel Dice <joel.dice@akamai.com>
5a0a3ae to
022f16b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Updates the postgres and sqlite modules to provide a completely async, opinionated API. This breaks the previous (canary) versions of the v4 SDKs but as they hadn't yet been released, this seems a good time to do it.
TODO:
Closes #146