CRUD: Create Read Update Delete Using Python and Admin Firebase.
- At first will create and connect to a firebase database in puthon.
- Once connected, create a demo database to store values for status and task as keys.
You'll need a Firebase project set up. If you don't have one, create one on the Firebase console.
- Here, we have created CRUD named web app.
- Select Web App.
- Give a Name and ID.
- Select your hosting Server and Location.
Once app is created,
-
Go to Firestore Database
-
Select Start in Production mode
-
Remember, once database is created will change the allow read, write to true.
-
Click on Create.
Your database will look like this, Once the database is created,
- Go to Rules inside the Fire database.
- Change the allow read, write to true.
- Click on Publish.
We will get the python config
code for our app.
- Select python.
- Copy the Python config code to your app.py on your local system. Then, click on Service Account it will redirect to a page.
Now will get the key, after getting redirected to our Google Cloud, Service Account.
-
Again select service accounts, your account will be displayed.
-
Click on Add Key.
-
Select JSON as key type.
-
Click on Create.
A file will be automatically downloaded.
Remember to download all the dependencies mentioned in requirements.txt
on your python venv.
pip install -r requirements.txt
To learn about python venv, check out the tutorial on python venv.
Now added the dependencies to your app.py, here we have named our app.py as "firebase-authdemo.py".
import firebase_admin
from firebase_admin import firestore
from firebase_admin import credentials
Here, we have renamed the JSON file as serviceAccount.
Inside the Python config code,
- Pass the key "serviceAccount.json" and initialize
firebase_admin
cred = credentials.Certificate("./serviceAccount.json")
firebase_admin.initialize_app(cred)
Here, we have defined a dictionary named data
that contains information about a task.
- task: The value is "washing," which presumably describes the task itself.
- status: The value is "ToDO," likely indicating the current state of the task.
Establishes a connection to your Firebase Firestore database.
firestore
is a module imported from thefirebase_admin
library, which provides functionality to interact with Firestore from your Python application.- The
client()
method creates a client object that handles communication with the Firestore database.
db = firestore.client()
data={
'task':'washing',
'status':'ToDO'
}
doc_ref = db.collection('taskCollection').document()
doc_ref.set(data)
print('Document ID : ',doc_ref.id)
db.collection('taskCollection')
: This selects the existing collection named "taskCollection" within your Firestore database..document()
: This creates a new, empty document within the chosen collection. A unique document ID is automatically generated by Firestore..set(data)
: This method sets the data for the newly created document. The provided data dictionary is used to populate the fields within the document.- The
doc_ref.id
attribute holds the unique identifier assigned to the document by Firestore.
Now, we run the firebase-authdemo.py file.
- We see Document ID being displayed in the terminal.
- They are different objects being stored to the database CRUD.
In Firebase Firestore,taskCollection
is a collection within your Firestore database. A collection is a container that groups related documents together. In this case, each document likely represents a task.
- Firestore: A NoSQL database service offered by Firebase that allows you to store and retrieve data in a flexible and scalable manner.
- Collection: A group of documents with a similar theme or purpose. Think of it like a folder in a filing cabinet that holds related documents.
- Document: An individual unit of data within a collection, similar to a file in a folder. It contains fields (like key-value pairs) that represent the data for a specific task.
- Field: A property or attribute within a document that holds a specific piece of information. In this case, you have two fields:
status
: A string field that likely indicates the current state of the task (e.g., "ToDO," "In Progress," "Done").task
: A string field that describes the actual task itself (e.g., "washing," "groceries," "exercise").