Cloud Firestore (In Beta at time of writing) 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.
More info: https://firebase.google.com/docs/firestore/
use cases for cloud functions:
- Notify users when something happens
- realtime maintainance and cleanup
- execute extensive tasks which cannot be done on the client
- integrate with third party services
- increment/decrement counters upon some event
More info: https://firebase.google.com/docs/functions/use-cases
Google finally announced support for python 3.7 runtime although the docs aren't updated yet.
Consider the following structure
finalDb/
account_1/
projects/
closedTaskCount (integer)
createedTime (timestamp)
creator (string)
deleted (boolean)
description (string)
projectDueDate (timestamp)
projectTags (map)
startDate (timestamp)
taskCount (integer)
tasks/
attachments (map)
createdTime (timestamp)
creator (string)
deleted (boolean)
dueDate (timestamp)
lastUpdatedTime (timestamp)
project (string)
owner (string)
status (string)
subTasks (map)
tags (map)
taskName (string)
taskUsers (map)
messages/
tags/
users/
activities/
.....
account_2/
account_3/
.....
account_n/
- users can create tasks with 5 statuses : open, closed, pause, yet to start, suspended
- A task can exist independently without being included in the project
- closed task count is incremented when task changes its state from open to closed and decremented when its state changes from closed to open
- taskcount is incremented/decremented whenever a new task is added or removed respectively
To increment/decrement the counters i.e; taskCount and closedTaskCount in projects collection whenever a task is created and references a project
To achieve consistency across all clients using transactions. Whenever a user makes changes, it must be consistent across all users and since transactions fail to execute whenever a device is offline, cloud functions are used to keep sanity with the value of counters when the device is online.
More info on transactions: https://firebase.google.com/docs/firestore/manage-data/transactions
The Cloud Functions for Firebase SDK exports a functions.firestore object that allows you to create handlers tied to specific events. Cloud Firestore supports create, update, delete, and write events.
More info: https://firebase.google.com/docs/firestore/extend-with-functions
- Goto google cloud console > cloud functions
- Click on create function
- Fill in function details such as function name, RAM.
- Select appropriate Trigger (in this case: cloud firestore)
- Select event type (create, update or delete or write)
- Provide a Document path to listen to changes. In this case: finalDb/{orgId}/tasks/{taskId}
- select source code type (inline editor/zip)
- Select runtime as python 3.7
- Provide the entry point i.e the function to start execution.
Note:
- Add firebase-admin==2.11.0 in requirements.txt file
Refer my stack overflow answer for screenshots:
https://stackoverflow.com/questions/48772583/python-in-google-cloud-functions/51466795#51466795