Skip to content

Cloud Firestore

Rui Dias edited this page Oct 16, 2021 · 1 revision

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.

  1. Import the Document or the Collection Hooks
    import { useDocument, useCollection } from "@netsoft/firebase";

Fetch a colletion of documents

  1. To fetch a collection of document, just use the useCollection hook:
    const [data, isBusy, err] = useCollection("<collectionPath>");
  2. 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,
    });
  3. If you want to filter your data, the fill the filter property in the options parameter:
    const [data, isBusy, err] = useCollection("<collectionPath>", {
        filter: ["Age", ">=", 18],
    });
    the filter row must contain always three items: "FieldName"; "Operator"; "Value"
  4. 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"],
        ],
    });

Fetch a single document

  1. To fetch a specific document, just use the useDocument hook:

    const [data, isBusy, err] = useDocument("<collectionPath>", "<documentId>");
  2. 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,
        }
    );

Actions

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);

Timestamps

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(),
    (...)
}
Clone this wiki locally