From 3dcba8cf2601f0335bca9e506ff0fe28935ecc96 Mon Sep 17 00:00:00 2001 From: Glyph Date: Thu, 28 Mar 2024 16:15:14 -0700 Subject: [PATCH] update example to use repository rather than accessor This commit was sponsored by Matt Campbell, Sergio Bost, rockstar, and my other patrons. If you want to join them, you can support my work at https://glyph.im/patrons/. --- docs/codeexamples/userpost.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/codeexamples/userpost.py b/docs/codeexamples/userpost.py index e2963a2..33bd1d5 100644 --- a/docs/codeexamples/userpost.py +++ b/docs/codeexamples/userpost.py @@ -9,17 +9,17 @@ from twisted.internet.interfaces import IReactorCore from twisted.python.failure import Failure -from dbxs import accessor, many, one, query, statement +from dbxs import many, one, query, repository, statement from dbxs.adapters.dbapi_twisted import adaptSynchronousDriver from dbxs.async_dbapi import transaction schema = """ -CREATE TABLE user ( +CREATE TABLE IF NOT EXISTS user ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL ); -CREATE TABLE post ( +CREATE TABLE IF NOT EXISTS post ( id INTEGER PRIMARY KEY AUTOINCREMENT, created TIMESTAMP NOT NULL, content TEXT NOT NULL, @@ -100,7 +100,12 @@ async def makePostByUser( ... -posts = accessor(PostDB) +@dataclass +class BlogRepo: + posts: PostDB + + +blog = repository(BlogRepo) async def main() -> None: @@ -109,9 +114,8 @@ async def main() -> None: for expr in schema.split(";"): await cur.execute(expr) - async with transaction(asyncDriver) as c: - poster = posts(c) - b = await poster.createUser("bob") + async with blog(asyncDriver) as db: + b = await db.posts.createUser("bob") await b.post("a post") await b.post("another post") post: Post