Skip to content

Commit

Permalink
update example to use repository rather than accessor
Browse files Browse the repository at this point in the history
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/.
  • Loading branch information
glyph committed Mar 28, 2024
1 parent d5a888b commit 3dcba8c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions docs/codeexamples/userpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -100,7 +100,12 @@ async def makePostByUser(
...


posts = accessor(PostDB)
@dataclass
class BlogRepo:
posts: PostDB


blog = repository(BlogRepo)


async def main() -> None:
Expand All @@ -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
Expand Down

0 comments on commit 3dcba8c

Please sign in to comment.