Allow setting multiple many-to-many relationships

This commit is contained in:
Adria Navarro 2024-11-05 11:24:23 +01:00
parent af2da7c6d8
commit 94d3466113
2 changed files with 14 additions and 17 deletions

View File

@ -65,7 +65,7 @@
let tableOptions let tableOptions
let errorChecker = new RelationshipErrorChecker( let errorChecker = new RelationshipErrorChecker(
invalidThroughTable, invalidThroughTable,
relationshipExists manyToManyRelationshipExistsFn
) )
let errors = {} let errors = {}
let fromPrimary, fromForeign, fromColumn, toColumn let fromPrimary, fromForeign, fromColumn, toColumn
@ -125,7 +125,7 @@
} }
return false return false
} }
function relationshipExists() { function manyToManyRelationshipExistsFn() {
if ( if (
originalFromTable && originalFromTable &&
originalToTable && originalToTable &&
@ -141,16 +141,14 @@
datasource.entities[getTable(toId).name].schema datasource.entities[getTable(toId).name].schema
).filter(value => value.through) ).filter(value => value.through)
const matchAgainstUserInput = (fromTableId, toTableId) => const matchAgainstUserInput = link =>
(fromTableId === fromId && toTableId === toId) || (link.throughTo === throughToKey &&
(fromTableId === toId && toTableId === fromId) link.throughFrom === throughFromKey) ||
(link.throughTo === throughFromKey && link.throughFrom === throughToKey)
return !!fromThroughLinks.find(from => const allLinks = [...fromThroughLinks, ...toThroughLinks]
toThroughLinks.find( return !!allLinks.find(
to => link => link.through === throughId && matchAgainstUserInput(link)
from.through === to.through &&
matchAgainstUserInput(from.tableId, to.tableId)
)
) )
} }
@ -181,16 +179,15 @@
relationshipType: errorChecker.relationshipTypeSet(relationshipType), relationshipType: errorChecker.relationshipTypeSet(relationshipType),
fromTable: fromTable:
errorChecker.tableSet(fromTable) || errorChecker.tableSet(fromTable) ||
errorChecker.doesRelationshipExists() ||
errorChecker.differentTables(fromId, toId, throughId), errorChecker.differentTables(fromId, toId, throughId),
toTable: toTable:
errorChecker.tableSet(toTable) || errorChecker.tableSet(toTable) ||
errorChecker.doesRelationshipExists() ||
errorChecker.differentTables(toId, fromId, throughId), errorChecker.differentTables(toId, fromId, throughId),
throughTable: throughTable:
errorChecker.throughTableSet(throughTable) || errorChecker.throughTableSet(throughTable) ||
errorChecker.throughIsNullable() || errorChecker.throughIsNullable() ||
errorChecker.differentTables(throughId, fromId, toId), errorChecker.differentTables(throughId, fromId, toId) ||
errorChecker.doesRelationshipExists(),
throughFromKey: throughFromKey:
errorChecker.manyForeignKeySet(throughFromKey) || errorChecker.manyForeignKeySet(throughFromKey) ||
errorChecker.manyTypeMismatch( errorChecker.manyTypeMismatch(

View File

@ -30,9 +30,9 @@ function typeMismatchCheck(fromTable, toTable, primary, foreign) {
} }
export class RelationshipErrorChecker { export class RelationshipErrorChecker {
constructor(invalidThroughTableFn, relationshipExistsFn) { constructor(invalidThroughTableFn, manyToManyRelationshipExistsFn) {
this.invalidThroughTable = invalidThroughTableFn this.invalidThroughTable = invalidThroughTableFn
this.relationshipExists = relationshipExistsFn this.manyToManyRelationshipExists = manyToManyRelationshipExistsFn
} }
setType(type) { setType(type) {
@ -72,7 +72,7 @@ export class RelationshipErrorChecker {
} }
doesRelationshipExists() { doesRelationshipExists() {
return this.isMany() && this.relationshipExists() return this.isMany() && this.manyToManyRelationshipExists()
? relationshipAlreadyExists ? relationshipAlreadyExists
: null : null
} }