Skip to content

Commit

Permalink
use db_url to access mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsung-Ju Lii committed Aug 24, 2023
1 parent 9c98443 commit 525f418
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions fastapi_template/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -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" %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
app.state.db_client = client
await beanie.init_beanie(
database=client[settings.db_base],
Expand Down

0 comments on commit 525f418

Please sign in to comment.