Working with Firestore on the server
TIP
Fireman abstracts the Firestore SDK API providing you exactly the same API you use on the client, but on the server using the package @fireman/admin
.
@fireman/admin
provides also provide a few more helper functions to make your requests easier on the server. In most cases every function you use on the client should work the same on the server with the package (but not the other way around).
Querying, updating and deleting data
Works exactly as on the client. see here, however, the imports should be replaced by @fireman/admin
:
INFO
db
is your Firestore instance (see guide for how to initialize it). It needs to be passed as first argument in each function
import Fireman from "fireman/admin/firestore";
const fireman = new Fireman(db);
// get
fireman.get("users", {doc: "user1"}).then((userinfo) => {
console.log(userinfo);
});
// add
fireman.add("todo", todoData)
// update
fireman.update("todo", "todo1", { title: "new" })
// delete
fireman.delete("todo", "todo1")
Count documents in a collection
fireman.count("todos") // returns 3
// with filters (can also take limits)
fireman.count("todos", { where: [{ field: "done", op: "==", val: true }] }) // returns 1
List all documents [server only]
@fireman/admin
provides a helper function to list all documents in a collection. This function returns all documents with ids (yes, even the zombie ones).
import { listAllDocuments } from "@fireman/admin/firestore/utils";
listAllDocuments(db, collectionId).then(allDocs => {
console.log(allDocs)
})