-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
27 lines (20 loc) · 916 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var functions = require('firebase-functions');
const algoliasearch = require('algoliasearch');
const client = algoliasearch(functions.config().algolia.app_id, functions.config().algolia.api_key);
// Name fo the algolia index for Blog posts content.
const ALGOLIA_POSTS_INDEX_NAME = 'items';
exports.algoliaTest = functions.database.ref('/item/{itemId}/')
.onWrite(event => {
const index = client.initIndex(ALGOLIA_POSTS_INDEX_NAME);
// remove item for delete event in firebase
if (!event.data.exists()) {
console.log('item deleted', event.params.itemId);
return index.deleteObject(event.params.itemId);
}
// create algolia object to insert/update
var item = event.data.val();
item["objectID"] = item.key
console.log(item);
console.log('item inserted or updated', event.params.itemId)
return index.saveObject(item);
});