-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
111 lines (90 loc) · 3.37 KB
/
db.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# code snippets: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/41095eba777a94e2748a233fd5a2df826792086a/firestore/cloud-client/snippets.py#L273-L279
# firebase docs: https://firebase.google.com/docs/firestore/query-data/queries#python_3
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import datetime
# Use the application default credentials
cred = credentials.Certificate('Chatty/chatty-y-firebase-adminsdk-zb1ow-7b79045fa0.json')
default_app = firebase_admin.initialize_app(cred)
# initialize firestore
db = firestore.client()
# create a new document
def add(username, fname, lname, email, bio, streak=0, tags=[], status='happy', location=(),city='',friends=[]):
# with specified document id.
doc_ref = db.collection(u'users').document(f'{username}')
doc_ref.set({ # if you uncommented the line above, then chanhe this line to doc_ref.set({, default = db.collection(u'users').add({
'username':username,
'fname':fname,
'lname':lname,
'email':email,
'streak':streak,
'bio':bio,
'tags':tags,
'status': status,
'location': location,
'created': datetime.datetime.now(),
'city':city,
'friends': friends
})
# create a new user example
# add(username='john',
# fname="john",
# lname="doe",
# tags=["dance","painting","photo","coding","hiking"],
# email="[email protected]",
# bio="hello text me!",
# status="online")
# get all the data in a collection
def get_all(collection="users"):
collection_ref = db.collection(f'{collection}')
docs = collection_ref.stream()
arr = []
for doc in docs:
print(f'{doc.id} => {doc.to_dict()}')
arr.append(doc.to_dict())
return arr
# get a document by id
def get_by_doc_id(docid):
doc_ref = db.collection(u'users').document(docid)
doc = doc_ref.get()
print(f'{doc.id} => {doc.to_dict()}')
dic = doc.to_dict()
return dic
# get a document by username.
def get_by_username(username):
doc = db.collection(u'users').where(u'username', u'==',username).stream()
#print(f'{doc.id} => {doc.to_dict()}')
for d in doc:
print(f'{d.id} => {d.to_dict()}')
# update some fields. but not array fields. you can update multiple
def update(username,**kwargs):
user_ref = db.collection(u'users').document(username)
user_ref.update(kwargs)
user_ref.update({
u'timestamp': firestore.SERVER_TIMESTAMP
})
# find the people with similar interests
def tag_match(tag=[]):
collection_ref = db.collection(u'users')
query = collection_ref.where(u'tags', u'array_contains_any', tag)
result = query.stream()
for r in result:
print(f'{r.id} => {r.to_dict()}')
def tag_add(username,tags):
user_ref = db.collection(u'users').document(username)
# Atomically add a new tag to the 'tags' array field. you can add multiple.
user_ref.update({u'tags': firestore.ArrayUnion(tags)})
user_ref.update({
u'timestamp': firestore.SERVER_TIMESTAMP
})
def tag_remove(username,tags):
user_ref = db.collection(u'users').document(username)
# remove a tag from the 'tags' array field.
user_ref.update({u'tags': firestore.ArrayRemove(tags)})
user_ref.update({
u'timestamp': firestore.SERVER_TIMESTAMP
})
# other functions we need:
# order results
# find people close by