File size: 930 Bytes
be25939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
from pymongo import MongoClient
from flask import current_app, g
import logging

def get_db():
    """Get database connection from Flask g object or create new one."""
    if 'db' not in g:
        client = MongoClient(current_app.config['MONGODB_URI'])
        g.db = client[current_app.config['MONGODB_DATABASE']]
        g.client = client
    return g.db

def close_db(error):
    """Close database connection."""
    client = g.pop('client', None)
    if client is not None:
        client.close()

def init_db(app):
    """Initialize database with app."""
    app.teardown_appcontext(close_db)
    
    # Test connection
    with app.app_context():
        try:
            db = get_db()
            # Ping the database
            db.command('ping')
            logging.info("Successfully connected to MongoDB")
        except Exception as e:
            logging.error(f"Failed to connect to MongoDB: {e}")
            raise