From a0e9abb95bff69074c729060b8c7b5000df1a6a5 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 11 Oct 2023 16:45:02 +0100 Subject: [PATCH] Responding to PR feedback. --- .../src/api/controllers/table/external.ts | 4 ++- .../server/src/api/controllers/table/index.ts | 11 ++++--- .../src/api/controllers/table/internal.ts | 4 ++- .../server/src/api/routes/tests/table.spec.ts | 29 +++++-------------- .../server/src/tests/utilities/api/row.ts | 15 ++++++++++ packages/types/src/api/web/app/rows.ts | 9 ++++++ 6 files changed, 43 insertions(+), 29 deletions(-) diff --git a/packages/server/src/api/controllers/table/external.ts b/packages/server/src/api/controllers/table/external.ts index 327904666d..a332247a26 100644 --- a/packages/server/src/api/controllers/table/external.ts +++ b/packages/server/src/api/controllers/table/external.ts @@ -18,6 +18,8 @@ import { AutoReason, Datasource, FieldSchema, + ImportRowsRequest, + ImportRowsResponse, Operation, QueryJson, RelationshipType, @@ -374,7 +376,7 @@ 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 diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index aa3f7cc702..69e1988def 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -6,9 +6,11 @@ import { validate as validateSchema, } from "../../../utilities/schema" import { isExternalTable, isSQL } from "../../../integrations/utils" -import { context, events } from "@budibase/backend-core" +import { events } from "@budibase/backend-core" import { FetchTablesResponse, + ImportRowsRequest, + ImportRowsResponse, SaveTableRequest, SaveTableResponse, Table, @@ -97,16 +99,13 @@ 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 - let db = context.getAppDB() let tableBefore = await sdk.tables.getTable(tableId) let tableAfter = await pickApi({ tableId }).bulkImport(ctx) if (!isEqual(tableBefore, tableAfter)) { - await db.put(tableAfter) - ctx.eventEmitter && - ctx.eventEmitter.emitTable(`table:save`, ctx.appId, tableAfter) + await sdk.tables.saveTable(tableAfter) } // 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..7765e630db 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -10,6 +10,8 @@ import { } from "../../../utilities/rowProcessor" import { runStaticFormulaChecks } from "./bulkFormula" import { + ImportRowsRequest, + ImportRowsResponse, RenameColumn, SaveTableRequest, SaveTableResponse, @@ -206,7 +208,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/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index 874b3535cd..12472eaa21 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -200,29 +200,16 @@ describe("/tables", () => { }, }) - let response = await request - .post(`/api/${table._id}/rows`) - .send({}) - .set(config.defaultHeaders()) - .expect(200) + let row = await config.api.row.save(table._id!, {}) + expect(row.autoId).toEqual(1) - expect(response.body.autoId).toEqual(1) + await config.api.row.bulkImport(table._id!, { + rows: [{ autoId: 2 }], + identifierFields: [], + }) - await request - .post(`/api/tables/${table._id}/import`) - .send({ - rows: [{ autoId: 2 }], - }) - .set(config.defaultHeaders()) - .expect(200) - - response = await request - .post(`/api/${table._id}/rows`) - .send({}) - .set(config.defaultHeaders()) - .expect(200) - - expect(response.body.autoId).toEqual(3) + row = await config.api.row.save(table._id!, {}) + expect(row.autoId).toEqual(3) }) }) diff --git a/packages/server/src/tests/utilities/api/row.ts b/packages/server/src/tests/utilities/api/row.ts index adeb96a593..ce95bf2835 100644 --- a/packages/server/src/tests/utilities/api/row.ts +++ b/packages/server/src/tests/utilities/api/row.ts @@ -4,6 +4,8 @@ import { Row, ValidateResponse, ExportRowsRequest, + ImportRowsRequest, + ImportRowsResponse, } from "@budibase/types" import TestConfiguration from "../TestConfiguration" import { TestAPI } from "./base" @@ -123,6 +125,19 @@ export class RowAPI extends TestAPI { return request } + bulkImport = async ( + tableId: string, + body: ImportRowsRequest, + { expectStatus } = { expectStatus: 200 } + ): Promise => { + let request = this.request + .post(`/api/tables/${tableId}/import`) + .send(body) + .set(this.config.defaultHeaders()) + .expect(expectStatus) + return (await request).body + } + search = async ( sourceId: string, { expectStatus } = { expectStatus: 200 } diff --git a/packages/types/src/api/web/app/rows.ts b/packages/types/src/api/web/app/rows.ts index 62ea90a6a4..d695075ac0 100644 --- a/packages/types/src/api/web/app/rows.ts +++ b/packages/types/src/api/web/app/rows.ts @@ -37,3 +37,12 @@ export interface ExportRowsRequest { } export type ExportRowsResponse = ReadStream + +export type ImportRowsRequest = { + rows: Row[] + identifierFields: string[] +} + +export type ImportRowsResponse = { + message: string +}