diff --git a/packages/server/src/sdk/app/tables/external/index.ts b/packages/server/src/sdk/app/tables/external/index.ts index 1045ef1e33..402baada78 100644 --- a/packages/server/src/sdk/app/tables/external/index.ts +++ b/packages/server/src/sdk/app/tables/external/index.ts @@ -41,9 +41,10 @@ export async function save( ...update, } + const tableId = opts?.tableId || update._id let oldTable: Table | undefined - if (opts?.tableId) { - oldTable = await getTable(opts.tableId) + if (tableId) { + oldTable = await getTable(tableId) } if (hasTypeChanged(tableToSave, oldTable)) { @@ -114,6 +115,11 @@ export async function save( relatedTable, relationType ) + if (fkTable.schema[foreignKey] != null) { + throw new Error( + `Unable to generate foreign key - column ${foreignKey} already in use.` + ) + } fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey) if (fkTable.constrained == null) { fkTable.constrained = [] @@ -132,7 +138,7 @@ export async function save( cleanupRelationships(tableToSave, tables, oldTable) - const operation = oldTable ? Operation.UPDATE_TABLE : Operation.CREATE_TABLE + const operation = tableId ? Operation.UPDATE_TABLE : Operation.CREATE_TABLE await makeTableRequest( datasource, operation, diff --git a/packages/server/src/sdk/app/tables/external/utils.ts b/packages/server/src/sdk/app/tables/external/utils.ts index ef280472bf..10c755a7d6 100644 --- a/packages/server/src/sdk/app/tables/external/utils.ts +++ b/packages/server/src/sdk/app/tables/external/utils.ts @@ -52,7 +52,7 @@ export function cleanupRelationships( } } -export function otherRelationshipType(type?: string) { +export function otherRelationshipType(type: RelationshipType) { if (type === RelationshipType.MANY_TO_MANY) { return RelationshipType.MANY_TO_MANY } @@ -68,7 +68,10 @@ export function generateManyLinkSchema( relatedTable: Table ): Table { if (!table.primary || !relatedTable.primary) { - throw new Error("Unable to generate many link schema, no primary keys") + const noPrimaryName = !table.primary ? table.name : relatedTable.name + throw new Error( + `Unable to generate many link schema, "${noPrimaryName}" does not have a primary key` + ) } const primary = table.name + table.primary[0] const relatedPrimary = relatedTable.name + relatedTable.primary[0] diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 69b2354196..81467bba6c 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -31,16 +31,12 @@ export async function getAllInternalTables(db?: Database): Promise { if (!db) { db = context.getAppDB() } - const internalTables = await db.allDocs( + const internalTables = await db.allDocs( getTableParams(null, { include_docs: true, }) ) - return internalTables.rows.map((tableDoc: any) => ({ - ...tableDoc.doc, - type: "internal", - sourceId: tableDoc.doc.sourceId || BudibaseInternalDB._id, - })) + return processInternalTables(internalTables) } async function getAllExternalTables(): Promise { @@ -63,7 +59,7 @@ export async function getExternalTable( return entities[tableName] } -export async function getTable(tableId: any): Promise { +export async function getTable(tableId: string): Promise
{ const db = context.getAppDB() if (isExternalTable(tableId)) { let { datasourceId, tableName } = breakExternalTableId(tableId) @@ -80,7 +76,7 @@ export async function getAllTables() { getAllInternalTables(), getAllExternalTables(), ]) - return [...internal, external] + return [...internal, ...external] } export async function getExternalTablesInDatasource( diff --git a/packages/server/src/sdk/app/tables/internal/index.ts b/packages/server/src/sdk/app/tables/internal/index.ts index c5c7822ffe..4fb8dce8c9 100644 --- a/packages/server/src/sdk/app/tables/internal/index.ts +++ b/packages/server/src/sdk/app/tables/internal/index.ts @@ -58,17 +58,6 @@ export async function save( }) table = await tableSaveFunctions.before(table) - // make sure that types don't change of a column, have to remove - // the column if you want to change the type - if (oldTable && oldTable.schema) { - for (const propKey of Object.keys(table.schema)) { - let oldColumn = oldTable.schema[propKey] - if (oldColumn && oldColumn.type === FieldTypes.INTERNAL) { - oldTable.schema[propKey].type = FieldTypes.AUTO - } - } - } - let renaming = opts?.renaming if (renaming && renaming.old === renaming.updated) { renaming = undefined @@ -104,17 +93,13 @@ export async function save( } // update linked rows - try { - const linkResp: any = await updateLinks({ - eventType: oldTable ? EventType.TABLE_UPDATED : EventType.TABLE_SAVE, - table: table, - oldTable: oldTable, - }) - if (linkResp != null && linkResp._rev) { - table._rev = linkResp._rev - } - } catch (err) { - throw new Error(err as string) + const linkResp: any = await updateLinks({ + eventType: oldTable ? EventType.TABLE_UPDATED : EventType.TABLE_SAVE, + table: table, + oldTable: oldTable, + }) + if (linkResp != null && linkResp._rev) { + table._rev = linkResp._rev } // don't perform any updates until relationships have been