Skip to content

Commit

Permalink
add table schema to function context
Browse files Browse the repository at this point in the history
  • Loading branch information
Han Tuerker committed Dec 19, 2022
1 parent ee846f5 commit 2ed2995
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/functionBuilder/compiler/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const getConfigFromTableSchema = async (
fieldTypes,
extensions,
runtimeOptions,
tableSchema: schemaData,
};
if (schemaData.searchIndex) {
config.searchIndex = {
Expand Down Expand Up @@ -86,6 +87,7 @@ export const combineConfigs = (configs: any[]) =>
extensions,
searchIndex,
runtimeOptions,
tableSchema,
} = cur;
return {
derivativeColumns: [...acc.derivativeColumns, ...derivativeColumns],
Expand All @@ -105,6 +107,7 @@ export const combineConfigs = (configs: any[]) =>
? [...acc.searchIndices, searchIndex]
: acc.searchIndices,
runtimeOptions,
tableSchema,
};
},
{
Expand All @@ -131,6 +134,7 @@ export const generateFile = async (configData, buildFolderTimestamp) => {
region,
searchHost,
runtimeOptions,
tableSchema,
} = configData;
const serializedConfigData = {
fieldTypes: JSON.stringify(fieldTypes),
Expand All @@ -144,6 +148,7 @@ export const generateFile = async (configData, buildFolderTimestamp) => {
serviceAccount: `rowy-functions@${projectId}.iam.gserviceaccount.com`,
...runtimeOptions,
}),
tableSchema: JSON.stringify(tableSchema),
region: JSON.stringify(region),
searchHost: JSON.stringify(searchHost),
searchIndices: JSON.stringify(searchIndices),
Expand Down
1 change: 1 addition & 0 deletions src/functionBuilder/compiler/loader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ export interface TableConfig {
fields: string[];
};
runtimeOptions: IRuntimeOptions;
tableSchema: any;
}
3 changes: 2 additions & 1 deletion src/functionBuilder/functions/src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import utilFns, { hasRequiredFields, getTriggerType } from "../utils";
import { db, auth, storage } from "../firebaseConfig";

const extension =
(extensionConfig, fieldTypes) =>
(extensionConfig, fieldTypes, tableSchema) =>
async (
change: functions.Change<functions.firestore.DocumentSnapshot>,
context: functions.EventContext
Expand Down Expand Up @@ -33,6 +33,7 @@ const extension =
utilFns,
fieldTypes,
storage,
tableSchema,
};
if (!triggers.includes(triggerType)) return false; //check if trigger type is included in the extension
if (
Expand Down
6 changes: 5 additions & 1 deletion src/functionBuilder/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export const R = {
extensionConfig.triggers.includes(triggerType)
)
.map((extensionConfig) =>
extension(extensionConfig, functionConfig.fieldTypes)(change, context)
extension(
extensionConfig,
functionConfig.fieldTypes,
functionConfig.tableSchema
)(change, context)
);
console.log(
`#${
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export const actionScript = async (req: Request, res: Response) => {
user: { ...authUser2rowyUser(user), roles: userRoles },
fetch,
rowy,
tableSchema: schemaDocData,
logging,
tableSchema: schemaDocData,
});
if (result.success || result.status) {
const cellValue = {
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ConnectorArgs = {
fetch: any;
storage: admin.storage.Storage;
logging: RowyLogging;
tableSchema: any;
};

type Connector = (args: ConnectorArgs) => Promise<any[]> | any[];
Expand Down Expand Up @@ -77,7 +78,7 @@ export const connector = async (req: Request, res: Response) => {
);

const connectorScript = eval(
`async ({ row, db, ref, auth, fetch, rowy, storage, logging }) =>` +
`async ({ row, db, ref, auth, fetch, rowy, storage, logging, tableSchema }) =>` +
connectorFnBody
) as Connector;
const pattern = /row(?!y)/;
Expand All @@ -96,6 +97,7 @@ export const connector = async (req: Request, res: Response) => {
storage,
rowy,
logging,
tableSchema: schemaDocData,
});

const functionEndTime = Date.now();
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/derivative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const evaluateDerivative = async (req: Request, res: Response) => {
);

const derivativeFunction = eval(
`async ({ row, db, ref, auth, fetch, rowy, logging }) =>` +
`async ({ row, db, ref, auth, fetch, rowy, logging, tableSchema }) =>` +
code.replace(/^.*=>/, "")
);
let rowSnapshots: FirebaseFirestore.DocumentSnapshot<FirebaseFirestore.DocumentData>[] =
Expand Down Expand Up @@ -97,6 +97,7 @@ export const evaluateDerivative = async (req: Request, res: Response) => {
fetch,
rowy,
logging,
tableSchema: schemaDocData,
});
const update = { [columnKey]: result };
if (schemaDocData?.audit !== false) {
Expand Down

0 comments on commit 2ed2995

Please sign in to comment.