-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.py
49 lines (45 loc) · 1.54 KB
/
firestore.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
from google.cloud import firestore
db = firestore.Client()
def get_firestore_field(collection: str,document: str,field):
doc_ref = db.collection(collection).document(document)
doc = doc_ref.get()
if doc.exists:
try:
value = doc.get(field)
return value
except:
return None
else:
return None
def update_firestore_field(collection: str,document: str,field: str,value):
doc_ref = db.collection(collection).document(document)
try:
try:
doc_ref.update({
field: value
})
except:
# If the document does not exist, create a new one
doc_ref.set({
field: value
})
#print(f'{collection}/{document}/{field}/{value} updated success')
except:
print(f'{collection}/{document}/{field}/{value} updated failed')
pass
def append_firestore_array_field(collection: str,document: str,field: str,value):
doc_ref = db.collection(collection).document(document)
try:
doc_ref.update({
field: firestore.ArrayUnion(value)
})
#print(f'{collection}/{document}/{field}/{value} updated success')
except:
print(f'{collection}/{document}/{field}/{value} updated failed')
pass
def delete_firestore_field(collection: str,document: str,field):
doc_ref = db.collection(collection).document(document)
doc_ref.update({
field: firestore.DELETE_FIELD
})
#print(f'{collection}/{document}/{field} deleted success')