Created
July 17, 2017 17:40
-
-
Save codediodeio/4a84e135513866b8a68a21937161f0ce to your computer and use it in GitHub Desktop.
Algolia Firebase Cloud Functions - Update/Delete Items from Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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