File size: 1,394 Bytes
60f8dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b8b5cc
60f8dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
import os
from pymongo import MongoClient
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

class Database:
    def __init__(self):
        self.mongodb_uri = os.getenv('MONGODB_URI', 'mongodb://localhost:27017/')
        self.db_name = os.getenv('DATABASE_NAME', 'contract_simplifier')
        self.client = None
        self.db = None
    
    def connect(self):
        """Establish connection to MongoDB"""
        try:
            self.client = MongoClient(self.mongodb_uri, serverSelectionTimeoutMS=5000)
            self.db = self.client[self.db_name]
            # Test the connection
            self.client.server_info()
            print(f"✓ Successfully connected to MongoDB database: {self.db_name}")
            return self.db
        except Exception as e:
            print(f"✗ Error connecting to MongoDB: {e}")
            return None
    
    def get_collection(self, collection_name):
        """Get a specific collection"""
        if self.db is not None:
            return self.db[collection_name]
        else:
            print("Database not connected. Call connect() first.")
            return None
    
    def close(self):
        """Close the database connection"""
        if self.client:
            self.client.close()
            print("✓ MongoDB connection closed")

# Create a global database instance
db_instance = Database()