-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
32 lines (27 loc) · 1.02 KB
/
db.py
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
""" Connecting to mongodb Atlas"""
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
from starlette.config import Config
class ConnectDB:
""" A Wrapper class for pymongo and Atlas """
def __init__(self) -> None:
self.conn = None
def get_collection(self):
""" Get the pymongo collection connection to db """
try:
config = Config('.env')
DBName = config('DBNAME')
collectionName = config('COLLECTION_NAME')
USER = config('USER')
PASS = config('PASS')
CLUSTER_URL = config('CLUSTER_URL')
self.conn = MongoClient(
f'mongodb+srv://{USER}:{PASS}@{CLUSTER_URL}/')
self.db = self.conn[DBName]
collection = self.db[collectionName]
return collection
except ConnectionFailure as err:
print("Could not connect to the MongoDB database:", err)
def close(self):
""" To close the connection to db. """
self.conn.close()