Fixing PR comments.

This commit is contained in:
mike12345567 2023-10-19 12:02:49 +01:00
parent 9bd67595ec
commit 54d976f750
4 changed files with 25 additions and 35 deletions

View File

@ -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,

View File

@ -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]

View File

@ -31,16 +31,12 @@ export async function getAllInternalTables(db?: Database): Promise<Table[]> {
if (!db) {
db = context.getAppDB()
}
const internalTables = await db.allDocs(
const internalTables = await db.allDocs<Table[]>(
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<Table[]> {
@ -63,7 +59,7 @@ export async function getExternalTable(
return entities[tableName]
}
export async function getTable(tableId: any): Promise<Table> {
export async function getTable(tableId: string): Promise<Table> {
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(

View File

@ -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