Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active March 4, 2021 17:30
Show Gist options
  • Save erfg12/80f4645dc6ab28ac4214fcecfbf5a01e to your computer and use it in GitHub Desktop.
Save erfg12/80f4645dc6ab28ac4214fcecfbf5a01e to your computer and use it in GitHub Desktop.
// ABOUT: This is how you import and sync your Firestore data in Algolia via Firebase functions.
// You need NodeJS and NPM installed prior on your dev PC and have created your Algolia and firebase project(s).
// Open terminal/cmd type in (NOTE: Use TypeScript):
// npm install -g firebase-tools
// firebase login
// firebase init
// npm install algoliasearch --save
// firebase functions:config:set algolia.appid="YOUR_APP_ID" algolia.apikey="YOUR_API_KEY"
// Open 'functions/src/index.ts' with VSCode/Atom/NotePad and put this code in it.
// In terminal/cmd type in 'firebase deploy' to make it go live. This process can be kinda slow at times.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import algoliasearch from 'algoliasearch';
// Set up Firestore.
admin.initializeApp();
const db = admin.firestore();
// Set up Algolia.
// The app id and API key are coming from the cloud functions environment, as we set up in Part 1, Step 3.
const algoliaClient = algoliasearch(functions.config().algolia.appid, functions.config().algolia.apikey);
const collectionIndex = algoliaClient.initIndex('ALGOLIA_INDEX_NAME_HERE');
// HTTP request to manually add index to Algolia
export const sendCollectionToAlgolia = functions.https.onRequest(async (req, res) => {
// This array will contain all records to be indexed in Algolia.
const algoliaRecords : any[] = [];
// Retrieve all documents from the COLLECTION collection.
const querySnapshot = await db.collection('FIRESTORE_COLLECTION_NAME_HERE').get();
querySnapshot.docs.forEach(doc => {
const document = doc.data();
// Essentially, you want your records to contain any information that facilitates search,
// display, filtering, or relevance. Otherwise, you can leave it out.
const record = {
objectID: doc.id,
title: document.title,
content: document.content
};
algoliaRecords.push(record);
});
collectionIndex.saveObjects(algoliaRecords).wait().then(() => {
res.status(200).send("topics was indexed to Algolia successfully.");
});
});
// Automatic request on firebase doc update
export const algoliaTopicsSync = functions
.firestore.document(`FIRESTORE_COLLECTION_NAME_HERE/{docId}`).onWrite(async (change, _context) => {
const oldData = change.before
const newData = change.after
const data = newData.data()
const objectID = newData.id
if (!oldData.exists && newData.exists) {
// creating
return collectionIndex.saveObject(Object.assign({}, {
objectID
}, data))
} else if (!newData.exists && oldData.exists) {
// deleting
return collectionIndex.deleteObject(objectID)
} else {
// updating
return collectionIndex.saveObject(Object.assign({}, {
objectID
}, data))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment