-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_interface.py
31 lines (28 loc) · 990 Bytes
/
DB_interface.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
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
class DB_Firestore:
def __init__(self):
#initialize connection
cred = credentials.Certificate("barbuddy-e6e6d-367c584fae56.json")
firebase_admin.initialize_app(cred)
self.db = firestore.client()
#@params
#Collection:either patrons or drinks
#Document:
# -patrons: it is name
# -drinks: name of drink or id
#Data: in json format
#
#Function will update or add entry
#
def add_data(self, collection, document, data):
ref = self.db.collection(collection).document(document)
ref.set(data, merge=True)
def read_data(self, collection,document):
doc_ref = self.db.collection(collection).document(document)
try:
doc = doc_ref.get()
return doc.to_dict()
except google.cloud.exceptions.NotFound:
return None