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,
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<ImportRowsRequest, ImportRowsResponse>) {
const table = await sdk.tables.getTable(ctx.params.tableId)
const { rows }: { rows: unknown } = ctx.request.body
const schema: unknown = table.schema

View File

@ -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<ImportRowsRequest, ImportRowsResponse>) {
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

View File

@ -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<ImportRowsRequest, ImportRowsResponse>) {
const table = await sdk.tables.getTable(ctx.params.tableId)
const { rows, identifierFields } = ctx.request.body
await handleDataImport(ctx.user, table, rows, identifierFields)

View File

@ -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 request
.post(`/api/tables/${table._id}/import`)
.send({
await config.api.row.bulkImport(table._id!, {
rows: [{ autoId: 2 }],
identifierFields: [],
})
.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)
})
})

View File

@ -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<ImportRowsResponse> => {
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 }

View File

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