From a05ec1498f4e6d5b01b5657b6603b1bdb02da16d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 26 Jul 2024 13:27:51 +0200 Subject: [PATCH] Allow importing internal tables --- .../server/src/api/controllers/table/internal.ts | 3 +++ .../server/src/sdk/app/tables/internal/index.ts | 5 ++++- packages/shared-core/src/table.ts | 13 +++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/server/src/api/controllers/table/internal.ts b/packages/server/src/api/controllers/table/internal.ts index a06cc4dee3..4286d51d3e 100644 --- a/packages/server/src/api/controllers/table/internal.ts +++ b/packages/server/src/api/controllers/table/internal.ts @@ -25,6 +25,8 @@ export async function save( sourceType: rest.sourceType || TableSourceType.INTERNAL, } + const isImport = !!rows + if (!tableToSave.views) { tableToSave.views = {} } @@ -35,6 +37,7 @@ export async function save( rowsToImport: rows, tableId: ctx.request.body._id, renaming, + isImport, }) return table diff --git a/packages/server/src/sdk/app/tables/internal/index.ts b/packages/server/src/sdk/app/tables/internal/index.ts index fc32708708..504b73e496 100644 --- a/packages/server/src/sdk/app/tables/internal/index.ts +++ b/packages/server/src/sdk/app/tables/internal/index.ts @@ -31,6 +31,7 @@ export async function save( tableId?: string rowsToImport?: Row[] renaming?: RenameColumn + isImport: boolean } ) { const db = context.getAppDB() @@ -47,7 +48,9 @@ export async function save( } // check for case sensitivity - we don't want to allow duplicated columns - const duplicateColumn = findDuplicateInternalColumns(table) + const duplicateColumn = findDuplicateInternalColumns(table, { + internalRowsAllowed: !!opts?.isImport, + }) if (duplicateColumn.length) { throw new Error( `Column(s) "${duplicateColumn.join( diff --git a/packages/shared-core/src/table.ts b/packages/shared-core/src/table.ts index 8fd7909b18..da0df370bb 100644 --- a/packages/shared-core/src/table.ts +++ b/packages/shared-core/src/table.ts @@ -53,7 +53,10 @@ export function canBeSortColumn(type: FieldType): boolean { return !!allowSortColumnByType[type] } -export function findDuplicateInternalColumns(table: Table): string[] { +export function findDuplicateInternalColumns( + table: Table, + opts?: { internalRowsAllowed: boolean } +): string[] { // maintains the case of keys const casedKeys = Object.keys(table.schema) // get the column names @@ -69,9 +72,11 @@ export function findDuplicateInternalColumns(table: Table): string[] { } } } - for (let internalColumn of CONSTANT_INTERNAL_ROW_COLS) { - if (casedKeys.find(key => key === internalColumn)) { - duplicates.push(internalColumn) + if (!opts?.internalRowsAllowed) { + for (let internalColumn of CONSTANT_INTERNAL_ROW_COLS) { + if (casedKeys.find(key => key === internalColumn)) { + duplicates.push(internalColumn) + } } } return duplicates