Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Firebase Functions to V2 API and Update Dependencies #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
598 changes: 291 additions & 307 deletions functions/package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"main": "src/index.js",
"dependencies": {
"@babel/runtime": "^7.24.7",
"firebase-admin": "^12.2.0",
"firebase-functions": "^5.0.1",
"dotenv": "^16.4.7",
"firebase-admin": "^13.0.1",
"firebase-functions": "^6.1.1",
"lodash.get": "^4.4.2",
"typesense": "^1.8.2"
},
Expand Down
23 changes: 11 additions & 12 deletions functions/src/backfill.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const functions = require("firebase-functions");
const {onDocumentWritten} = require("firebase-functions/v2/firestore");
const {info, error} = require("firebase-functions/logger");
const admin = require("firebase-admin");
const config = require("./config.js");
const createTypesenseClient = require("./createTypesenseClient.js");
Expand All @@ -10,32 +11,30 @@ admin.initializeApp({

const validateBackfillRun = (snapshot) => {
if (![true, "true"].includes(snapshot.after.get("trigger"))) {
functions.logger.error("Skipping backfill. `trigger: true` key " + `was not found in Firestore document ${config.typesenseBackfillTriggerDocumentInFirestore}.`);
error("Skipping backfill. `trigger: true` key " + `was not found in Firestore document ${config.typesenseBackfillTriggerDocumentInFirestore}.`);
return false;
}

// Check if there's a collection specific sync setup
const collectionsToSync = snapshot.after.get("firestore_collections");
if (Array.isArray(collectionsToSync) && !collectionsToSync.includes(config.firestoreCollectionPath)) {
functions.logger.error(
"Skipping backfill. The `firestore_collections` key in " + `${config.typesenseBackfillTriggerDocumentInFirestore} did not contain collection ${config.firestoreCollectionPath}.`,
);
error("Skipping backfill. The `firestore_collections` key in " + `${config.typesenseBackfillTriggerDocumentInFirestore} did not contain collection ${config.firestoreCollectionPath}.`);
return false;
}

return true;
};

module.exports = functions.firestore.document(config.typesenseBackfillTriggerDocumentInFirestore).onWrite(async (snapshot, context) => {
functions.logger.info(
module.exports = onDocumentWritten(config.typesenseBackfillTriggerDocumentInFirestore, async (event) => {
info(
"Backfilling " +
`${config.firestoreCollectionFields.join(",")} fields in Firestore documents ` +
`from ${config.firestoreCollectionPath} ` +
`into Typesense Collection ${config.typesenseCollectionName} ` +
`on ${config.typesenseHosts.join(",")}`,
);

if (!validateBackfillRun(snapshot)) {
if (!validateBackfillRun(event.data)) {
return false;
}

Expand All @@ -60,9 +59,9 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
lastDoc = thisBatch.docs.at(-1) ?? null;
try {
await typesense.collections(encodeURIComponent(config.typesenseCollectionName)).documents().import(currentDocumentsBatch);
functions.logger.info(`Imported ${currentDocumentsBatch.length} documents into Typesense`);
info(`Imported ${currentDocumentsBatch.length} documents into Typesense`);
} catch (error) {
functions.logger.error(`Import error in a batch of documents from ${currentDocumentsBatch[0].id} to ${lastDoc.id}`, error);
error(`Import error in a batch of documents from ${currentDocumentsBatch[0].id} to ${lastDoc.id}`, error);
if ("importResults" in error) {
logImportErrors(error.importResults);
}
Expand All @@ -76,7 +75,7 @@ module.exports = functions.firestore.document(config.typesenseBackfillTriggerDoc
await new Promise((resolve) => process.nextTick(resolve));
} while (lastDoc);

functions.logger.info("Done backfilling to Typesense from Firestore");
info("Done backfilling to Typesense from Firestore");
});

/**
Expand All @@ -87,6 +86,6 @@ function logImportErrors(importResults) {
importResults.forEach((result) => {
if (result.success) return;

functions.logger.error(`Error importing document with error: ${result.error}`, result);
error(`Error importing document with error: ${result.error}`, result);
});
}
3 changes: 3 additions & 0 deletions functions/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
require("dotenv").config();

exports.indexOnWrite = require("./indexOnWrite.js");
exports.backfill = require("./backfill.js");

42 changes: 18 additions & 24 deletions functions/src/indexOnWrite.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
const functions = require("firebase-functions");
const {onDocumentWritten} = require("firebase-functions/v2/firestore");
const {debug} = require("firebase-functions/logger");
const config = require("./config.js");
const createTypesenseClient = require("./createTypesenseClient.js");
const utils = require("./utils.js");

module.exports = functions.firestore.document(config.firestoreCollectionPath)
.onWrite(async (snapshot, context) => {
const typesense = createTypesenseClient();
module.exports = onDocumentWritten(config.firestoreCollectionPath, async (event) => {
const typesense = createTypesenseClient();

if (snapshot.after.data() == null) {
// Delete
const documentId = snapshot.before.id;
functions.logger.debug(`Deleting document ${documentId}`);
return await typesense
.collections(encodeURIComponent(config.typesenseCollectionName))
.documents(encodeURIComponent(documentId))
.delete();
} else {
// Create / update
if (event.data.after.data() == null) {
// Delete
const documentId = event.data.before.id;
debug(`Deleting document ${documentId}`);
return await typesense.collections(encodeURIComponent(config.typesenseCollectionName)).documents(encodeURIComponent(documentId)).delete();
} else {
// Create / update

// snapshot.after.ref.get() will refetch the latest version of the document
const latestSnapshot = await snapshot.after.ref.get();
const typesenseDocument = await utils.typesenseDocumentFromSnapshot(latestSnapshot);
// event.data.after.ref.get() will refetch the latest version of the document
const latestEventData = await event.data.after.ref.get();
const typesenseDocument = await utils.typesenseDocumentFromevent.data(latestEventData.data);

functions.logger.debug(`Upserting document ${JSON.stringify(typesenseDocument)}`);
return await typesense
.collections(encodeURIComponent(config.typesenseCollectionName))
.documents()
.upsert(typesenseDocument);
}
});
debug(`Upserting document ${JSON.stringify(typesenseDocument)}`);
return await typesense.collections(encodeURIComponent(config.typesenseCollectionName)).documents().upsert(typesenseDocument);
}
});
Loading
Loading