Simplifying lookups
This commit is contained in:
parent
8aeb19aabe
commit
95de01c86c
|
@ -17,6 +17,7 @@ import {
|
||||||
PaginationJson,
|
PaginationJson,
|
||||||
QueryJson,
|
QueryJson,
|
||||||
RelationshipFieldMetadata,
|
RelationshipFieldMetadata,
|
||||||
|
RelationshipType,
|
||||||
Row,
|
Row,
|
||||||
SearchFilters,
|
SearchFilters,
|
||||||
SortJson,
|
SortJson,
|
||||||
|
@ -421,17 +422,30 @@ export class ExternalRequest<T extends Operation> {
|
||||||
return { row: newRow as T, manyRelationships }
|
return { row: newRow as T, manyRelationships }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getLookupRelationsKey(relationship: {
|
||||||
|
relationshipType: RelationshipType
|
||||||
|
fieldName: string
|
||||||
|
through?: string
|
||||||
|
}) {
|
||||||
|
if (relationship.relationshipType === RelationshipType.MANY_TO_MANY) {
|
||||||
|
return `${relationship.through}_${relationship.fieldName}`
|
||||||
|
}
|
||||||
|
return relationship.fieldName
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* This is a cached lookup, of relationship records, this is mainly for creating/deleting junction
|
* This is a cached lookup, of relationship records, this is mainly for creating/deleting junction
|
||||||
* information.
|
* information.
|
||||||
*/
|
*/
|
||||||
async lookupRelations(tableId: string, row: Row) {
|
private async lookupRelations(tableId: string, row: Row) {
|
||||||
const related: {
|
const related: Record<
|
||||||
rows: Row[]
|
string,
|
||||||
isMany: boolean
|
{
|
||||||
tableId: string
|
rows: Row[]
|
||||||
field: string
|
isMany: boolean
|
||||||
}[] = []
|
tableId: string
|
||||||
|
field: string
|
||||||
|
}
|
||||||
|
> = {}
|
||||||
|
|
||||||
const { tableName } = breakExternalTableId(tableId)
|
const { tableName } = breakExternalTableId(tableId)
|
||||||
const table = this.tables[tableName]
|
const table = this.tables[tableName]
|
||||||
|
@ -496,12 +510,12 @@ export class ExternalRequest<T extends Operation> {
|
||||||
? field.throughFrom || linkPrimaryKey
|
? field.throughFrom || linkPrimaryKey
|
||||||
: fieldName
|
: fieldName
|
||||||
|
|
||||||
related.push({
|
related[this.getLookupRelationsKey(field)] = {
|
||||||
rows,
|
rows,
|
||||||
isMany: isManyToMany(field),
|
isMany: isManyToMany(field),
|
||||||
tableId: relatedTableId,
|
tableId: relatedTableId,
|
||||||
field: storeTo,
|
field: storeTo,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
return related
|
return related
|
||||||
}
|
}
|
||||||
|
@ -537,8 +551,13 @@ export class ExternalRequest<T extends Operation> {
|
||||||
const linkSecondary = relationshipPrimary[1]
|
const linkSecondary = relationshipPrimary[1]
|
||||||
|
|
||||||
const rows =
|
const rows =
|
||||||
related.find(r => r.tableId === relationship.tableId && r.field === key)
|
related[
|
||||||
?.rows || []
|
this.getLookupRelationsKey({
|
||||||
|
relationshipType: RelationshipType.MANY_TO_MANY,
|
||||||
|
fieldName: key,
|
||||||
|
through: relationship.tableId,
|
||||||
|
})
|
||||||
|
]?.rows || []
|
||||||
|
|
||||||
const relationshipMatchPredicate = ({
|
const relationshipMatchPredicate = ({
|
||||||
row,
|
row,
|
||||||
|
@ -583,7 +602,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// finally cleanup anything that needs to be removed
|
// finally cleanup anything that needs to be removed
|
||||||
for (let { isMany, rows, tableId, field } of related) {
|
for (let { isMany, rows, tableId, field } of Object.values(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 (
|
||||||
|
@ -613,11 +632,7 @@ export class ExternalRequest<T extends Operation> {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const relatedByTable = isManyToMany(column)
|
const relatedByTable = related[this.getLookupRelationsKey(column)]
|
||||||
? related.find(
|
|
||||||
r => r.tableId === column.through && r.field === column.fieldName
|
|
||||||
)
|
|
||||||
: related.find(r => r.field === column.fieldName)
|
|
||||||
if (!relatedByTable) {
|
if (!relatedByTable) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue