Skip to content

Commit 5a0a3ae

Browse files
vdicedicej
andcommitted
feat(postgres/sqlite): update API to be fully async
Signed-off-by: Vaughn Dice <vdice@akamai.com> Co-authored-by: Joel Dice <joel.dice@akamai.com>
1 parent 715e06b commit 5a0a3ae

File tree

6 files changed

+384
-90
lines changed

6 files changed

+384
-90
lines changed

docs/v4/postgres.html

Lines changed: 115 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/v4/sqlite.html

Lines changed: 143 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/spin-postgres/app.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
from spin_sdk import http, postgres
1+
from spin_sdk import http, postgres, util
22
from spin_sdk.http import Request, Response
3-
from spin_sdk.postgres import RowSet, DbValue
4-
3+
from spin_sdk.postgres import Connection, RowSet, DbValue
54

65
def format_value(db_value: DbValue) -> str:
76
if hasattr(db_value, "value"):
87
return str(db_value.value)
98
return "NULL"
109

11-
1210
def format_rowset(rowset: RowSet) -> str:
1311
lines = []
1412
col_names = [col.name for col in rowset.columns]
@@ -19,11 +17,12 @@ def format_rowset(rowset: RowSet) -> str:
1917
lines.append(" | ".join(values))
2018
return "\n".join(lines)
2119

22-
2320
class HttpHandler(http.Handler):
2421
async def handle_request(self, request: Request) -> Response:
25-
with await postgres.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
26-
rowset = db.query("SELECT * FROM test", [])
22+
with await Connection.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
23+
columns, stream, future = await db.query("SELECT * FROM test", [])
24+
rows = await util.collect((stream, future))
25+
rowset = RowSet(columns, list(rows))
2726
result = format_rowset(rowset)
2827

2928
return Response(200, {"content-type": "text/plain"}, bytes(result, "utf-8"))

examples/spin-sqlite/app.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from spin_sdk import http, sqlite
1+
from spin_sdk import http, util
22
from spin_sdk.http import Request, Response
3-
from spin_sdk.sqlite import Value_Integer
3+
from spin_sdk.sqlite import Connection, Value_Text, Value_Integer
44

55
class HttpHandler(http.Handler):
66
async def handle_request(self, request: Request) -> Response:
7-
with await sqlite.open_default() as db:
8-
result = db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
9-
rows = result.rows
10-
7+
with await Connection.open_default() as db:
8+
await db.execute("INSERT INTO todos (description, due) VALUES (?, ?)", [Value_Text("Try out Spin SQLite"), Value_Text("Friday")])
9+
columns, stream, future = await db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
10+
rows = await util.collect((stream, future))
11+
1112
return Response(
1213
200,
1314
{"content-type": "text/plain"},

0 commit comments

Comments
 (0)