28 lines
667 B
Python
28 lines
667 B
Python
from beanie import init_beanie
|
|
|
|
from app.core.config import settings
|
|
from app.models import ALL_DOCUMENTS
|
|
|
|
_client = None
|
|
|
|
|
|
def _make_client():
|
|
"""Real Motor client, or in-memory mongomock when MOCK_DB=1."""
|
|
if settings.mock_db:
|
|
from mongomock_motor import AsyncMongoMockClient
|
|
return AsyncMongoMockClient()
|
|
from motor.motor_asyncio import AsyncIOMotorClient
|
|
return AsyncIOMotorClient(settings.mongodb_url)
|
|
|
|
|
|
async def init_db():
|
|
global _client
|
|
_client = _make_client()
|
|
db = _client[settings.mongodb_db]
|
|
await init_beanie(database=db, document_models=ALL_DOCUMENTS)
|
|
return db
|
|
|
|
|
|
def get_client():
|
|
return _client
|