-
Notifications
You must be signed in to change notification settings - Fork 0
Cloud Firestore
Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud. 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 products, including Cloud Functions.
- Import the Document or the Collection Hooks
import { useDocument, useCollection } from "@netsoft/firebase";
- To fetch a collection of document, just use the
useCollection
hook:const [data, isBusy, err] = useCollection("<collectionPath>");
- If you need your collection to be automaticaly updated when data changes in the backend, you can pass an optional options parameter with
snapshot: true
:const [data, isBusy, err] = useCollection("<collectionPath>", { snapshot: true, });
- If you want to filter your data, the fill the
filter
property in the options parameter:the filter row must contain always three items: "FieldName"; "Operator"; "Value"const [data, isBusy, err] = useCollection("<collectionPath>", { filter: ["Age", ">=", 18], });
- The
filter
property can also be an array of rows, if you need more than one criteria:const [data, isBusy, err] = useCollection("<collectionPath>", { filter: [ ["Age", ">=", 18], ["State", "==", "CA"], ], });
-
To fetch a specific document, just use the
useDocument
hook:const [data, isBusy, err] = useDocument("<collectionPath>", "<documentId>");
-
If you need your data to be automaticaly updated when data changes in the backend, you can pass an optional options parameter with
snapshot: true
:const [data, isBusy, err] = useDocument( "<collectionPath>", "<documentId>", { snapshot: true, } );
If you need to interact with your data (add; update; remove), just add an extra field to the hook:
const [data, isBusy, err, actions] = useCollection(path);
actions.add(newDoc); // let Cloud Firestore auto-generate an ID
actions.add(newDoc, id); // specify an ID for the document to create
actions.remove(id);
const [data, isBusy, err, actions] = useDocument(path, id);
actions.update(data);
You can set a field in your document to a server timestamp which tracks when the server receives the update. When updating multiple timestamp fields inside of a transaction, each field receives the same server timestamp value.
const { Timestamp, serverTimestamp } = useTimestamp();
const data = {
stringExample: "Hello world!",
dateExample: Timestamp.fromDate(new Date("December 10, 1815")),
timestamp: serverTimestamp(),
(...)
}