Further typing.

This commit is contained in:
mike12345567 2024-02-26 18:16:42 +00:00
parent ad1964f47f
commit 1bb375a500
2 changed files with 41 additions and 31 deletions

View File

@ -1,4 +1,3 @@
#!/bin/bash #!/bin/bash
docker-compose down docker-compose down -v
docker volume prune -f docker volume prune -f
docker volume rm postgres_pg_data

View File

@ -19,6 +19,7 @@ import {
SortJson, SortJson,
SortType, SortType,
Table, Table,
isManyToOne,
} from "@budibase/types" } from "@budibase/types"
import { import {
breakExternalTableId, breakExternalTableId,
@ -104,23 +105,36 @@ function buildFilters(
} }
} }
function removeRelationships( async function removeManyToManyRelationships(
rowId: string, rowId: string,
table: Table, table: Table,
isManyToMany: boolean, colName: string
colName?: string
) { ) {
const tableId = table._id! const tableId = table._id!
const filters = buildFilters(rowId, {}, table) const filters = buildFilters(rowId, {}, table)
// safety check, if there are no filters on deletion bad things happen // safety check, if there are no filters on deletion bad things happen
if (Object.keys(filters).length !== 0) { if (Object.keys(filters).length !== 0) {
const op = isManyToMany ? Operation.DELETE : Operation.UPDATE
const body = colName && !isManyToMany ? { [colName]: null } : undefined
return getDatasourceAndQuery({ return getDatasourceAndQuery({
endpoint: getEndpoint(tableId, op), endpoint: getEndpoint(tableId, Operation.DELETE),
body, body: { [colName]: null },
filters, filters,
}) })
} else {
return []
}
}
async function removeOneToManyRelationships(rowId: string, table: Table) {
const tableId = table._id!
const filters = buildFilters(rowId, {}, table)
// safety check, if there are no filters on deletion bad things happen
if (Object.keys(filters).length !== 0) {
return getDatasourceAndQuery({
endpoint: getEndpoint(tableId, Operation.UPDATE),
filters,
})
} else {
return []
} }
} }
@ -734,12 +748,10 @@ export class ExternalRequest<T extends Operation> {
continue continue
} }
for (let row of rows) { for (let row of rows) {
const promise = removeRelationships( const rowId = generateIdForRow(row, table)
generateIdForRow(row, table), const promise: Promise<any> = isMany
table, ? removeManyToManyRelationships(rowId, table, colName)
isMany, : removeOneToManyRelationships(rowId, table)
colName
)
if (promise) { if (promise) {
promises.push(promise) promises.push(promise)
} }
@ -752,24 +764,23 @@ export class ExternalRequest<T extends Operation> {
const row = await this.getRow(table, rowId) const row = await this.getRow(table, rowId)
const related = await this.lookupRelations(table._id!, row) const related = await this.lookupRelations(table._id!, row)
for (let column of Object.values(table.schema)) { for (let column of Object.values(table.schema)) {
if ( const relationshipColumn = column as RelationshipFieldMetadata
column.type !== FieldType.LINK || if (!isManyToOne(relationshipColumn)) {
column.relationshipType === RelationshipType.ONE_TO_MANY
) {
continue continue
} }
const relationshipColumn = column as ManyToOneRelationshipFieldMetadata
const { rows, isMany, tableId } = related[relationshipColumn.fieldName] const { rows, isMany, tableId } = related[relationshipColumn.fieldName]
const table = this.getTable(tableId)! const table = this.getTable(tableId)!
await Promise.all( await Promise.all(
rows.map(row => rows.map(row => {
removeRelationships( const rowId = generateIdForRow(row, table)
generateIdForRow(row, table), return isMany
table, ? removeManyToManyRelationships(
isMany, rowId,
relationshipColumn.fieldName table,
) relationshipColumn.fieldName
) )
: removeOneToManyRelationships(rowId, table)
})
) )
} }
} }
@ -892,11 +903,11 @@ export class ExternalRequest<T extends Operation> {
// aliasing can be disabled fully if desired // aliasing can be disabled fully if desired
let response let response
if (!env.SQL_ALIASING_DISABLE) { if (env.SQL_ALIASING_DISABLE) {
response = await getDatasourceAndQuery(json)
} else {
const aliasing = new AliasTables(Object.keys(this.tables)) const aliasing = new AliasTables(Object.keys(this.tables))
response = await aliasing.queryWithAliasing(json) response = await aliasing.queryWithAliasing(json)
} else {
response = await getDatasourceAndQuery(json)
} }
const responseRows = Array.isArray(response) ? response : [] const responseRows = Array.isArray(response) ? response : []