Skip to content

Instantly share code, notes, and snippets.

@shadow1349
Last active September 16, 2019 21:04
Show Gist options
  • Save shadow1349/cac74a07ff3fdbbc4aec9e55d008cc91 to your computer and use it in GitHub Desktop.
Save shadow1349/cac74a07ff3fdbbc4aec9e55d008cc91 to your computer and use it in GitHub Desktop.
import * as algoliasearch from "algoliasearch";
import * as functions from "firebase-functions";
import { Model } from "firebasenoteapptypes";
const algolia = algoliasearch(
functions.config().algolia.appid,
functions.config().algolia.adminkey
);
type IndexName = "Notes" | "Users" | "Notifications";
const Indicies: { [key: string]: algoliasearch.Index } = {
Notes: algolia.initIndex("Notes"),
Users: algolia.initIndex("Users"),
Notifications: algolia.initIndex("Notifications")
};
export class AlgoliaSearch {
index: algoliasearch.Index;
constructor(indexName: IndexName) {
if (!Indicies[`${indexName}`]) {
throw new Error(`There was no ${indexName} found`);
}
this.index = Indicies[`${indexName}`];
}
store(model: Model) {
return this.index.addObject(model.toJSON());
}
search<T>(params: algoliasearch.QueryParameters) {
return this.index.search<T>(params);
}
delete(id: string) {
return this.index.deleteObject(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment