In this Python Firebase lesson we are going to learn about Python Firebase SDK Cloud Firestore, according to Firebase Documentation, Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity. Cloud Firestore also offers seamless integration with other Firebase and Google Cloud Platform products, including Cloud Functions.
Make sure that you have already created an account in Firebase Console, after that you need to enable Firebase Cloud Firestore in your project. after that for integration of Firebase SDK you need to install the SDK for the language of your choice. because we are using python, so we do the process for python programming language.
To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format.
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- Securely store the JSON file containing the key.
This is the complete code for this lesson.
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 |
import firebase_admin from firebase_admin import credentials from firebase_admin import firestore # initializations cred = credentials.Certificate('firebase-sdk.json') firebase_admin.initialize_app(cred) db = firestore.client() #adding first data doc_ref = db.collection('employee').document('empdoc') doc_ref.set({ 'name':'Parwiz', 'lname':'Forogh', 'age':24 }) #adding second data doc_ref = db.collection('employee').document('emptwodoc') doc_ref.set({ 'name':'John', 'lname':'Doe', 'email':'john@gmail.com', 'age':24 }) #Reading the data ''' emp_ref = db.collection('employee') docs = emp_ref.stream() for doc in docs: print('{} => {} '.format(doc.id, doc.to_dict())) ''' |
In here we have created our credentials and we have added our json key file, also we need to initialize our credentials.
1 2 3 |
cred = credentials.Certificate('firebase-sdk.json') firebase_admin.initialize_app(cred) |
Now we need to create our firestore in here.
1 |
db = firestore.client() |
Adding Data in Cloud Firststore
Cloud Firestore stores data in Documents, which are stored in Collections. Cloud Firestore creates collections and documents implicitly the first time you add data to the document. you do not need to explicitly create collections or documents. Create a new collection and a document using the following example code.
1 2 3 4 5 6 7 8 9 10 |
doc_ref = db.collection('employee').document('empdoc') doc_ref.set({ 'name':'Parwiz', 'lname':'Forogh', 'age':24 }) |
Now we are going to add another data to the document.
1 2 3 4 5 6 7 8 9 10 |
doc_ref = db.collection('employee').document('emptwodoc') doc_ref.set({ 'name':'John', 'lname':'Doe', 'email':'john@gmail.com', 'age':24 }) |
Reading Data From Cloud Firestore
To quickly verify that you’ve added data to Cloud Firestore, use the data viewer in the Firebase console. You can also use the “get” method to retrieve the entire collection.
1 2 3 4 5 |
emp_ref = db.collection('employee') docs = emp_ref.stream() for doc in docs: print('{} => {} '.format(doc.id, doc.to_dict())) |