Skip to content

Commit

Permalink
dao for mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsung-Ju Lii committed Aug 24, 2023
1 parent d7c3207 commit 5da8c72
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""DAO classes."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import List, Optional

from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel


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_one(DummyModel(name=name))

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.find_all(skip=offset, limit=limit).to_list()

async def filter(
self,
name: Optional[str] = None
) -> List[DummyModel]:
"""
Get specific dummy model.
:param name: name of dummy instance.
:return: dummy models.
"""
if name is None:
return []
return await DummyModel.find(DummyModel.name == name).to_list()
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

{%- endif %}
from starlette import status
{%- if cookiecutter.orm != 'beanie' %}
from {{cookiecutter.project_name}}.db.dao.dummy_dao import DummyDAO

{%- endif %}
from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel


Expand Down Expand Up @@ -56,17 +53,12 @@ async def test_creation(
dao = DummyDAO(dbsession)
{%- elif cookiecutter.orm == "psycopg" %}
dao = DummyDAO(dbpool)
{%- elif cookiecutter.orm in ["tortoise", "ormar", "piccolo"] %}
{%- else %}
dao = DummyDAO()
{%- endif %}

{%- if cookiecutter.orm == "beanie" %}
instance = await DummyModel.find(DummyModel.name == test_name).first_or_none()
assert instance is not None and instance.name == test_name
{%- else %}
instances = await dao.filter(name=test_name)
assert instances[0].name == test_name
{%- endif %}


@pytest.mark.anyio
Expand All @@ -84,15 +76,11 @@ async def test_getting(
dao = DummyDAO(dbsession)
{%- elif cookiecutter.orm == "psycopg" %}
dao = DummyDAO(dbpool)
{%- elif cookiecutter.orm in ["tortoise", "ormar", "piccolo"] %}
{%- else %}
dao = DummyDAO()
{%- endif %}
test_name = uuid.uuid4().hex
{%- if cookiecutter.orm == "beanie" %}
await DummyModel.insert_one(DummyModel(name=test_name))
{%- else %}
await dao.create_dummy_model(name=test_name)
{%- endif %}

{%- if cookiecutter.api_type == 'rest' %}
url = fastapi_app.url_path_for('get_dummy_models')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

from fastapi import APIRouter
from fastapi.param_functions import Depends
{%- if cookiecutter.db_info.name != "mongodb" %}
from {{cookiecutter.project_name}}.db.dao.dummy_dao import DummyDAO
{%- endif %}
from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel
from {{cookiecutter.project_name}}.web.api.dummy.schema import (DummyModelDTO,
DummyModelInputDTO)
Expand All @@ -16,9 +14,7 @@
async def get_dummy_models(
limit: int = 10,
offset: int = 0,
{%- if cookiecutter.db_info.name != "mongodb" %}
dummy_dao: DummyDAO = Depends(),
{%- endif %}
) -> List[DummyModel]:
"""
Retrieve all dummy objects from the database.
Expand All @@ -30,19 +26,13 @@ async def get_dummy_models(
{%- endif %}
:return: list of dummy objects from database.
"""
{%- if cookiecutter.db_info.name != "mongodb" %}
return await dummy_dao.get_all_dummies(limit=limit, offset=offset)
{%- else %}
return await DummyModel.find_all(skip=offset, limit=limit).to_list()
{%- endif %}


@router.put("/")
async def create_dummy_model(
new_dummy_object: DummyModelInputDTO,
{%- if cookiecutter.db_info.name != "mongodb" %}
dummy_dao: DummyDAO = Depends(),
{%- endif %}
) -> None:
"""
Creates dummy model in the database.
Expand All @@ -52,8 +42,4 @@ async def create_dummy_model(
:param dummy_dao: DAO for dummy models.
{%- endif %}
"""
{%- if cookiecutter.db_info.name != "mongodb" %}
await dummy_dao.create_dummy_model(name=new_dummy_object.name)
{%- else %}
await DummyModel.insert_one(DummyModel(name=new_dummy_object.name))
{%- endif %}

0 comments on commit 5da8c72

Please sign in to comment.