Skip to content

Commit 101c75c

Browse files
committed
Adding db to docs
1 parent 0654ef6 commit 101c75c

File tree

11 files changed

+218
-7
lines changed

11 files changed

+218
-7
lines changed

docs/reference/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
Here's the reference or code API, the classes, functions, parameters, attributes, and all the Aio Fluid parts you can use in your applications.
44

55
If you want to **learn Aio Fluid** you are much better off reading the
6-
[Api Fluid Tutorial](/tutorial).
6+
[Api Fluid Tutorials](/tutorials).

docs/tutorials/db.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Database
2+
3+
The `fluid.db` module provides a simple asynchronous interface to interact with postgres databases. It is built on top of the [sqlalchemy](https://www.sqlalchemy.org/) and [asyncpg](https://github.com/MagicStack/asyncpg) libraries.

docs/tutorials/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Tutorials
2+
3+
The step-by-step guides, the how-to's, the recipes, and all the Aio Fluid parts you can use in your applications.

docs/scheduler.md renamed to docs/tutorials/scheduler.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Distributed Task Producer/Consumer
1+
# Task Queue
22

33
This module has a lightweight implementation of a distributed task producer (TaskScheduler) and consumer (TaskConsumer).
44
The middleware for distributing tasks can be configured via the Broker interface.

examples/db/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
from fluid.db import CrudDB, get_db
4+
5+
from .tables1 import meta
6+
from .tables2 import additional_meta
7+
8+
DATASTORE = os.getenv(
9+
"DATASTORE", "postgresql+asyncpg://postgres:postgres@localhost:5432/openapi"
10+
)
11+
12+
13+
def setup(app: Application) -> CrudDB:
14+
return setup_tables(get_db(app, DATASTORE))
15+
16+
17+
def setup_tables(db: CrudDB) -> CrudDB:
18+
additional_meta(meta(db.metadata))
19+
return db
20+
21+
22+
DB = setup_tables(CrudDB(DATASTORE))

examples/db/tables1.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import enum
2+
3+
import sqlalchemy as sa
4+
from sqlalchemy_utils import UUIDType
5+
6+
from openapi.data import fields
7+
from openapi.db.columns import UUIDColumn
8+
9+
original_init = UUIDType.__init__
10+
11+
12+
class TaskType(enum.Enum):
13+
todo = 0
14+
issue = 1
15+
16+
17+
def patch_init(self, binary=True, native=True, **kw):
18+
original_init(self, binary=binary, native=native)
19+
20+
21+
UUIDType.__init__ = patch_init
22+
23+
24+
def title_field(**kwargs):
25+
return fields.str_field(**kwargs)
26+
27+
28+
def meta(meta=None):
29+
"""Add task related tables"""
30+
if meta is None:
31+
meta = sa.MetaData()
32+
33+
sa.Table(
34+
"tasks",
35+
meta,
36+
UUIDColumn("id", make_default=True, doc="Unique ID"),
37+
sa.Column(
38+
"title",
39+
sa.String(64),
40+
nullable=False,
41+
info=dict(min_length=3, data_field=title_field),
42+
),
43+
sa.Column("done", sa.DateTime(timezone=True)),
44+
sa.Column("severity", sa.Integer),
45+
sa.Column("created_by", sa.String, default="", nullable=False),
46+
sa.Column("type", sa.Enum(TaskType)),
47+
sa.Column("unique_title", sa.String, unique=True),
48+
sa.Column("story_points", sa.Numeric),
49+
sa.Column("random", sa.String(64)),
50+
sa.Column(
51+
"subtitle",
52+
sa.String(64),
53+
nullable=False,
54+
default="",
55+
),
56+
)
57+
58+
sa.Table(
59+
"series",
60+
meta,
61+
sa.Column("date", sa.DateTime(timezone=True), nullable=False, index=True),
62+
sa.Column("group", sa.String(32), nullable=False, index=True, default=""),
63+
sa.Column("value", sa.Numeric(precision=20, scale=8)),
64+
sa.UniqueConstraint("date", "group"),
65+
)
66+
67+
return meta

examples/db/tables2.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import uuid
2+
from datetime import date
3+
4+
import sqlalchemy as sa
5+
from sqlalchemy_utils import UUIDType
6+
7+
from openapi.db.columns import UUIDColumn
8+
from openapi.tz import utcnow
9+
10+
11+
def additional_meta(meta=None):
12+
"""Add task related tables"""
13+
if meta is None:
14+
meta = sa.MetaData()
15+
16+
sa.Table(
17+
"randoms",
18+
meta,
19+
sa.Column(
20+
"id", UUIDType(), primary_key=True, nullable=False, default=uuid.uuid4
21+
),
22+
sa.Column("randomdate", sa.Date, nullable=False, default=date.today),
23+
sa.Column(
24+
"timestamp", sa.DateTime(timezone=True), nullable=False, default=utcnow
25+
),
26+
sa.Column("price", sa.Numeric(precision=100, scale=4), nullable=False),
27+
sa.Column("tenor", sa.String(3), nullable=False),
28+
sa.Column("tick", sa.Boolean),
29+
sa.Column("info", sa.JSON),
30+
sa.Column("jsonlist", sa.JSON, default=[]),
31+
sa.Column(
32+
"task_id", sa.ForeignKey("tasks.id", ondelete="CASCADE"), nullable=False
33+
),
34+
)
35+
36+
sa.Table(
37+
"multi_key_unique",
38+
meta,
39+
sa.Column("x", sa.Integer, nullable=False),
40+
sa.Column("y", sa.Integer, nullable=False),
41+
sa.UniqueConstraint("x", "y"),
42+
)
43+
44+
sa.Table(
45+
"multi_key",
46+
meta,
47+
sa.Column("x", sa.JSON),
48+
sa.Column("y", sa.JSON),
49+
)
50+
51+
return meta
52+
53+
54+
def extra(meta):
55+
sa.Table(
56+
"extras",
57+
meta,
58+
UUIDColumn("id", make_default=True, doc="Unique ID"),
59+
sa.Column("name", sa.String(64), nullable=False),
60+
)

fluid/tools_fastapi/db.py

Whitespace-only changes.

fluid/tools_fastapi/service.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313

1414
class FastapiAppWorkers(Workers):
15-
"""An aiohttp runner"""
1615

1716
@classmethod
1817
def setup(cls, app: FastAPI, **kwargs: Any) -> Self:
19-
"""Setup the app runner"""
2018
workers = cls(**kwargs)
2119
app.state.workers = workers
2220
app.add_event_handler("startup", workers.startup)

poetry.lock

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

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ aiohttp = {version = "^3.9.5", optional = true}
3030
alembic = {version = "^1.13.2", optional = true}
3131
sqlalchemy-utils = {version = "^0.41.2", optional = true}
3232
python-dateutil = {version = "^2.9.0.post0", optional = true}
33+
asyncpg = {version = "^0.29.0", optional = true}
3334

3435
[tool.poetry.group.dev.dependencies]
3536
pytest = "^8.1.1"
@@ -54,7 +55,7 @@ mkdocstrings = {version = "^0.25.1", extras = ["python"]}
5455

5556
[tool.poetry.extras]
5657
cli = ["click", "rich"]
57-
db = ["sqlalchemy", "sqlalchemy-utils", "alembic", "python-dateutil"]
58+
db = ["sqlalchemy", "sqlalchemy-utils", "alembic", "python-dateutil", "asyncpg"]
5859
http = ["aiohttp"]
5960
log = ["python-json-logger"]
6061
full = ["cli", "db", "http", "log"]

0 commit comments

Comments
 (0)