Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add tableSchema to context #21

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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