-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgoliaFirebase.js
59 lines (53 loc) · 1.8 KB
/
algoliaFirebase.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use strict";
const algoliasearch = require('algoliasearch');
const firebase = require('firebase-admin');
class AlgoliaFirebase {
constructor(configFirebase, indexNameFirebase, configAlgolia, indexNameAlgolia) {
this.firebase = firebase.initializeApp({
...configFirebase,
credential: firebase.credential.cert(configFirebase.credential),
});
this.algolia = algoliasearch(configAlgolia.appId, configAlgolia.apiKey)
.initIndex(indexNameAlgolia)
this.algolia.setSettings({
searchableAttributes: [
'email,firstName,lastName',
],
});
const ref = this.firebase.database().ref(`/${indexNameFirebase}`);
ref.on('child_added', this.addOrUpdateIndexRecord.bind(this));
ref.on('child_changed', this.addOrUpdateIndexRecord.bind(this));
ref.on('child_removed', this.deleteIndexRecord.bind(this));
}
addOrUpdateIndexRecord(user) {
// Get Firebase object
const record = user.val();
// Specify Algolia's objectID using the Firebase object key
record.objectID = user.key;
// Add or update object
this.algolia
.saveObject(record)
.then(() => {
console.log('Firebase object indexed in Algolia', record.objectID);
})
.catch(error => {
console.error('Error when indexing user into Algolia', error);
process.exit(1);
});
}
deleteIndexRecord(user) {
// Get Algolia's objectID from the Firebase object key
const objectID = user.key;
// Remove the object from Algolia
this.algolia
.deleteObject(objectID)
.then(() => {
console.log('Firebase object deleted from Algolia', objectID);
})
.catch(error => {
console.error('Error when deleting user from Algolia', error);
process.exit(1);
});
}
}
module.exports = AlgoliaFirebase;