-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
33 lines (24 loc) · 814 Bytes
/
db.py
File metadata and controls
33 lines (24 loc) · 814 Bytes
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
import pymongo
class ItemHandler:
"""
Mixin for dealing with items from a specific foo.bar database.
"""
bar_collection: pymongo.collection.Collection
def get_db_item(self):
return self.bar_collection.find_one()
class MongoDB(ItemHandler):
def __init__(self):
self.client = None
def init_from_app(self, app):
"""
Set up a mongoclient using app.config
"""
mongo_uri = app.config["MONGO_URI"]
self.client = self._get_mongo_client(mongo_uri)
self.bar_collection = self.client["foo"]["bar"]
def _get_mongo_client(self, mongo_uri):
"""
This function will be mocked in testing and modified
to return a mongomock.MongoClient instead.
"""
return pymongo.MongoClient(mongo_uri)