Skip to content

Instantly share code, notes, and snippets.

@JesseE
Created January 22, 2019 14:14
Show Gist options
  • Save JesseE/f18e27c97acdd5e498c1bafb7a7b6f78 to your computer and use it in GitHub Desktop.
Save JesseE/f18e27c97acdd5e498c1bafb7a7b6f78 to your computer and use it in GitHub Desktop.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as algoliasearch from 'algoliasearch';
admin.initializeApp();
const client = algoliasearch(
functions.config().algolia.app_id,
functions.config().algolia.api_key
);
const index = client.initIndex('movie_title');
exports.addMovies = functions.firestore
.document('movies/{movieId}')
.onCreate((snap, context) => {
const data = snap.data();
const objectID = context.params.movieId
return index.addObject({
objectID,
...data,
poster: null,
metascore: null
})
});
@JesseE
Copy link
Author

JesseE commented Aug 14, 2019

This is a cloud functions inside firebase firestore that listens to when a new movie is create in the firestore database. When the onCreate function gets triggered it will create a new autocomplete value in algolia's index.

Algolia is a solution for when you need a fast autocomplete search in your website. And is used often with large databases.

This code works by signing in to firebase and use the configured app_id and api_key to add a movie to algolia's database. After its done it can be suggested to you in a search field as a value to autocomplete your search term.

...data adds all the elements inside the data object. It removes elements poster and metascore by setting it to null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment