I am new to using Algolia. I'm trying to send user data to Aloglia from my Firestore so that I can search for them later in the app I am creating. I've successfully set everything up, all my functions work via the Firebase cloud function. So when I create a document for 'users' in Firestore it passes all of the fields to Algolia, updating and deleting this data is also manifested in Algolia.
However, for the sake of maintaining security, I do not want to send ALL of the user data to Algolia, but rather only a few fields. These include 'displayName' and 'username' (two of the fields in the 'users' collection).
So my question is how do I change my code to only send those two fields to Algolia? Please provide an answer for delete and update as well.
My Code:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');
const ALGOLIA_APP_ID = "MY KEY";
const ALGOLIA_ADMIN_KEY = "MY KEY";
const ALGOLIA_INDEX_NAME = "users";
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
admin.initializeApp(functions.config().firebase);
exports.createUser = functions.firestore
.document('users/{userID}')
.onCreate( async (snap, context) => {
const newValue = snap.data();
newValue.objectID = snap.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(newValue);
});
exports.updateUser = functions.firestore
.document('users/{userID}')
.onUpdate( async (snap, context) => {
const afterUpdate = snap.after.data();
afterUpdate.objectID = snap.after.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(afterUpdate);
})
exports.deleteUser = functions.firestore
.document('users/{userID}/')
.onDelete( async (snap, context) => {
const oldID = snap.id;
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.deleteObject(oldID);
});