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 789922e5a2
commit 5a0ef45c29
2 changed files with 18 additions and 8 deletions

View File

@ -364,7 +364,7 @@ module External {
} }
} }
if (cache[fullKey] == null) { if (cache[fullKey] == null) {
cache[fullKey] = await makeExternalQuery(this.appId, { const response = await makeExternalQuery(this.appId, {
endpoint: getEndpoint(tableId, DataSourceOperation.READ), endpoint: getEndpoint(tableId, DataSourceOperation.READ),
filters: { filters: {
equal: { 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 { tableName } = breakExternalTableId(tableId)
const table = this.tables[tableName] const table = this.tables[tableName]
for (let row of rows) { for (let row of rows) {
promises.push( const filters = buildFilters(generateIdForRow(row, table), {}, table)
makeExternalQuery(this.appId, { // safety check, if there are no filters on deletion bad things happen
endpoint: getEndpoint(tableId, DataSourceOperation.DELETE), if (Object.keys(filters).length !== 0) {
filters: buildFilters(generateIdForRow(row, table), {}, table), promises.push(
}) makeExternalQuery(this.appId, {
) endpoint: getEndpoint(tableId, DataSourceOperation.DELETE),
filters,
})
)
}
} }
} }
await Promise.all(promises) await Promise.all(promises)

View File

@ -151,6 +151,8 @@ function buildRead(knex: Knex, json: QueryJson, limit: number): KnexQuery {
} }
// handle select // handle select
if (resource.fields && resource.fields.length > 0) { 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}`)) query = query.select(resource.fields.map(field => `${field} as ${field}`))
} else { } else {
query = query.select("*") query = query.select("*")