Skip to content

jeet-dhandha/cloud-firestore-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@libs-jd/cloud-firestore-cache npm version

Firestore Cache a Solution for Cloud Function based fetching and caching of Firestore data.

Install

Install with npm:

$ npm install --save @libs-jb/cloud-firestore-cache

Early Alpha : Unstable ⚠️

  • This is an EARLY, UNSTABLE, PREVIEW RELEASE of the project. ⚠️ Until v1 is released, it is expected to have breaking changes and other issues.

Usage

From @libs-jb/cloud-firestore-cache we can use the following functions:

Using Cache in a Single Repo

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

Using Cache Function in Another Repo for Faster Execution

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

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jeet Dhandha

License

Released under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published