Commit 1f2e4944 authored by Hoanganhvu123's avatar Hoanganhvu123

perf: add production-ready MongoDB indexes and clean env files

parent d12635ed
VITE_CLERK_PUBLISHABLE_KEY=pk_test_Y29tbXVuYWwtc3VuYmVhbS0wLmNsZXJrLmFjY291bnRzLmRldiQ
CLERK_SECRET_KEY=sk_test_ek7ozVR80Qi9UdvhGaTmlXovS16GDuBDlDrpH1rkyQ
MONGODB_URI = mongodb+srv://20010841:vuhoanganh1704@cluster0.h6qro.mongodb.net/?appName=Cluster0
\ No newline at end of file
...@@ -155,35 +155,58 @@ def parse_object_id(id_str: str) -> ObjectId | None: ...@@ -155,35 +155,58 @@ def parse_object_id(id_str: str) -> ObjectId | None:
async def create_indexes(): async def create_indexes():
"""Create database indexes for performance optimization.""" """Create database indexes for performance optimization (Production-ready)."""
try: try:
db = mongodb_client.db db = mongodb_client.db
# Indexes for memos collection # ====================== MEMOS COLLECTION ======================
await db[COLLECTION_MEMOS].create_index([("user_id", 1), ("created_at", -1)]) # Primary query patterns - creator_id is used extensively
await db[COLLECTION_MEMOS].create_index([("user_id", 1), ("visibility", 1)]) await db[COLLECTION_MEMOS].create_index([("creator_id", 1), ("created_at", -1)])
await db[COLLECTION_MEMOS].create_index([("creator_id", 1), ("visibility", 1)])
await db[COLLECTION_MEMOS].create_index([("creator_id", 1), ("pinned", 1)])
await db[COLLECTION_MEMOS].create_index([("creator_id", 1), ("row_status", 1)])
# Lookup by uid (used for memo detail pages)
await db[COLLECTION_MEMOS].create_index([("uid", 1)], unique=True, sparse=True)
# Comments lookup (parent reference)
await db[COLLECTION_MEMOS].create_index([("parent", 1), ("visibility", 1)])
# Tag filtering and date range queries
await db[COLLECTION_MEMOS].create_index([("tags", 1)]) await db[COLLECTION_MEMOS].create_index([("tags", 1)])
await db[COLLECTION_MEMOS].create_index([("created_at", -1)]) await db[COLLECTION_MEMOS].create_index([("created_at", -1)])
# Indexes for memo relations # ====================== MEMO RELATIONS ======================
await db[COLLECTION_MEMO_RELATIONS].create_index([("memo_id", 1)]) await db[COLLECTION_MEMO_RELATIONS].create_index([("memo_id", 1)])
await db[COLLECTION_MEMO_RELATIONS].create_index([("related_memo_id", 1)]) await db[COLLECTION_MEMO_RELATIONS].create_index([("related_memo_id", 1)])
# Indexes for reactions # ====================== REACTIONS ======================
await db[COLLECTION_REACTIONS].create_index([("memo_id", 1)]) await db[COLLECTION_REACTIONS].create_index([("content_id", 1)])
await db[COLLECTION_REACTIONS].create_index([("user_id", 1)]) await db[COLLECTION_REACTIONS].create_index([("creator_id", 1)])
# Indexes for memo embeddings # ====================== MEMO EMBEDDINGS ======================
await db[COLLECTION_MEMO_EMBEDDINGS].create_index([("memo_id", 1)]) await db[COLLECTION_MEMO_EMBEDDINGS].create_index([("memo_id", 1)], unique=True, sparse=True)
await db[COLLECTION_MEMO_EMBEDDINGS].create_index([("user_id", 1), ("date", -1)]) await db[COLLECTION_MEMO_EMBEDDINGS].create_index([("date_key", 1)])
# Indexes for inbox # ====================== INBOX ======================
await db[COLLECTION_INBOX].create_index([("user_id", 1), ("created_at", -1)]) await db[COLLECTION_INBOX].create_index([("receiver", 1), ("created_at", -1)])
await db[COLLECTION_INBOX].create_index([("receiver", 1), ("status", 1)])
# Indexes for user settings # ====================== USER SETTINGS ======================
await db[COLLECTION_USER_SETTINGS].create_index([("user_id", 1)], unique=True) await db[COLLECTION_USER_SETTINGS].create_index([("user_id", 1)], unique=True)
logger.info("✅ Database indexes created successfully") # ====================== ACTIVITIES ======================
await db["cuccu_activities"].create_index([("created_at", -1)])
await db["cuccu_activities"].create_index([("creator_id", 1)])
# ====================== NOTIFICATIONS ======================
await db["cuccu_notifications"].create_index([("recipient_id", 1), ("created_at", -1)])
await db["cuccu_notifications"].create_index([("recipient_id", 1), ("status", 1)])
# ====================== MEMO VERSIONS ======================
await db["cuccu_memo_versions"].create_index([("memo_id", 1), ("version_index", -1)])
logger.info("✅ Database indexes created successfully (Production-ready)")
except Exception as e: except Exception as e:
logger.warning(f"⚠️ Error creating indexes (may already exist): {e}") logger.warning(f"⚠️ Error creating indexes (may already exist): {e}")
......
# ====================== CLERK (PUBLIC KEY ONLY - NO SECRET) ======================
VITE_CLERK_PUBLISHABLE_KEY=pk_test_Y29tbXVuYWwtc3VuYmVhbS0wLmNsZXJrLmFjY291bnRzLmRldiQ VITE_CLERK_PUBLISHABLE_KEY=pk_test_Y29tbXVuYWwtc3VuYmVhbS0wLmNsZXJrLmFjY291bnRzLmRldiQ
CLERK_SECRET_KEY=sk_test_ek7ozVR80Qi9UdvhGaTmlXovS16GDuBDlDrpH1rkyQ
\ No newline at end of file # ====================== API URL ======================
VITE_API_URL=http://localhost:8080
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment