Fixing an issue with removing relationships from the many side of a table in SQL, this was not correctly updating the other table.

This commit is contained in:
mike12345567 2024-10-09 15:09:38 +01:00
parent d29de7c2ad
commit d62d5b7043
2 changed files with 29 additions and 18 deletions

View File

@ -325,14 +325,18 @@ class InternalBuilder {
return input return input
} }
private parseBody(body: any) { private parseBody(body: Record<string, any>) {
for (let [key, value] of Object.entries(body)) { try {
const { column } = this.splitter.run(key) for (let [key, value] of Object.entries(body)) {
const schema = this.table.schema[column] const { column } = this.splitter.run(key)
if (!schema) { const schema = this.table.schema[column]
continue if (!schema) {
continue
}
body[key] = this.parse(value, schema)
} }
body[key] = this.parse(value, schema) } catch (err) {
console.log(err)
} }
return body return body
} }
@ -1259,6 +1263,10 @@ class InternalBuilder {
create(opts: QueryOptions): Knex.QueryBuilder { create(opts: QueryOptions): Knex.QueryBuilder {
const { body } = this.query const { body } = this.query
if (!body) {
throw new Error("Cannot create without row body")
}
let query = this.qualifiedKnex({ alias: false }) let query = this.qualifiedKnex({ alias: false })
const parsedBody = this.parseBody(body) const parsedBody = this.parseBody(body)
@ -1417,6 +1425,9 @@ class InternalBuilder {
update(opts: QueryOptions): Knex.QueryBuilder { update(opts: QueryOptions): Knex.QueryBuilder {
const { body, filters } = this.query const { body, filters } = this.query
if (!body) {
throw new Error("Cannot update without row body")
}
let query = this.qualifiedKnex() let query = this.qualifiedKnex()
const parsedBody = this.parseBody(body) const parsedBody = this.parseBody(body)
query = this.addFilters(query, filters) query = this.addFilters(query, filters)

View File

@ -269,18 +269,13 @@ export class ExternalRequest<T extends Operation> {
} }
} }
private async removeManyToManyRelationships( private async removeManyToManyRelationships(rowId: string, table: Table) {
rowId: string,
table: Table,
colName: string
) {
const tableId = table._id! const tableId = table._id!
const filters = this.prepareFilters(rowId, {}, table) const filters = this.prepareFilters(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) {
return getDatasourceAndQuery({ return getDatasourceAndQuery({
endpoint: getEndpoint(tableId, Operation.DELETE), endpoint: getEndpoint(tableId, Operation.DELETE),
body: { [colName]: null },
filters, filters,
meta: { meta: {
table, table,
@ -291,13 +286,18 @@ export class ExternalRequest<T extends Operation> {
} }
} }
private async removeOneToManyRelationships(rowId: string, table: Table) { private async removeOneToManyRelationships(
rowId: string,
table: Table,
colName: string
) {
const tableId = table._id! const tableId = table._id!
const filters = this.prepareFilters(rowId, {}, table) const filters = this.prepareFilters(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) {
return getDatasourceAndQuery({ return getDatasourceAndQuery({
endpoint: getEndpoint(tableId, Operation.UPDATE), endpoint: getEndpoint(tableId, Operation.UPDATE),
body: { [colName]: null },
filters, filters,
meta: { meta: {
table, table,
@ -595,8 +595,8 @@ export class ExternalRequest<T extends Operation> {
for (let row of rows) { for (let row of rows) {
const rowId = generateIdForRow(row, table) const rowId = generateIdForRow(row, table)
const promise: Promise<any> = isMany const promise: Promise<any> = isMany
? this.removeManyToManyRelationships(rowId, table, colName) ? this.removeManyToManyRelationships(rowId, table)
: this.removeOneToManyRelationships(rowId, table) : this.removeOneToManyRelationships(rowId, table, colName)
if (promise) { if (promise) {
promises.push(promise) promises.push(promise)
} }
@ -619,12 +619,12 @@ export class ExternalRequest<T extends Operation> {
rows.map(row => { rows.map(row => {
const rowId = generateIdForRow(row, table) const rowId = generateIdForRow(row, table)
return isMany return isMany
? this.removeManyToManyRelationships( ? this.removeManyToManyRelationships(rowId, table)
: this.removeOneToManyRelationships(
rowId, rowId,
table, table,
relationshipColumn.fieldName relationshipColumn.fieldName
) )
: this.removeOneToManyRelationships(rowId, table)
}) })
) )
} }