Fix many to many
This commit is contained in:
parent
5efcf97c0d
commit
96be447817
|
@ -425,8 +425,12 @@ export class ExternalRequest<T extends Operation> {
|
||||||
*/
|
*/
|
||||||
async lookupRelations(tableId: string, row: Row) {
|
async lookupRelations(tableId: string, row: Row) {
|
||||||
const related: {
|
const related: {
|
||||||
[key: string]: { rows: Row[]; isMany: boolean; tableId: string }
|
rows: Row[]
|
||||||
} = {}
|
isMany: boolean
|
||||||
|
tableId: string
|
||||||
|
field: string
|
||||||
|
}[] = []
|
||||||
|
|
||||||
const { tableName } = breakExternalTableId(tableId)
|
const { tableName } = breakExternalTableId(tableId)
|
||||||
const table = this.tables[tableName]
|
const table = this.tables[tableName]
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -489,11 +493,13 @@ export class ExternalRequest<T extends Operation> {
|
||||||
const storeTo = isManyToMany(field)
|
const storeTo = isManyToMany(field)
|
||||||
? field.throughFrom || linkPrimaryKey
|
? field.throughFrom || linkPrimaryKey
|
||||||
: fieldName
|
: fieldName
|
||||||
related[storeTo] = {
|
|
||||||
|
related.push({
|
||||||
rows,
|
rows,
|
||||||
isMany: isManyToMany(field),
|
isMany: isManyToMany(field),
|
||||||
tableId: relatedTableId,
|
tableId: relatedTableId,
|
||||||
}
|
field: storeTo,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return related
|
return related
|
||||||
}
|
}
|
||||||
|
@ -528,7 +534,9 @@ export class ExternalRequest<T extends Operation> {
|
||||||
|
|
||||||
const linkSecondary = relationshipPrimary[1]
|
const linkSecondary = relationshipPrimary[1]
|
||||||
|
|
||||||
const rows = related[key]?.rows || []
|
const rows =
|
||||||
|
related.find(r => r.tableId === relationship.tableId && r.field === key)
|
||||||
|
?.rows || []
|
||||||
|
|
||||||
const relationshipMatchPredicate = ({
|
const relationshipMatchPredicate = ({
|
||||||
row,
|
row,
|
||||||
|
@ -573,12 +581,12 @@ export class ExternalRequest<T extends Operation> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// finally cleanup anything that needs to be removed
|
// finally cleanup anything that needs to be removed
|
||||||
for (let [colName, { isMany, rows, tableId }] of Object.entries(related)) {
|
for (let { isMany, rows, tableId, field } of related) {
|
||||||
const table: Table | undefined = this.getTable(tableId)
|
const table: Table | undefined = this.getTable(tableId)
|
||||||
// if it's not the foreign key skip it, nothing to do
|
// if it's not the foreign key skip it, nothing to do
|
||||||
if (
|
if (
|
||||||
!table ||
|
!table ||
|
||||||
(!isMany && table.primary && table.primary.indexOf(colName) !== -1)
|
(!isMany && table.primary && table.primary.indexOf(field) !== -1)
|
||||||
) {
|
) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -586,7 +594,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
const rowId = generateIdForRow(row, table)
|
const rowId = generateIdForRow(row, table)
|
||||||
const promise: Promise<any> = isMany
|
const promise: Promise<any> = isMany
|
||||||
? this.removeManyToManyRelationships(rowId, table)
|
? this.removeManyToManyRelationships(rowId, table)
|
||||||
: this.removeOneToManyRelationships(rowId, table, colName)
|
: this.removeOneToManyRelationships(rowId, table, field)
|
||||||
if (promise) {
|
if (promise) {
|
||||||
promises.push(promise)
|
promises.push(promise)
|
||||||
}
|
}
|
||||||
|
@ -603,7 +611,9 @@ export class ExternalRequest<T extends Operation> {
|
||||||
if (!isManyToOne(relationshipColumn)) {
|
if (!isManyToOne(relationshipColumn)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const { rows, isMany, tableId } = related[relationshipColumn.fieldName]
|
const { rows, isMany, tableId } = related.find(
|
||||||
|
r => r.field === relationshipColumn.fieldName
|
||||||
|
)!
|
||||||
const table = this.getTable(tableId)!
|
const table = this.getTable(tableId)!
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
rows.map(row => {
|
rows.map(row => {
|
||||||
|
|
Loading…
Reference in New Issue