diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json b/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json index b875a9bc84d6df0520f25a2eef5e9228a80ff1d9..ee4dfcaf8d03e99058b6df67bea87201671ffb55 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json +++ b/fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json @@ -1,138 +1,144 @@ { - "Redis": { - "enabled": "{{cookiecutter.enable_redis}}", - "resources": [ - "{{cookiecutter.project_name}}/web/api/redis", - "{{cookiecutter.project_name}}/services/redis", - "{{cookiecutter.project_name}}/tests/test_redis.py", - "deploy/kube/redis.yml" - ] - }, - "Kubernetes": { - "enabled": "{{cookiecutter.enable_kube}}", - "resources": [ - "deploy/kube" - ] - }, - "Database support": { - "enabled": "{{cookiecutter.db_info.name != 'none'}}", - "resources": [ - "aerich.ini", - "alembic.ini", - "{{cookiecutter.project_name}}/web/api/dummy", - "{{cookiecutter.project_name}}/db_sa", - "{{cookiecutter.project_name}}/tests/test_dummy.py", - "deploy/kube/db.yml" - ] - }, - "Postgres and MySQL support": { - "enabled": "{{cookiecutter.db_info.name != 'sqlite'}}", - "resources": [ - "deploy/kube/db.yml" - ] - }, - "Migrations": { - "enabled": "{{cookiecutter.enable_migrations}}", - "resources": [ - "aerich.ini", - "alembic.ini", - "{{cookiecutter.project_name}}/db_sa/migrations", - "{{cookiecutter.project_name}}/db_ormar/migrations", - "{{cookiecutter.project_name}}/db_tortoise/migrations" - ] - }, - "Alembic migrations": { - "enabled": "{{cookiecutter.orm in ['ormar', 'sqlalchemy']}}", - "resources": [ - "alembic.ini" - ] - }, - "Gitlab CI": { - "enabled": "{{cookiecutter.ci_type == 'gitlab_ci'}}", - "resources": [ - ".gitlab-ci.yml" - ] - }, - "Github CI": { - "enabled": "{{cookiecutter.ci_type == 'github'}}", - "resources": [ - ".github" - ] - }, - "Routers": { - "enabled": "{{cookiecutter.enable_routers}}", - "resources": [ - "{{cookiecutter.project_name}}/web/api/echo", - "{{cookiecutter.project_name}}/web/api/dummy", - "{{cookiecutter.project_name}}/web/api/redis", - "{{cookiecutter.project_name}}/tests/test_echo.py", - "{{cookiecutter.project_name}}/tests/test_dummy.py", - "{{cookiecutter.project_name}}/tests/test_redis.py" - ] - }, - "Dummy model": { - "enabled": "{{cookiecutter.add_dummy}}", - "resources": [ - "{{cookiecutter.project_name}}/web/api/dummy", - "{{cookiecutter.project_name}}/db_sa/dao", - "{{cookiecutter.project_name}}/db_sa/models/dummy_model.py", - "{{cookiecutter.project_name}}/db_ormar/dao", - "{{cookiecutter.project_name}}/db_ormar/models/dummy_model.py", - "{{cookiecutter.project_name}}/db_tortoise/dao", - "{{cookiecutter.project_name}}/db_tortoise/models/dummy_model.py", - "{{cookiecutter.project_name}}/tests/test_dummy.py", - "{{cookiecutter.project_name}}/db_sa/migrations/versions/2021-08-16-16-55_2b7380507a71.py", - "{{cookiecutter.project_name}}/db_ormar/migrations/versions/2021-08-16-16-55_2b7380507a71.py", - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_pg.sql", - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql", - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql" - ] - }, - "Self-hosted swagger": { - "enabled": "{{cookiecutter.self_hosted_swagger}}", - "resources": [ - "{{cookiecutter.project_name}}/web/static", - "{{cookiecutter.project_name}}/web/api/docs" - ] - }, - "SQLAlchemy ORM": { - "enabled": "{{cookiecutter.orm == 'sqlalchemy'}}", - "resources": [ - "{{cookiecutter.project_name}}/db_sa" - ] - }, - "Tortoise ORM": { - "enabled": "{{cookiecutter.orm == 'tortoise'}}", - "resources": [ - "aerich.ini", - "{{cookiecutter.project_name}}/db_tortoise" - ] - }, - "Ormar ORM": { - "enabled": "{{cookiecutter.orm == 'ormar'}}", - "resources": [ - "{{cookiecutter.project_name}}/db_ormar" - ] - }, - "Postgresql DB": { - "enabled": "{{cookiecutter.db_info.name == 'postgresql'}}", - "resources": [ - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_pg.sql", - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_pg.sql" - ] - }, - "MySQL DB": { - "enabled": "{{cookiecutter.db_info.name == 'mysql'}}", - "resources": [ - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_mysql.sql", - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql" - ] - }, - "SQLite DB": { - "enabled": "{{cookiecutter.db_info.name == 'sqlite'}}", - "resources": [ - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_sqlite.sql", - "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql" - ] - } + "Redis": { + "enabled": "{{cookiecutter.enable_redis}}", + "resources": [ + "{{cookiecutter.project_name}}/web/api/redis", + "{{cookiecutter.project_name}}/services/redis", + "{{cookiecutter.project_name}}/tests/test_redis.py", + "deploy/kube/redis.yml" + ] + }, + "Kubernetes": { + "enabled": "{{cookiecutter.enable_kube}}", + "resources": [ + "deploy/kube" + ] + }, + "Database support": { + "enabled": "{{cookiecutter.db_info.name != 'none'}}", + "resources": [ + "aerich.ini", + "alembic.ini", + "{{cookiecutter.project_name}}/web/api/dummy", + "{{cookiecutter.project_name}}/db_sa", + "{{cookiecutter.project_name}}/tests/test_dummy.py", + "deploy/kube/db.yml" + ] + }, + "Postgres and MySQL support": { + "enabled": "{{cookiecutter.db_info.name != 'sqlite'}}", + "resources": [ + "deploy/kube/db.yml" + ] + }, + "Migrations": { + "enabled": "{{cookiecutter.enable_migrations}}", + "resources": [ + "aerich.ini", + "alembic.ini", + "{{cookiecutter.project_name}}/db_sa/migrations", + "{{cookiecutter.project_name}}/db_ormar/migrations", + "{{cookiecutter.project_name}}/db_tortoise/migrations" + ] + }, + "Alembic migrations": { + "enabled": "{{cookiecutter.orm in ['ormar', 'sqlalchemy']}}", + "resources": [ + "alembic.ini" + ] + }, + "Gitlab CI": { + "enabled": "{{cookiecutter.ci_type == 'gitlab_ci'}}", + "resources": [ + ".gitlab-ci.yml" + ] + }, + "Github CI": { + "enabled": "{{cookiecutter.ci_type == 'github'}}", + "resources": [ + ".github" + ] + }, + "Routers": { + "enabled": "{{cookiecutter.enable_routers}}", + "resources": [ + "{{cookiecutter.project_name}}/web/api/echo", + "{{cookiecutter.project_name}}/web/api/dummy", + "{{cookiecutter.project_name}}/web/api/redis", + "{{cookiecutter.project_name}}/tests/test_echo.py", + "{{cookiecutter.project_name}}/tests/test_dummy.py", + "{{cookiecutter.project_name}}/tests/test_redis.py" + ] + }, + "Dummy model": { + "enabled": "{{cookiecutter.add_dummy}}", + "resources": [ + "{{cookiecutter.project_name}}/web/api/dummy", + "{{cookiecutter.project_name}}/db_sa/dao", + "{{cookiecutter.project_name}}/db_sa/models/dummy_model.py", + "{{cookiecutter.project_name}}/db_ormar/dao", + "{{cookiecutter.project_name}}/db_ormar/models/dummy_model.py", + "{{cookiecutter.project_name}}/db_tortoise/dao", + "{{cookiecutter.project_name}}/db_tortoise/models/dummy_model.py", + "{{cookiecutter.project_name}}/tests/test_dummy.py", + "{{cookiecutter.project_name}}/db_sa/migrations/versions/2021-08-16-16-55_2b7380507a71.py", + "{{cookiecutter.project_name}}/db_ormar/migrations/versions/2021-08-16-16-55_2b7380507a71.py", + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_pg.sql", + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql", + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql" + ] + }, + "Self-hosted swagger": { + "enabled": "{{cookiecutter.self_hosted_swagger}}", + "resources": [ + "{{cookiecutter.project_name}}/web/static", + "{{cookiecutter.project_name}}/web/api/docs" + ] + }, + "SQLAlchemy ORM": { + "enabled": "{{cookiecutter.orm == 'sqlalchemy'}}", + "resources": [ + "{{cookiecutter.project_name}}/db_sa" + ] + }, + "Tortoise ORM": { + "enabled": "{{cookiecutter.orm == 'tortoise'}}", + "resources": [ + "aerich.ini", + "{{cookiecutter.project_name}}/db_tortoise" + ] + }, + "Ormar ORM": { + "enabled": "{{cookiecutter.orm == 'ormar'}}", + "resources": [ + "{{cookiecutter.project_name}}/db_ormar" + ] + }, + "Piccolo ORM": { + "enabled": "{{cookiecutter.orm == 'piccolo'}}", + "resources": [ + "{{cookiecutter.project_name}}/db_piccolo" + ] + }, + "Postgresql DB": { + "enabled": "{{cookiecutter.db_info.name == 'postgresql'}}", + "resources": [ + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_pg.sql", + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_pg.sql" + ] + }, + "MySQL DB": { + "enabled": "{{cookiecutter.db_info.name == 'mysql'}}", + "resources": [ + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_mysql.sql", + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql" + ] + }, + "SQLite DB": { + "enabled": "{{cookiecutter.db_info.name == 'sqlite'}}", + "resources": [ + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_sqlite.sql", + "{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql" + ] + } } diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml b/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml index c9edcd8c98fd1e4893e2d6140c8bcfa8ad91eec8..c1614f043913ef422ddda15101c0ba54011e5679 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/pyproject.toml @@ -51,6 +51,9 @@ ormar = "^0.10.20" {%- if cookiecutter.enable_migrations == "True" %} alembic = "^1.6.5" {%- endif %} +{%- if cookiecutter.orm == "piccolo" %} +piccolo = "^0.66.0" +{%- endif %} {%- if cookiecutter.db_info.name == "postgresql" %} asyncpg = "^0.24.0" psycopg2-binary = "^2.9.1" diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/dao/__init__.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/dao/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/dao/dummy_dao.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/dao/dummy_dao.py new file mode 100644 index 0000000000000000000000000000000000000000..917701a6cb1307263f05ac5fa8e08777e4ac4f6f --- /dev/null +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/dao/dummy_dao.py @@ -0,0 +1,38 @@ +from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel +from typing import List, Optional + + +class DummyDAO: + """Class for accessing dummy table.""" + + async def create_dummy_model(self, name: str) -> None: + """ + Add single dummy to session. + + :param name: name of a dummy. + """ + await DummyModel.insert(DummyModel(name=name)).run() + + async def get_all_dummies(self, limit: int, offset: int) -> List[DummyModel]: + """ + Get all dummy models with limit/offset pagination. + + :param limit: limit of dummies. + :param offset: offset of dummies. + :return: stream of dummies. + """ + return ( + await DummyModel.select().offset(offset).limit(limit).run() + ) + + async def filter(self, name: Optional[str] = None) -> List[DummyModel]: + """ + Get specific dummy model. + + :param name: name of dummy instance. + :return: dummy models. + """ + query = DummyModel.select() + if name: + query = query.where(DummyModel.name == name) + return await query.run() diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/models/__init__.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/models/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/models/dummy_model.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/models/dummy_model.py new file mode 100644 index 0000000000000000000000000000000000000000..40ecdafbece6bebcd639909e9ff5df25962618f0 --- /dev/null +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/db_piccolo/models/dummy_model.py @@ -0,0 +1,6 @@ +from piccolo.columns import Varchar +from piccolo.table import Table + + +class DummyModel(Table): + name = Varchar(length=200)