-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters