Support no updating existing rows
This commit is contained in:
parent
f794f84e90
commit
a6beb0fa82
|
@ -122,11 +122,12 @@ export function makeSureTableUpToDate(table: Table, tableToSave: Table) {
|
||||||
export async function importToRows(
|
export async function importToRows(
|
||||||
data: Row[],
|
data: Row[],
|
||||||
table: Table,
|
table: Table,
|
||||||
user?: ContextUser
|
user?: ContextUser,
|
||||||
|
opts?: { keepCouchId: boolean }
|
||||||
) {
|
) {
|
||||||
const originalTable = table
|
const originalTable = table
|
||||||
const finalData: Row[] = []
|
const finalData: Row[] = []
|
||||||
const keepCouchId = "_id" in table.schema
|
const keepCouchId = !!opts?.keepCouchId
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
let row = data[i]
|
let row = data[i]
|
||||||
row._id = (keepCouchId && row._id) || generateRowID(table._id!)
|
row._id = (keepCouchId && row._id) || generateRowID(table._id!)
|
||||||
|
@ -181,7 +182,9 @@ export async function handleDataImport(
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const data = parse(importRows, table)
|
const data = parse(importRows, table)
|
||||||
|
|
||||||
const finalData = await importToRows(data, table, user)
|
const finalData = await importToRows(data, table, user, {
|
||||||
|
keepCouchId: identifierFields.includes("_id"),
|
||||||
|
})
|
||||||
|
|
||||||
let newRowCount = finalData.length
|
let newRowCount = finalData.length
|
||||||
|
|
||||||
|
|
|
@ -1351,6 +1351,60 @@ describe.each([
|
||||||
await assertRowUsage(rowUsage + 2)
|
await assertRowUsage(rowUsage + 2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
isInternal &&
|
||||||
|
it("should create new rows if not identifierFields are provided", async () => {
|
||||||
|
const table = await config.api.table.save(
|
||||||
|
saveTableRequest({
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
type: FieldType.STRING,
|
||||||
|
name: "name",
|
||||||
|
},
|
||||||
|
description: {
|
||||||
|
type: FieldType.STRING,
|
||||||
|
name: "description",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const existingRow = await config.api.row.save(table._id!, {
|
||||||
|
name: "Existing row",
|
||||||
|
description: "Existing description",
|
||||||
|
})
|
||||||
|
|
||||||
|
const rowUsage = await getRowUsage()
|
||||||
|
|
||||||
|
await config.api.row.bulkImport(table._id!, {
|
||||||
|
rows: [
|
||||||
|
{
|
||||||
|
name: "Row 1",
|
||||||
|
description: "Row 1 description",
|
||||||
|
},
|
||||||
|
{ ...existingRow, name: "Updated existing row" },
|
||||||
|
{
|
||||||
|
name: "Row 2",
|
||||||
|
description: "Row 2 description",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
const rows = await config.api.row.fetch(table._id!)
|
||||||
|
expect(rows.length).toEqual(4)
|
||||||
|
|
||||||
|
rows.sort((a, b) => a.name.localeCompare(b.name))
|
||||||
|
expect(rows[0].name).toEqual("Existing row")
|
||||||
|
expect(rows[0].description).toEqual("Existing description")
|
||||||
|
expect(rows[1].name).toEqual("Row 1")
|
||||||
|
expect(rows[1].description).toEqual("Row 1 description")
|
||||||
|
expect(rows[2].name).toEqual("Row 2")
|
||||||
|
expect(rows[2].description).toEqual("Row 2 description")
|
||||||
|
expect(rows[3].name).toEqual("Updated existing row")
|
||||||
|
expect(rows[3].description).toEqual("Existing description")
|
||||||
|
|
||||||
|
await assertRowUsage(rowUsage + 3)
|
||||||
|
})
|
||||||
|
|
||||||
// Upserting isn't yet supported in MSSQL, see:
|
// Upserting isn't yet supported in MSSQL, see:
|
||||||
// https://github.com/knex/knex/pull/6050
|
// https://github.com/knex/knex/pull/6050
|
||||||
!isMSSQL &&
|
!isMSSQL &&
|
||||||
|
|
Loading…
Reference in New Issue