Merge pull request #10162 from Budibase/bug/budi-6782-internal-table-all-row-ids-for
Bug - BUDI-6782 - Internal table all row ids for
This commit is contained in:
commit
7d794f005e
|
@ -0,0 +1,97 @@
|
||||||
|
import { FieldType } from "@budibase/types"
|
||||||
|
import { AutoFieldSubTypes } from "../../../../constants"
|
||||||
|
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
|
||||||
|
import { importToRows } from "../utils"
|
||||||
|
|
||||||
|
describe("utils", () => {
|
||||||
|
const config = new TestConfiguration()
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await config.init()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(config.end)
|
||||||
|
|
||||||
|
describe("importToRows", () => {
|
||||||
|
it("consecutive row have consecutive auto ids", async () => {
|
||||||
|
await config.doInContext(config.appId, async () => {
|
||||||
|
const table = await config.createTable({
|
||||||
|
name: "table",
|
||||||
|
type: "table",
|
||||||
|
schema: {
|
||||||
|
autoId: {
|
||||||
|
name: "autoId",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
subtype: AutoFieldSubTypes.AUTO_ID,
|
||||||
|
autocolumn: true,
|
||||||
|
constraints: {
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
constraints: {
|
||||||
|
type: FieldType.STRING,
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = [{ name: "Alice" }, { name: "Bob" }, { name: "Claire" }]
|
||||||
|
|
||||||
|
const result = importToRows(data, table, config.user)
|
||||||
|
expect(result).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
autoId: 1,
|
||||||
|
name: "Alice",
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
autoId: 2,
|
||||||
|
name: "Bob",
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
autoId: 3,
|
||||||
|
name: "Claire",
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can import data without a specific user performing the action", async () => {
|
||||||
|
await config.doInContext(config.appId, async () => {
|
||||||
|
const table = await config.createTable({
|
||||||
|
name: "table",
|
||||||
|
type: "table",
|
||||||
|
schema: {
|
||||||
|
autoId: {
|
||||||
|
name: "autoId",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
subtype: AutoFieldSubTypes.AUTO_ID,
|
||||||
|
autocolumn: true,
|
||||||
|
constraints: {
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
constraints: {
|
||||||
|
type: FieldType.STRING,
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = [{ name: "Alice" }, { name: "Bob" }, { name: "Claire" }]
|
||||||
|
|
||||||
|
const result = importToRows(data, table)
|
||||||
|
expect(result).toHaveLength(3)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -20,7 +20,13 @@ import viewTemplate from "../view/viewBuilder"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { quotas } from "@budibase/pro"
|
import { quotas } from "@budibase/pro"
|
||||||
import { events, context } from "@budibase/backend-core"
|
import { events, context } from "@budibase/backend-core"
|
||||||
import { Database, Datasource, SourceName, Table } from "@budibase/types"
|
import {
|
||||||
|
ContextUser,
|
||||||
|
Database,
|
||||||
|
Datasource,
|
||||||
|
SourceName,
|
||||||
|
Table,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
export async function clearColumns(table: any, columnNames: any) {
|
export async function clearColumns(table: any, columnNames: any) {
|
||||||
const db: Database = context.getAppDB()
|
const db: Database = context.getAppDB()
|
||||||
|
@ -99,32 +105,35 @@ export function makeSureTableUpToDate(table: any, tableToSave: any) {
|
||||||
return tableToSave
|
return tableToSave
|
||||||
}
|
}
|
||||||
|
|
||||||
export function importToRows(data: any, table: any, user: any = {}) {
|
export function importToRows(
|
||||||
|
data: any[],
|
||||||
|
table: Table,
|
||||||
|
user: ContextUser | null = null
|
||||||
|
) {
|
||||||
let finalData: any = []
|
let finalData: any = []
|
||||||
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 = generateRowID(table._id)
|
row._id = generateRowID(table._id!)
|
||||||
row.tableId = table._id
|
row.tableId = table._id
|
||||||
const processed: any = inputProcessing(user, table, row, {
|
const processed = inputProcessing(user, table, row, {
|
||||||
noAutoRelationships: true,
|
noAutoRelationships: true,
|
||||||
})
|
})
|
||||||
row = processed.row
|
row = processed.row
|
||||||
|
table = processed.table
|
||||||
|
|
||||||
let fieldName: any
|
for (const [fieldName, schema] of Object.entries(table.schema)) {
|
||||||
let schema: any
|
|
||||||
for ([fieldName, schema] of Object.entries(table.schema)) {
|
|
||||||
// check whether the options need to be updated for inclusion as part of the data import
|
// check whether the options need to be updated for inclusion as part of the data import
|
||||||
if (
|
if (
|
||||||
schema.type === FieldTypes.OPTIONS &&
|
schema.type === FieldTypes.OPTIONS &&
|
||||||
row[fieldName] &&
|
row[fieldName] &&
|
||||||
(!schema.constraints.inclusion ||
|
(!schema.constraints!.inclusion ||
|
||||||
schema.constraints.inclusion.indexOf(row[fieldName]) === -1)
|
schema.constraints!.inclusion.indexOf(row[fieldName]) === -1)
|
||||||
) {
|
) {
|
||||||
schema.constraints.inclusion = [
|
schema.constraints!.inclusion = [
|
||||||
...schema.constraints.inclusion,
|
...schema.constraints!.inclusion!,
|
||||||
row[fieldName],
|
row[fieldName],
|
||||||
]
|
]
|
||||||
schema.constraints.inclusion.sort()
|
schema.constraints!.inclusion.sort()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ function syncLastIds(table: Table, rowCount: number) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function tableImport(table: Table, data: Row) {
|
function tableImport(table: Table, data: Row[]) {
|
||||||
const cloneTable = cloneDeep(table)
|
const cloneTable = cloneDeep(table)
|
||||||
const rowDocs = importToRows(data, cloneTable)
|
const rowDocs = importToRows(data, cloneTable)
|
||||||
syncLastIds(cloneTable, rowDocs.length)
|
syncLastIds(cloneTable, rowDocs.length)
|
||||||
|
|
|
@ -131,7 +131,7 @@ export function coerce(row: any, type: string) {
|
||||||
* @returns {object} the row which has been prepared to be written to the DB.
|
* @returns {object} the row which has been prepared to be written to the DB.
|
||||||
*/
|
*/
|
||||||
export function inputProcessing(
|
export function inputProcessing(
|
||||||
user: ContextUser,
|
user: ContextUser | null,
|
||||||
table: Table,
|
table: Table,
|
||||||
row: Row,
|
row: Row,
|
||||||
opts?: AutoColumnProcessingOpts
|
opts?: AutoColumnProcessingOpts
|
||||||
|
|
Loading…
Reference in New Issue