From 25d86d179dc08e083bfcd74ed0d41b4c096c5b02 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 15 May 2024 10:56:55 +0200 Subject: [PATCH] Handle inputProcessing on bulk row import --- .../api/controllers/row/ExternalRequest.ts | 19 +++++++++++++++++-- .../src/api/controllers/table/external.ts | 13 +++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index be6ac885df..bd92413851 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -265,7 +265,10 @@ export class ExternalRequest { } } - inputProcessing(row: Row | undefined, table: Table) { + inputProcessing( + row: T, + table: Table + ): { row: T; manyRelationships: ManyRelationship[] } { if (!row) { return { row, manyRelationships: [] } } @@ -346,7 +349,7 @@ export class ExternalRequest { // we return the relationships that may need to be created in the through table // we do this so that if the ID is generated by the DB it can be inserted // after the fact - return { row: newRow, manyRelationships } + return { row: newRow as T, manyRelationships } } /** @@ -598,6 +601,18 @@ export class ExternalRequest { // clean up row on ingress using schema const processed = this.inputProcessing(row, table) row = processed.row + let manyRelationships = processed.manyRelationships + + if (!row && rows) { + manyRelationships = [] + for (let i = 0; i < rows.length; i++) { + const processed = this.inputProcessing(rows[i], table) + rows[i] = processed.row + if (processed.manyRelationships.length) { + manyRelationships.push(...processed.manyRelationships) + } + } + } if ( operation === Operation.DELETE && (filters == null || Object.keys(filters).length === 0) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index e526af4ecb..bd674d7d38 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -15,6 +15,7 @@ import { } from "@budibase/types" import sdk from "../../../sdk" import { builderSocket } from "../../../websockets" +import { inputProcessing } from "../../../utilities/rowProcessor" function getDatasourceId(table: Table) { if (!table) { @@ -80,7 +81,7 @@ export async function destroy(ctx: UserCtx) { export async function bulkImport( ctx: UserCtx ) { - const table = await sdk.tables.getTable(ctx.params.tableId) + let table = await sdk.tables.getTable(ctx.params.tableId) const { rows } = ctx.request.body const schema = table.schema @@ -88,7 +89,15 @@ export async function bulkImport( ctx.throw(400, "Provided data import information is invalid.") } - const parsedRows = parse(rows, schema) + const parsedRows = [] + for (const row of parse(rows, schema)) { + const processed = await inputProcessing(ctx.user?._id, table, row, { + noAutoRelationships: true, + }) + parsedRows.push(processed.row) + table = processed.table + } + await handleRequest(Operation.BULK_CREATE, table._id!, { rows: parsedRows, })