diff --git a/fastapi_template/cli.py b/fastapi_template/cli.py index 08a015d..64d8128 100644 --- a/fastapi_template/cli.py +++ b/fastapi_template/cli.py @@ -186,10 +186,10 @@ def checker(ctx: BuilderContext) -> bool: ), additional_info=Database( name="mongodb", - image="mongo:4.2", + image="mongo:7.0", async_driver="beanie", - driver_short="pymongo", - driver="pymongo", + driver_short="mongodb", + driver="mongodb", port=27017 ), ) diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml index 67e02db..f349b14 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml +++ b/fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.yml @@ -52,13 +52,17 @@ services: {%- if cookiecutter.db_info.name == "sqlite" %} {{cookiecutter.project_name | upper }}_DB_FILE: /db_data/db.sqlite3 {%- else %} - {{cookiecutter.project_name | upper}}_DB_HOST: {{cookiecutter.project_name}}-db + {{cookiecutter.project_name | upper}}_DB_HOST: {{cookiecutter.project_name}}_db_1 {{cookiecutter.project_name | upper}}_DB_PORT: {{cookiecutter.db_info.port}} {{cookiecutter.project_name | upper}}_DB_USER: {{cookiecutter.project_name}} {{cookiecutter.project_name | upper}}_DB_PASS: {{cookiecutter.project_name}} + {%- if cookiecutter.db_info.name == "mongodb" %} + {{cookiecutter.project_name | upper}}_DB_BASE: admin + {%- else %} {{cookiecutter.project_name | upper}}_DB_BASE: {{cookiecutter.project_name}} {%- endif %} {%- endif %} + {%- endif %} {%- if cookiecutter.enable_rmq == 'True' %} {{cookiecutter.project_name | upper }}_RABBIT_HOST: {{cookiecutter.project_name}}-rmq {%- endif %} @@ -106,13 +110,18 @@ services: {%- if cookiecutter.db_info.name == "mongodb"%} db: image: {{cookiecutter.db_info.image}} - ports: - - "{{cookiecutter.db_info.port}}:{{cookiecutter.db_info.port}}" restart: always environment: MONGO_INITDB_ROOT_USERNAME: "{{cookiecutter.project_name}}" MONGO_INITDB_ROOT_PASSWORD: "{{cookiecutter.project_name}}" command: "mongod" + volumes: + - {{cookiecutter.project_name}}-db-data:/data/db + healthcheck: + test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet + interval: 10s + timeout: 5s + retries: 40 {%- endif %} {%- if cookiecutter.db_info.name == "mysql" %} diff --git a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py index 3068743..9ee9994 100644 --- a/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py +++ b/fastapi_template/template/{{cookiecutter.project_name}}/{{cookiecutter.project_name}}/web/lifetime.py @@ -125,10 +125,7 @@ def _setup_db(app: FastAPI) -> None: # pragma: no cover from motor.motor_asyncio import AsyncIOMotorClient from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel async def _setup_db(app: FastAPI) -> None: - client = AsyncIOMotorClient( - f"mongodb://{settings.db_user}:{settings.db_pass}" - + f"@{settings.db_host}:{settings.db_port}/{settings.db_base}" - ) + client = AsyncIOMotorClient(settings.db_url.human_repr()) app.state.db_client = client await beanie.init_beanie( database=client[settings.db_base],