-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_api.py
More file actions
60 lines (49 loc) · 1.86 KB
/
database_api.py
File metadata and controls
60 lines (49 loc) · 1.86 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from pymongo import MongoClient
from dotenv import load_dotenv
import logging
import os
import certifi
load_dotenv()
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
"""
This file creates an Database object that handles all things related to the database.
This might include querying from or inserting information into the database.
Logic regarding the database should go into this file.
"""
class DatabaseAPI:
def __init__(self):
self.connect()
self.db_test = self.client.test
self.db = self.client.course_selection
def connect(self):
self.client = MongoClient(os.getenv("MONGO"), tlsCAFile=certifi.where())
self.db_admin = self.client.admin
logger.info(f"MongoDB server status: {self.db_admin.command('serverStatus')}")
def get_all_test(self):
# note that when you are returning, you want it to be jsonify-able,
# which means that all fields either have to be a str or a int/float
# it will cause an error if some values are of type ObjectId which
# are return by default that are created by mongo
# you can specify which field to return or not to return in the second dict
# you send to the db
logger.info("Querying for all people in test database.")
try:
ret = list(self.db_test.people.find({}, {"_id": 0}))
except Exception as e:
logger.error(
f"Failed to query for all people in test database with error {e}"
)
ret = None
return ret
def close(self):
self.client.close()
if __name__ == "__main__":
# a basic example of how to use, can remove later
db = DatabaseAPI()
logger.info(db.get_all_test())
db.close()