File size: 752 Bytes
272199f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from motor.motor_asyncio import AsyncIOMotorClient
import os
from dotenv import load_dotenv

# Load environment from .env if present
load_dotenv()

MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017/")
# Optional database name (useful for MongoDB SRV URIs that don't include a DB)
MONGODB_DB = os.getenv("MONGODB_DB", "ucprojects")

client: AsyncIOMotorClient | None = None


def get_client() -> AsyncIOMotorClient:
    global client
    if client is None:
        client = AsyncIOMotorClient(MONGODB_URI)
    return client


def get_db():
    # Return a named database. If the URI already contains a default DB, this
    # will still work because get_database accepts the explicit name.
    return get_client().get_database(MONGODB_DB)