diff --git a/packages/server/src/api/controllers/table/tests/utils.spec.ts b/packages/server/src/api/controllers/table/tests/utils.spec.ts index ef143100a4..957cf51ecd 100644 --- a/packages/server/src/api/controllers/table/tests/utils.spec.ts +++ b/packages/server/src/api/controllers/table/tests/utils.spec.ts @@ -59,5 +59,39 @@ describe("utils", () => { ]) }) }) + + 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) + }) + }) }) }) diff --git a/packages/server/src/api/controllers/table/utils.ts b/packages/server/src/api/controllers/table/utils.ts index 337b8fc5ed..cf1883c083 100644 --- a/packages/server/src/api/controllers/table/utils.ts +++ b/packages/server/src/api/controllers/table/utils.ts @@ -105,7 +105,11 @@ export function makeSureTableUpToDate(table: any, tableToSave: any) { return tableToSave } -export function importToRows(data: any[], table: Table, user: ContextUser) { +export function importToRows( + data: any[], + table: Table, + user: ContextUser | null = null +) { let finalData: any = [] for (let i = 0; i < data.length; i++) { let row = data[i] diff --git a/packages/server/src/db/defaultData/datasource_bb_default.ts b/packages/server/src/db/defaultData/datasource_bb_default.ts index 516783ed31..6ffbf601c8 100644 --- a/packages/server/src/db/defaultData/datasource_bb_default.ts +++ b/packages/server/src/db/defaultData/datasource_bb_default.ts @@ -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 rowDocs = importToRows(data, cloneTable) syncLastIds(cloneTable, rowDocs.length) diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 9e1029fa1a..a5bb352eeb 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -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. */ export function inputProcessing( - user: ContextUser, + user: ContextUser | null, table: Table, row: Row, opts?: AutoColumnProcessingOpts