Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Created Jul 17, 2017
Embed
What would you like to do?
Algolia Firebase Cloud Functions - Update/Delete Items from Index
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
const algoliasearch = require('algoliasearch');
const algolia = algoliasearch(functions.config().algolia.appid, functions.config().algolia.adminkey);
exports.updateIndex = functions.database.ref('/books/{bookId}').onWrite(event => {
const index = algolia.initIndex('books');
const bookId = event.params.bookId
const data = event.data.val()
if (!data) {
return index.deleteObject(bookId, (err) => {
if (err) throw err
console.log('Book Removed from Algolia Index', bookId)
})
}
data['objectID'] = bookId
return index.saveObject(data, (err, content) => {
if (err) throw err
console.log('Book Updated in Algolia Index', data.objectID)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment