What is @firebase/firestore?
The @firebase/firestore package is a part of the Firebase JavaScript SDK, providing a powerful, scalable database for mobile, web, and server development. It offers real-time synchronization, offline support, and efficient data querying capabilities. This package allows developers to interact with Firestore, a NoSQL cloud database to store and sync data for client- and server-side development.
What are @firebase/firestore's main functionalities?
Adding Data
This feature allows you to add new documents to your Firestore database. The code sample demonstrates adding a new user with name and age fields to a 'users' collection.
db.collection('users').add({
name: 'Ada Lovelace',
age: 36
});
Reading Data
This feature enables reading documents from your Firestore database. The code sample shows how to fetch a document by its ID from the 'users' collection and log its data.
db.collection('users').doc('userId').get().then((doc) => {
if (doc.exists) {
console.log('Document data:', doc.data());
} else {
console.log('No such document!');
}
});
Listening for Real-time Updates
This feature provides real-time updates on data changes. The code sample demonstrates setting up a listener on a user document to log updates in real-time.
db.collection('users').doc('userId')
.onSnapshot((doc) => {
console.log('Current data: ', doc.data());
});
Querying Data
This feature allows querying documents based on certain conditions. The code sample shows how to query all users older than 18 years and log their data.
db.collection('users').where('age', '>', 18)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.id, ' => ', doc.data());
});
});
Other packages similar to @firebase/firestore
mongoose
Mongoose is an ODM (Object Data Modeling) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. Compared to @firebase/firestore, Mongoose is specific to MongoDB and does not offer real-time data synchronization.
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. Unlike @firebase/firestore, Sequelize is focused on SQL databases and does not provide real-time data updates or offline support.
pouchdb
PouchDB is an open-source JavaScript database that allows you to store data locally while offline, then synchronize it with CouchDB and compatible servers when the application is back online, keeping the user's data in sync no matter where they next login. While it offers offline data storage and sync capabilities similar to Firestore, it is more suited for applications that require data to be stored locally on the user's device.
@firebase/firestore
This is the Cloud Firestore component for the Firebase JS SDK. It has a peer
dependency on the @firebase/app
package on NPM. This package
is not included by default in the firebase
wrapper package.
Installation
You can install this package by running the following in your project:
$ npm install @firebase/firestore
Usage
You can then use the firebase namespace exposed by this package as illustrated
below:
ES Modules
import firebase from '@firebase/app';
import '@firebase/firestore'
CommonJS Modules
const firebase = require('@firebase/app').default;
require('@firebase/firestore');
Documentation
For comprehensive documentation please see the Firebase Reference
Docs.
Contributing
See Contributing to the Firebase SDK for general
information about contributing to the firebase-js-sdk repo and
Contributing to the Cloud Firestore Component for
details specific to the Cloud Firestore code and tests.