Skip to content

Commit be570f1

Browse files
author
Jack Li
committed
add mongodb storage apis
1 parent 9abbe76 commit be570f1

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

storage/base.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pymongo import collection
2+
3+
def __insert_one__(coll: collection.Collection, doc: dict):
4+
return coll.insert_one(doc)
5+
6+
def __insert_many__(coll: collection.Collection, docs: list):
7+
return coll.insert_many(docs)
8+
9+
def __delete_one__(coll: collection.Collection, filter: dict):
10+
return coll.delete_one(filter)
11+
12+
def __delete_many__(coll: collection.Collection, filter: dict):
13+
return coll.delete_many(filter)
14+
15+
def __find_one_and_replace__(coll: collection.Collection, filter: dict, new_doc: dict):
16+
return coll.find_one_and_replace(filter, new_doc)
17+
18+
def __find_one_and_delete__(coll: collection.Collection, filter: dict):
19+
return coll.find_one_and_delete(filter)
20+
21+
def __find_one__(coll: collection.Collection, filter: dict, projection):
22+
return coll.find_one(filter=filter, projection=projection)
23+
24+
def __find_many__(coll: collection.Collection, filter: dict, projection, **kwargs):
25+
return coll.find(filter=filter, projection=projection, **kwargs)

storage/storage.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from ..utils import get_db_handle
2+
from .base import *
3+
4+
# insert one
5+
def insert_one(collection_name, doc: dict, db_name="tickets"):
6+
db = get_db_handle(db_name)
7+
coll = db[collection_name]
8+
return __insert_one__(coll, doc)
9+
10+
# insert many
11+
def insert_many(collection_name, docs: list, db_name="tickets"):
12+
db = get_db_handle(db_name)
13+
coll = db[collection_name]
14+
return __insert_many__(coll, docs)
15+
16+
# delete one
17+
def delete_one(collection_name, filter: dict, db_name="tickets"):
18+
db = get_db_handle(db_name)
19+
coll = db[collection_name]
20+
return __delete_one__(coll, filter)
21+
22+
# delete many
23+
def delete_many(collection_name, filter: dict, db_name="tickets"):
24+
db = get_db_handle(db_name)
25+
coll = db[collection_name]
26+
return __delete_many__(coll, filter)
27+
28+
# find one and replace
29+
def find_one_and_replace(collection_name, filter: dict, new_doc: dict, db_name="tickets"):
30+
db = get_db_handle(db_name)
31+
coll = db[collection_name]
32+
return __find_one_and_replace__(coll, filter, new_doc)
33+
34+
def find_one_and_delete(collection_name, filter: dict, db_name="tickets"):
35+
db = get_db_handle(db_name)
36+
coll = db[collection_name]
37+
return __find_one_and_delete__(coll, filter)
38+
39+
# find one
40+
def find_one(collection_name, filter: dict, projection=None, db_name="tickets"):
41+
db = get_db_handle(db_name)
42+
coll = db[collection_name]
43+
return __find_one__(coll, filter, projection)
44+
45+
# find many
46+
def find_many(collection_name, filter: dict, projection=None, db_name="tickets", **kwargs):
47+
db = get_db_handle(db_name)
48+
coll = db[collection_name]
49+
return __find_many__(coll, filter, projection, **kwargs)

tmtracker/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ class MyAppConfig(AppConfig):
99

1010
def ready(self):
1111
print(f"server started at {datetime.now().strftime('%d/%m/%Y %H:%M:%S')}")
12-
get_db_handle('tickets')
1312
print("=== database connection is established ===")
1413
start()

0 commit comments

Comments
 (0)