From 71159e4f4a42531a62d23c8f554db5315ebcf535 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 10 Oct 2023 12:58:51 +0200 Subject: [PATCH] Import internal --- .../server/src/api/controllers/table/external.ts | 8 ++++---- .../server/src/api/controllers/table/index.ts | 3 ++- .../server/src/api/controllers/table/internal.ts | 3 ++- .../server/src/api/controllers/table/utils.ts | 16 +++++++++++----- packages/types/src/api/web/app/table.ts | 5 +++++ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index 327904666d..8917bcaab1 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -15,7 +15,7 @@ import { handleRequest } from "../row/external" import { context, events } from "@budibase/backend-core" import { isRows, isSchema, parse } from "../../../utilities/schema" import { - AutoReason, + BulkImportRequest, Datasource, FieldSchema, Operation, @@ -374,10 +374,10 @@ export async function destroy(ctx: UserCtx) { return tableToDelete } -export async function bulkImport(ctx: UserCtx) { +export async function bulkImport(ctx: UserCtx) { const table = await sdk.tables.getTable(ctx.params.tableId) - const { rows }: { rows: unknown } = ctx.request.body - const schema: unknown = table.schema + const { rows } = ctx.request.body + const schema = table.schema if (!rows || !isRows(rows) || !isSchema(schema)) { ctx.throw(400, "Provided data import information is invalid.") diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index e7c6ae57b0..e1956e33c0 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -8,6 +8,7 @@ import { import { isExternalTable, isSQL } from "../../../integrations/utils" import { events } from "@budibase/backend-core" import { + BulkImportRequest, FetchTablesResponse, SaveTableRequest, SaveTableResponse, @@ -97,7 +98,7 @@ export async function destroy(ctx: UserCtx) { builderSocket?.emitTableDeletion(ctx, deletedTable) } -export async function bulkImport(ctx: UserCtx) { +export async function bulkImport(ctx: UserCtx) { const tableId = ctx.params.tableId await pickApi({ tableId }).bulkImport(ctx) // right now we don't trigger anything for bulk import because it diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index e468848c57..999ea35d37 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -10,6 +10,7 @@ import { } from "../../../utilities/rowProcessor" import { runStaticFormulaChecks } from "./bulkFormula" import { + BulkImportRequest, RenameColumn, SaveTableRequest, SaveTableResponse, @@ -206,7 +207,7 @@ export async function destroy(ctx: any) { return tableToDelete } -export async function bulkImport(ctx: any) { +export async function bulkImport(ctx: UserCtx) { const table = await sdk.tables.getTable(ctx.params.tableId) const { rows, identifierFields } = ctx.request.body await handleDataImport(ctx.user, table, rows, identifierFields) diff --git a/packages/server/src/api/controllers/table/utils.ts b/packages/server/src/api/controllers/table/utils.ts index e0d564db2a..bf64bc32ab 100644 --- a/packages/server/src/api/controllers/table/utils.ts +++ b/packages/server/src/api/controllers/table/utils.ts @@ -20,7 +20,13 @@ import viewTemplate from "../view/viewBuilder" import { cloneDeep } from "lodash/fp" import { quotas } from "@budibase/pro" import { events, context } from "@budibase/backend-core" -import { ContextUser, Datasource, SourceName, Table } from "@budibase/types" +import { + ContextUser, + Datasource, + Row, + SourceName, + Table, +} from "@budibase/types" export async function clearColumns(table: any, columnNames: any) { const db = context.getAppDB() @@ -144,12 +150,12 @@ export async function importToRows( } export async function handleDataImport( - user: any, - table: any, - rows: any, + user: ContextUser, + table: Table, + rows: Row[], identifierFields: Array = [] ) { - const schema: unknown = table.schema + const schema = table.schema if (!rows || !isRows(rows) || !isSchema(schema)) { return table diff --git a/packages/types/src/api/web/app/table.ts b/packages/types/src/api/web/app/table.ts index 0885dac72a..8fb0297a9e 100644 --- a/packages/types/src/api/web/app/table.ts +++ b/packages/types/src/api/web/app/table.ts @@ -24,3 +24,8 @@ export interface SaveTableRequest extends TableRequest { } export type SaveTableResponse = Table + +export interface BulkImportRequest { + rows: Row[] + identifierFields?: Array +}