Firestore Cache a Solution for Cloud Function based fetching and caching of Firestore data.
Install with npm:
$ npm install --save @libs-jb/cloud-firestore-cache
- This is an EARLY, UNSTABLE, PREVIEW RELEASE of the project.
⚠️ Until v1 is released, it is expected to have breaking changes and other issues.
From @libs-jb/cloud-firestore-cache
we can use the following functions:
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
const { FirestoreCache } = require("@libs-jb/cloud-firestore-cache");
initializeApp();
const firestoreInstance = getFirestore();
const db = FirestoreCache(firestoreInstance);
// Add Doc to Collection
db.add("test_collection", { test: "test" }, { fetch: true }).then((result) => {
console.log("ADD RESULT: ", result);
});
// Set data
db.set("test_collection/test_id", { test: "test" }, { merge: true, fetch: true }).then((result) => {
console.log("SET RESULT: ", result);
});
// Get data
db.get("test_collection/test_id").then((result) => {
console.log("GET RESULT: ", result);
});
// Update data
db.update("test_collection/test_id", { test: "updated" }).then((result) => {
console.log("UPDATE RESULT: ", result); // `null` - as {fetch: true} is not passed
});
// Delete data
db.delete("test_collection/test_id").then((result) => {
console.log("DELETE RESULT: ", result);
});
In repo1
- for handling Firestore operations:
const { onRequest } = require("firebase-functions/v2/https");
const { initializeApp } = require("firebase-admin/app");
const { getFirestore } = require("firebase-admin/firestore");
const { FirestoreCache } = require("@libs-jb/cloud-firestore-cache");
initializeApp();
const firestoreInstance = getFirestore();
const db = FirestoreCache(firestoreInstance);
exports.db_handler = onRequest(async (request, response) => {
const { path, data, type } = request.body;
switch (type) {
case "get":
db.get(path).then((result) => {
response.send(result);
});
break;
case "add":
db.add(path, data).then((result) => {
response.send(result);
});
break;
case "set":
db.set(path, data).then((result) => {
response.send(result);
});
break;
case "update":
db.update(path, data).then((result) => {
response.send(result);
});
break;
case "delete":
db.delete(path).then((result) => {
response.send(result);
});
break;
default:
response.send("Invalid type");
}
});
In repo2
- for calling Firestore operations or any other micro service:
const { onRequest } = require("firebase-functions/v2/https");
const axios = require("axios");
const isEmulator = process.env.FUNCTIONS_EMULATOR === "true";
const fetchCall = (data) =>
axios.post(
isEmulator
? "http://127.0.0.1:5001/demo-project/us-central1/db_handler"
: "https://us-central1-demo-project.cloudfunctions.net/db_handler",
data
);
exports.testcollectionhandler = onRequest(async (request, response) => {
const testCollection = "test_collection";
const test_id = "test_id";
// Set data
await fetchCall({
path: `${testCollection}/${test_id}`,
type: "set",
data: { test: "test" },
}).then((result) => {
console.log("SET RESULT: ", result.data);
});
// Get data
await fetchCall({
path: `${testCollection}/${test_id}`,
type: "get",
}).then((result) => {
console.log("GET RESULT: ", result.data);
response.send({ result: "success", data: result.data.data });
response.end();
});
});
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Jeet Dhandha
Released under the MIT License.