edusync / app /core /database.py
sakshusat's picture
feat: implement initial face recognition service with API endpoints for registration, attendance, and anti-spoofing capabilities.
e51f7f4
from motor.motor_asyncio import AsyncIOMotorClient
from app.core.config import settings
class Database:
client: AsyncIOMotorClient = None
def connect_db(self):
# Adding connection pooling for faster concurrent requests
self.client = AsyncIOMotorClient(
settings.MONGODB_URL,
minPoolSize=5,
maxPoolSize=50,
serverSelectionTimeoutMS=5000
)
print("✅ Connected to MongoDB with PoolSize=5-50")
# Ensure proper indexing for student lookups (In background)
import asyncio
asyncio.create_task(self.ensure_indexes())
async def ensure_indexes(self):
try:
collection = self.get_collection()
await collection.create_index([("studentId", 1), ("ownerAdmin", 1)], unique=True)
print("✅ MongoDB Indexes ensured for performance")
except Exception as e:
print(f"⚠️ Index creation warning: {e}")
def close_db(self):
if self.client:
self.client.close()
print("🔌 Disconnected from MongoDB")
def get_collection(self):
if self.client is None:
self.connect_db() # Self-healing on access
return self.client[settings.DB_NAME][settings.COLLECTION_NAME]
db = Database()