Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Created July 17, 2017 17:40
Show Gist options
  • Save codediodeio/4a84e135513866b8a68a21937161f0ce to your computer and use it in GitHub Desktop.
Save codediodeio/4a84e135513866b8a68a21937161f0ce to your computer and use it in GitHub Desktop.
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