Skip to content

Commit

Permalink
Adding db to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Aug 4, 2024
1 parent 0654ef6 commit 101c75c
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
Here's the reference or code API, the classes, functions, parameters, attributes, and all the Aio Fluid parts you can use in your applications.

If you want to **learn Aio Fluid** you are much better off reading the
[Api Fluid Tutorial](/tutorial).
[Api Fluid Tutorials](/tutorials).
3 changes: 3 additions & 0 deletions docs/tutorials/db.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Database

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.
3 changes: 3 additions & 0 deletions docs/tutorials/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tutorials

The step-by-step guides, the how-to's, the recipes, and all the Aio Fluid parts you can use in your applications.
2 changes: 1 addition & 1 deletion docs/scheduler.md → docs/tutorials/scheduler.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Distributed Task Producer/Consumer
# Task Queue

This module has a lightweight implementation of a distributed task producer (TaskScheduler) and consumer (TaskConsumer).
The middleware for distributing tasks can be configured via the Broker interface.
Expand Down
22 changes: 22 additions & 0 deletions examples/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from fluid.db import CrudDB, get_db

from .tables1 import meta
from .tables2 import additional_meta

DATASTORE = os.getenv(
"DATASTORE", "postgresql+asyncpg://postgres:postgres@localhost:5432/openapi"
)


def setup(app: Application) -> CrudDB:
return setup_tables(get_db(app, DATASTORE))


def setup_tables(db: CrudDB) -> CrudDB:
additional_meta(meta(db.metadata))
return db


DB = setup_tables(CrudDB(DATASTORE))
67 changes: 67 additions & 0 deletions examples/db/tables1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import enum

import sqlalchemy as sa
from sqlalchemy_utils import UUIDType

from openapi.data import fields
from openapi.db.columns import UUIDColumn

original_init = UUIDType.__init__


class TaskType(enum.Enum):
todo = 0
issue = 1


def patch_init(self, binary=True, native=True, **kw):
original_init(self, binary=binary, native=native)


UUIDType.__init__ = patch_init


def title_field(**kwargs):
return fields.str_field(**kwargs)


def meta(meta=None):
"""Add task related tables"""
if meta is None:
meta = sa.MetaData()

sa.Table(
"tasks",
meta,
UUIDColumn("id", make_default=True, doc="Unique ID"),
sa.Column(
"title",
sa.String(64),
nullable=False,
info=dict(min_length=3, data_field=title_field),
),
sa.Column("done", sa.DateTime(timezone=True)),
sa.Column("severity", sa.Integer),
sa.Column("created_by", sa.String, default="", nullable=False),
sa.Column("type", sa.Enum(TaskType)),
sa.Column("unique_title", sa.String, unique=True),
sa.Column("story_points", sa.Numeric),
sa.Column("random", sa.String(64)),
sa.Column(
"subtitle",
sa.String(64),
nullable=False,
default="",
),
)

sa.Table(
"series",
meta,
sa.Column("date", sa.DateTime(timezone=True), nullable=False, index=True),
sa.Column("group", sa.String(32), nullable=False, index=True, default=""),
sa.Column("value", sa.Numeric(precision=20, scale=8)),
sa.UniqueConstraint("date", "group"),
)

return meta
60 changes: 60 additions & 0 deletions examples/db/tables2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import uuid
from datetime import date

import sqlalchemy as sa
from sqlalchemy_utils import UUIDType

from openapi.db.columns import UUIDColumn
from openapi.tz import utcnow


def additional_meta(meta=None):
"""Add task related tables"""
if meta is None:
meta = sa.MetaData()

sa.Table(
"randoms",
meta,
sa.Column(
"id", UUIDType(), primary_key=True, nullable=False, default=uuid.uuid4
),
sa.Column("randomdate", sa.Date, nullable=False, default=date.today),
sa.Column(
"timestamp", sa.DateTime(timezone=True), nullable=False, default=utcnow
),
sa.Column("price", sa.Numeric(precision=100, scale=4), nullable=False),
sa.Column("tenor", sa.String(3), nullable=False),
sa.Column("tick", sa.Boolean),
sa.Column("info", sa.JSON),
sa.Column("jsonlist", sa.JSON, default=[]),
sa.Column(
"task_id", sa.ForeignKey("tasks.id", ondelete="CASCADE"), nullable=False
),
)

sa.Table(
"multi_key_unique",
meta,
sa.Column("x", sa.Integer, nullable=False),
sa.Column("y", sa.Integer, nullable=False),
sa.UniqueConstraint("x", "y"),
)

sa.Table(
"multi_key",
meta,
sa.Column("x", sa.JSON),
sa.Column("y", sa.JSON),
)

return meta


def extra(meta):
sa.Table(
"extras",
meta,
UUIDColumn("id", make_default=True, doc="Unique ID"),
sa.Column("name", sa.String(64), nullable=False),
)
Empty file removed fluid/tools_fastapi/db.py
Empty file.
2 changes: 0 additions & 2 deletions fluid/tools_fastapi/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@


class FastapiAppWorkers(Workers):
"""An aiohttp runner"""

@classmethod
def setup(cls, app: FastAPI, **kwargs: Any) -> Self:
"""Setup the app runner"""
workers = cls(**kwargs)
app.state.workers = workers
app.add_event_handler("startup", workers.startup)
Expand Down
61 changes: 59 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ aiohttp = {version = "^3.9.5", optional = true}
alembic = {version = "^1.13.2", optional = true}
sqlalchemy-utils = {version = "^0.41.2", optional = true}
python-dateutil = {version = "^2.9.0.post0", optional = true}
asyncpg = {version = "^0.29.0", optional = true}

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

[tool.poetry.extras]
cli = ["click", "rich"]
db = ["sqlalchemy", "sqlalchemy-utils", "alembic", "python-dateutil"]
db = ["sqlalchemy", "sqlalchemy-utils", "alembic", "python-dateutil", "asyncpg"]
http = ["aiohttp"]
log = ["python-json-logger"]
full = ["cli", "db", "http", "log"]
Expand Down

0 comments on commit 101c75c

Please sign in to comment.