1
0
Fork 0
mirror of synced 2024-09-29 16:51:33 +13:00

Fixing issues with many to many relationships in SQL, sometimes not creating right relationships.

This commit is contained in:
mike12345567 2021-08-05 19:49:30 +01:00
parent 8981db7f4c
commit a64ce3f55a
2 changed files with 18 additions and 8 deletions

View file

@ -364,7 +364,7 @@ module External {
}
}
if (cache[fullKey] == null) {
cache[fullKey] = await makeExternalQuery(this.appId, {
const response = await makeExternalQuery(this.appId, {
endpoint: getEndpoint(tableId, DataSourceOperation.READ),
filters: {
equal: {
@ -372,8 +372,12 @@ module External {
},
},
})
// this is the response from knex if no rows found
if (!response[0].read) {
cache[fullKey] = response
}
}
return { rows: cache[fullKey], table }
return { rows: cache[fullKey] || [], table }
}
/**
@ -423,12 +427,16 @@ module External {
const { tableName } = breakExternalTableId(tableId)
const table = this.tables[tableName]
for (let row of rows) {
promises.push(
makeExternalQuery(this.appId, {
endpoint: getEndpoint(tableId, DataSourceOperation.DELETE),
filters: buildFilters(generateIdForRow(row, table), {}, table),
})
)
const filters = buildFilters(generateIdForRow(row, table), {}, table)
// safety check, if there are no filters on deletion bad things happen
if (Object.keys(filters).length !== 0) {
promises.push(
makeExternalQuery(this.appId, {
endpoint: getEndpoint(tableId, DataSourceOperation.DELETE),
filters,
})
)
}
}
}
await Promise.all(promises)

View file

@ -151,6 +151,8 @@ function buildRead(knex: Knex, json: QueryJson, limit: number): KnexQuery {
}
// handle select
if (resource.fields && resource.fields.length > 0) {
// select the resources as the format "table.columnName" - this is what is provided
// by the resource builder further up
query = query.select(resource.fields.map(field => `${field} as ${field}`))
} else {
query = query.select("*")