Responding to PR feedback.

This commit is contained in:
Sam Rose 2023-10-11 16:45:02 +01:00
parent 0d9f257cc2
commit a0e9abb95b
6 changed files with 43 additions and 29 deletions

View File

@ -18,6 +18,8 @@ import {
AutoReason, AutoReason,
Datasource, Datasource,
FieldSchema, FieldSchema,
ImportRowsRequest,
ImportRowsResponse,
Operation, Operation,
QueryJson, QueryJson,
RelationshipType, RelationshipType,
@ -374,7 +376,7 @@ export async function destroy(ctx: UserCtx) {
return tableToDelete return tableToDelete
} }
export async function bulkImport(ctx: UserCtx) { export async function bulkImport(ctx: UserCtx<ImportRowsRequest, ImportRowsResponse>) {
const table = await sdk.tables.getTable(ctx.params.tableId) const table = await sdk.tables.getTable(ctx.params.tableId)
const { rows }: { rows: unknown } = ctx.request.body const { rows }: { rows: unknown } = ctx.request.body
const schema: unknown = table.schema const schema: unknown = table.schema

View File

@ -6,9 +6,11 @@ import {
validate as validateSchema, validate as validateSchema,
} from "../../../utilities/schema" } from "../../../utilities/schema"
import { isExternalTable, isSQL } from "../../../integrations/utils" import { isExternalTable, isSQL } from "../../../integrations/utils"
import { context, events } from "@budibase/backend-core" import { events } from "@budibase/backend-core"
import { import {
FetchTablesResponse, FetchTablesResponse,
ImportRowsRequest,
ImportRowsResponse,
SaveTableRequest, SaveTableRequest,
SaveTableResponse, SaveTableResponse,
Table, Table,
@ -97,16 +99,13 @@ export async function destroy(ctx: UserCtx) {
builderSocket?.emitTableDeletion(ctx, deletedTable) builderSocket?.emitTableDeletion(ctx, deletedTable)
} }
export async function bulkImport(ctx: UserCtx) { export async function bulkImport(ctx: UserCtx<ImportRowsRequest, ImportRowsResponse>) {
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
let db = context.getAppDB()
let tableBefore = await sdk.tables.getTable(tableId) let tableBefore = await sdk.tables.getTable(tableId)
let tableAfter = await pickApi({ tableId }).bulkImport(ctx) let tableAfter = await pickApi({ tableId }).bulkImport(ctx)
if (!isEqual(tableBefore, tableAfter)) { if (!isEqual(tableBefore, tableAfter)) {
await db.put(tableAfter) await sdk.tables.saveTable(tableAfter)
ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:save`, ctx.appId, tableAfter)
} }
// right now we don't trigger anything for bulk import because it // right now we don't trigger anything for bulk import because it

View File

@ -10,6 +10,8 @@ import {
} from "../../../utilities/rowProcessor" } from "../../../utilities/rowProcessor"
import { runStaticFormulaChecks } from "./bulkFormula" import { runStaticFormulaChecks } from "./bulkFormula"
import { import {
ImportRowsRequest,
ImportRowsResponse,
RenameColumn, RenameColumn,
SaveTableRequest, SaveTableRequest,
SaveTableResponse, SaveTableResponse,
@ -206,7 +208,7 @@ export async function destroy(ctx: any) {
return tableToDelete return tableToDelete
} }
export async function bulkImport(ctx: any) { export async function bulkImport(ctx: UserCtx<ImportRowsRequest, ImportRowsResponse>) {
const table = await sdk.tables.getTable(ctx.params.tableId) const table = await sdk.tables.getTable(ctx.params.tableId)
const { rows, identifierFields } = ctx.request.body const { rows, identifierFields } = ctx.request.body
await handleDataImport(ctx.user, table, rows, identifierFields) await handleDataImport(ctx.user, table, rows, identifierFields)

View File

@ -200,29 +200,16 @@ describe("/tables", () => {
}, },
}) })
let response = await request let row = await config.api.row.save(table._id!, {})
.post(`/api/${table._id}/rows`) expect(row.autoId).toEqual(1)
.send({})
.set(config.defaultHeaders())
.expect(200)
expect(response.body.autoId).toEqual(1) await config.api.row.bulkImport(table._id!, {
await request
.post(`/api/tables/${table._id}/import`)
.send({
rows: [{ autoId: 2 }], rows: [{ autoId: 2 }],
identifierFields: [],
}) })
.set(config.defaultHeaders())
.expect(200)
response = await request row = await config.api.row.save(table._id!, {})
.post(`/api/${table._id}/rows`) expect(row.autoId).toEqual(3)
.send({})
.set(config.defaultHeaders())
.expect(200)
expect(response.body.autoId).toEqual(3)
}) })
}) })

View File

@ -4,6 +4,8 @@ import {
Row, Row,
ValidateResponse, ValidateResponse,
ExportRowsRequest, ExportRowsRequest,
ImportRowsRequest,
ImportRowsResponse,
} from "@budibase/types" } from "@budibase/types"
import TestConfiguration from "../TestConfiguration" import TestConfiguration from "../TestConfiguration"
import { TestAPI } from "./base" import { TestAPI } from "./base"
@ -123,6 +125,19 @@ export class RowAPI extends TestAPI {
return request return request
} }
bulkImport = async (
tableId: string,
body: ImportRowsRequest,
{ expectStatus } = { expectStatus: 200 }
): Promise<ImportRowsResponse> => {
let request = this.request
.post(`/api/tables/${tableId}/import`)
.send(body)
.set(this.config.defaultHeaders())
.expect(expectStatus)
return (await request).body
}
search = async ( search = async (
sourceId: string, sourceId: string,
{ expectStatus } = { expectStatus: 200 } { expectStatus } = { expectStatus: 200 }

View File

@ -37,3 +37,12 @@ export interface ExportRowsRequest {
} }
export type ExportRowsResponse = ReadStream export type ExportRowsResponse = ReadStream
export type ImportRowsRequest = {
rows: Row[]
identifierFields: string[]
}
export type ImportRowsResponse = {
message: string
}