-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_operations.py
61 lines (40 loc) · 1.42 KB
/
db_operations.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
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
61
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from flagships_data import flagships
from events_data import events
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
def addCompleteData(collection_name="safety_collection"):
'''
Function to add whole collection into firestore
'''
list_data = []
if collection_name == "Flagships":
list_data = flagships
elif collection_name == "Events":
list_data = events
else:
list_data = []
if len(list_data) == 0:
print("Please check the collection name")
else:
for data in list_data:
db.collection(collection_name).add(data)
print("Write Complete.")
def addNewTechnitude(collection_name="safety_collection"):
'''
Function to add new event into firebase collection, append a new dictionary in the data file.
'''
db.collection(collection_name).add(events[-1])
print("Write Complete.")
def readCompleteData(collection_name="safety_collection"):
'''
Function to read whole collection from firestore
'''
result = db.collection(collection_name).stream()
for doc in result:
print(f'{doc.id} => {doc.to_dict()}', end="\n\n")
print(result)
print("Read Complete. If you did not get what you wanted, please check the collection name.")