algolia config on firetstore

This commit is contained in:
Shams mosowi
2020-02-14 10:27:32 +11:00
parent 90a10457aa
commit 526f28e0e1
5 changed files with 38 additions and 96 deletions

1
.gitignore vendored
View File

@@ -19,6 +19,7 @@ cloud_functions/functions/lib
cloud_functions/functions/src/collectionSync/config.json
cloud_functions/functions/src/history/config.json
cloud_functions/functions/src/algolia/config.json
cloud_functions/functions/firebase-credentials.json
# misc

View File

@@ -25,11 +25,14 @@ const main = async () => {
"_FIRETABLE_/_SETTINGS_/_CONFIG_/_HISTORY_",
"./src/history/config.json"
);
await docConfig2json(
"_FIRETABLE_/_SETTINGS_/_CONFIG_/_ALGOLIA_",
"./src/algolia/config.json"
);
await docConfig2json(
"_FIRETABLE_/_SETTINGS_/_CONFIG_/_COLLECTION_SYNC_",
"./src/collectionSync/config.json"
);
return true;
};

View File

@@ -1,78 +0,0 @@
import * as _ from "lodash";
const filterSnapshot = (
field: { docPath: string; snapshot: any },
preservedKeys: string[]
) => {
return {
docPath: field.docPath,
...preservedKeys.reduce((acc: any, currentKey: string) => {
const value = _.get(field.snapshot, currentKey);
if (value) {
return { ...acc, snapshot: { [currentKey]: value, ...acc.snapshot } };
} else return acc;
}, {}),
};
};
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: "teams",
fieldsToSync: [
"createdAt",
"createdBy",
"updatedAt",
"updatedBy",
"cohort",
"teamName",
"trackOutDate",
{
fieldName: "teamMembers",
transformer: (teamMembers: any[]) => {
return teamMembers.map(member =>
filterSnapshot(member, [
"cohort",
"firstName",
"lastName",
"preferredName",
"profilePhoto",
])
);
},
},
"focusArea",
"isDissolved",
"oneLineDescription",
],
},
{
name: "cohorts",
fieldsToSync: ["cohort", "location", "demoDay"],
},
];
export default algoliaConfig;

View File

@@ -1,6 +1,6 @@
import * as algoliasearch from "algoliasearch";
import * as functions from "firebase-functions";
import * as _ from "lodash";
import { env } from "../config";
const APP_ID = env.algolia.app;
@@ -8,10 +8,25 @@ const ADMIN_KEY = env.algolia.key;
const client = algoliasearch(APP_ID, ADMIN_KEY);
const filterSnapshot = (
field: { docPath: string; snapshot: any },
preservedKeys: string[]
) => {
return {
docPath: field.docPath,
...preservedKeys.reduce((acc: any, currentKey: string) => {
const value = _.get(field.snapshot, currentKey);
if (value) {
return { ...acc, snapshot: { [currentKey]: value, ...acc.snapshot } };
} else return acc;
}, {}),
};
};
// returns object of fieldsToSync
const algoliaReducer = (docData: FirebaseFirestore.DocumentData) => (
acc: any,
curr: string | { fieldName: string; transformer: Function }
curr: string | { fieldName: string; snapshotFields: string[] }
) => {
if (typeof curr === "string") {
if (docData[curr] && typeof docData[curr].toDate === "function") {
@@ -25,10 +40,12 @@ const algoliaReducer = (docData: FirebaseFirestore.DocumentData) => (
return acc;
}
} else {
if (docData[curr.fieldName]) {
if (docData[curr.fieldName] && curr.snapshotFields) {
return {
...acc,
[curr.fieldName]: curr.transformer(docData[curr.fieldName]),
[curr.fieldName]: docData[curr.fieldName].map(snapshot =>
filterSnapshot(snapshot, curr.snapshotFields)
),
};
} else {
return acc;

View File

@@ -1,5 +1,5 @@
import algoliaFnsGenerator from "./algolia";
import algoliaConfig from "./algolia/algoliaConfig";
import * as algoliaConfig from "./algolia/config.json";
import collectionSyncFnsGenerator from "./collectionSync";
import * as collectionSyncConfig from "./collectionSync/config.json";
@@ -8,12 +8,12 @@ import * as collectionHistoryConfig from "./history/config.json";
export { exportTable } from "./export";
import * as callableFns from "./callable";
export const callable = callableFns;
export const algolia = algoliaConfig.reduce((acc: any, collection) => {
const callable = callableFns;
const algolia = algoliaConfig.reduce((acc: any, collection) => {
return { ...acc, [collection.name]: algoliaFnsGenerator(collection) };
}, {});
export const sync = collectionSyncConfig.reduce((acc: any, collection) => {
const sync = collectionSyncConfig.reduce((acc: any, collection) => {
return {
...acc,
[`${collection.source}2${collection.target}`]: collectionSyncFnsGenerator(
@@ -22,12 +22,11 @@ export const sync = collectionSyncConfig.reduce((acc: any, collection) => {
};
}, {});
export const history = collectionHistoryConfig.reduce(
(acc: any, collection) => {
return {
...acc,
[collection.name]: collectionSnapshotFnsGenerator(collection),
};
},
{}
);
const history = collectionHistoryConfig.reduce((acc: any, collection) => {
return {
...acc,
[collection.name]: collectionSnapshotFnsGenerator(collection),
};
}, {});
export const FIRETABLE = { callable, algolia, sync, history };