Introduction to Cloud Firestore
Get a brief introduction to Cloud Firestore.
We'll cover the following
Cloud Firestore is a flexible, scalable, real-time NoSQL cloud database provided by Firebase for storing application data. Data is stored as
How it works
Cloud Firestore stores data as documents. A document is a unit of storage made of a set of key-value pairs. It supports various data types—strings, numbers, booleans, arrays—and subcollections. Documents in Cloud Firestore are stored in a collection. A collection is a container for documents that organizes data and helps build efficient queries. We can also create subcollections within documents. However, collections in Firestore can only contain documents.
Cloud Firestore allows shallow querying. Therefore, we can retrieve individual, specific documents or all documents in a collection or subcollection. We can dd sort, filter, and limit our queries or cursors to paginate our results. We can also add real-time listeners to update the application by retrieving data whenever changes occur in the database.
Cloud Firestore also persists data to the local cache, making it optimized for offline usage. Whenever a user goes offline, their application is still able to read, write, and listen to data. This data then synchronizes when the user comes online.
Implementation
Like any other Firebase service, we must first import the Cloud Firestore’s getter function from its subpackage and then initialize it. We do this by importing the getFirestore
function from the firebase/firestore
subpackage. This function then takes a reference to the Firebase App instance as a parameter:
Get hands-on with 1200+ tech skills courses.