mirror of
https://github.com/rowyio/rowy.git
synced 2026-02-24 04:01:17 +01:00
Feat: algolia firestore trigger integration
This commit is contained in:
44
cloud_functions/functions/src/algolia/algoliaConfig.ts
Normal file
44
cloud_functions/functions/src/algolia/algoliaConfig.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
const algoliaConfig = [
|
||||
{
|
||||
name: "founders",
|
||||
fieldsToSync: [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"founderType",
|
||||
"cohort",
|
||||
"employerLogos",
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "advisors",
|
||||
fieldsToSync: [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"location",
|
||||
"title",
|
||||
"type",
|
||||
"bio",
|
||||
"linkedIn",
|
||||
"expertise",
|
||||
"profilePhoto",
|
||||
"bookingLink",
|
||||
"twitter",
|
||||
"antlerPoc",
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "portfolio",
|
||||
fieldsToSync: [
|
||||
"cohort",
|
||||
"companyName",
|
||||
"oneLine",
|
||||
"logo",
|
||||
"website",
|
||||
"crunchbase",
|
||||
"angelList",
|
||||
"sector",
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default algoliaConfig;
|
||||
@@ -1,18 +0,0 @@
|
||||
const algoliaConfig = [
|
||||
{ name: "founders" },
|
||||
{
|
||||
name: "portfolio",
|
||||
fieldsToSync: [
|
||||
"cohort",
|
||||
"company",
|
||||
"oneLine",
|
||||
"logo",
|
||||
"website",
|
||||
"crunchbase",
|
||||
"angelList",
|
||||
"sector",
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default algoliaConfig;
|
||||
@@ -2,60 +2,68 @@ import * as algoliasearch from "algoliasearch";
|
||||
import * as functions from "firebase-functions";
|
||||
|
||||
import { env } from "../config";
|
||||
import collectionsConfig from "./collectionsConfig";
|
||||
|
||||
const APP_ID = env.algolia.app;
|
||||
const ADMIN_KEY = env.algolia.key;
|
||||
|
||||
const client = algoliasearch(APP_ID, ADMIN_KEY);
|
||||
|
||||
// returns object of fieldsToSync
|
||||
const algoliaReducer = docData => (acc: any, curr: string) => {
|
||||
if (docData[curr]) return { ...acc, [curr]: docData[curr] };
|
||||
const algoliaReducer = (docData: FirebaseFirestore.DocumentData) => (
|
||||
acc: any,
|
||||
curr: string
|
||||
) => {
|
||||
if (docData?.[curr]) return { ...acc, [curr]: docData[curr] };
|
||||
else return acc;
|
||||
};
|
||||
|
||||
const addToAlgolia = (fieldsToSync?: string[]) => (
|
||||
const addToAlgolia = (fieldsToSync: string[]) => (
|
||||
snapshot: FirebaseFirestore.DocumentSnapshot
|
||||
) => {
|
||||
const collectionName = snapshot.ref.parent.id;
|
||||
const objectID = snapshot.id;
|
||||
const docData = snapshot.data();
|
||||
const index = client.initIndex(collectionName);
|
||||
if (fieldsToSync === undefined)
|
||||
return index.addObject({ ...docData, objectID });
|
||||
if (!docData) return false; // returns if theres no data in the doc
|
||||
const algoliaData = fieldsToSync.reduce(algoliaReducer(docData), {});
|
||||
return index.addObject({ ...algoliaData, objectID });
|
||||
if (Object.keys(algoliaData).length === 0) return false; // returns if theres nothing to sync
|
||||
const index = client.initIndex(collectionName); // initialize algolia index
|
||||
return index.addObject({ ...algoliaData, objectID }); // add new algolia entry
|
||||
};
|
||||
|
||||
const updateAlgolia = (fieldsToSync?: string[]) => (
|
||||
const updateAlgolia = (fieldsToSync: string[]) => (
|
||||
snapshot: functions.Change<FirebaseFirestore.DocumentSnapshot>
|
||||
) => {
|
||||
const collectionName = snapshot.after.ref.parent.id;
|
||||
const objectID = snapshot.after.id;
|
||||
const docData = snapshot.after.data();
|
||||
const index = client.initIndex(collectionName);
|
||||
|
||||
if (fieldsToSync === undefined)
|
||||
return index.saveObject({ ...docData, objectID });
|
||||
if (!docData) return false; // returns if theres no data in the doc
|
||||
const algoliaData = fieldsToSync.reduce(algoliaReducer(docData), {});
|
||||
return index.addObject({ ...algoliaData, objectID });
|
||||
if (Object.keys(algoliaData).length === 0) return false; // returns if theres nothing to sync
|
||||
const index = client.initIndex(collectionName); // initialize algolia index
|
||||
return index.saveObject({ ...algoliaData, objectID }); // add update algolia entry
|
||||
};
|
||||
|
||||
const deleteFromAlgolia = (snapshot: FirebaseFirestore.DocumentSnapshot) => {
|
||||
const collectionName = snapshot.ref.parent.id;
|
||||
const objectID = snapshot.id;
|
||||
const index = client.initIndex(collectionName);
|
||||
return index.deleteObject(objectID);
|
||||
const index = client.initIndex(collectionName); // initialize algolia index
|
||||
return index.deleteObject(objectID); // delete algolia entry
|
||||
};
|
||||
|
||||
module.exports = collectionsConfig.map(collection => ({
|
||||
[`${collection.name}_onCreate`]: functions.firestore
|
||||
/**
|
||||
* returns 3 different trigger functions (onCreate,onUpdate,onDelete) in an object
|
||||
* @param collection configuration object
|
||||
*/
|
||||
const algoliaFnsGenerator = collection => ({
|
||||
onCreate: functions.firestore
|
||||
.document(`${collection.name}/{docId}`)
|
||||
.onCreate(addToAlgolia(collection.fieldsToSync)),
|
||||
[`${collection.name}_onUpdate`]: functions.firestore
|
||||
onUpdate: functions.firestore
|
||||
.document(`${collection.name}/{docId}`)
|
||||
.onUpdate(updateAlgolia(collection.fieldsToSync)),
|
||||
[`${collection.name}_onDelete`]: functions.firestore
|
||||
onDelete: functions.firestore
|
||||
.document(`${collection.name}/{docId}`)
|
||||
.onDelete(deleteFromAlgolia),
|
||||
}));
|
||||
});
|
||||
|
||||
export default algoliaFnsGenerator;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
export { exportTable } from "./export";
|
||||
import * as algoliaFunctions from "./algolia";
|
||||
export const ALGOLIA = { ...algoliaFunctions };
|
||||
import algoliaFnsGenerator from "./algolia";
|
||||
import algoliaConfig from "./algolia/algoliaConfig";
|
||||
export const algolia = algoliaConfig.reduce((acc: any, collection) => {
|
||||
return { ...acc, [collection.name]: algoliaFnsGenerator(collection) };
|
||||
}, {});
|
||||
|
||||
10
cloud_functions/node_modules/.yarn-integrity
generated
vendored
Normal file
10
cloud_functions/node_modules/.yarn-integrity
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"systemParams": "darwin-x64-64",
|
||||
"modulesFolders": [],
|
||||
"flags": [],
|
||||
"linkedModules": [],
|
||||
"topLevelPatterns": [],
|
||||
"lockfileEntries": {},
|
||||
"files": [],
|
||||
"artifacts": {}
|
||||
}
|
||||
Reference in New Issue
Block a user