first commit
This commit is contained in:
commit
d416fe4cce
69 changed files with 8269 additions and 0 deletions
29
backend/app/db/mongo.py
Normal file
29
backend/app/db/mongo.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
client: AsyncIOMotorClient | None = None
|
||||
|
||||
|
||||
def get_database() -> AsyncIOMotorDatabase:
|
||||
if client is None:
|
||||
raise RuntimeError("MongoDB client has not been initialized")
|
||||
return client[settings.mongo_db_name]
|
||||
|
||||
|
||||
def get_client() -> AsyncIOMotorClient:
|
||||
if client is None:
|
||||
raise RuntimeError("MongoDB client has not been initialized")
|
||||
return client
|
||||
|
||||
|
||||
async def connect_to_mongo() -> None:
|
||||
global client
|
||||
client = AsyncIOMotorClient(settings.mongo_uri)
|
||||
|
||||
|
||||
async def close_mongo_connection() -> None:
|
||||
global client
|
||||
if client is not None:
|
||||
client.close()
|
||||
client = None
|
||||
Loading…
Add table
Add a link
Reference in a new issue