Skip to content

Commit b00f774

Browse files
committed
N-ary relationships behave just like associative entities
1 parent 6cb732a commit b00f774

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/server/cypher/erToCypher.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,41 @@ const erToCypher = (er: string, strictMode = true): string => {
5252
}
5353

5454
for (const relationship of rel) {
55-
if (relationship.entities.length == 2) {
56-
schema += generateRelationship(relationship)
55+
if (relationship.entities.length == 2) { // Simple relationship, mapped to a Neo4j relationship.
56+
schema += generateRelationship(relationship) // This includes "generatePropertyExistenceConstraints".
57+
5758
}
58-
else if (relationship.entities.length > 2) {
59+
else if (relationship.entities.length > 2) { // Complex relationship, mapped to its own Neo4j node.
5960
schema += generatePropertyExistenceConstraints(relationship.id, relationship.attributes)
6061
schema += generateCompositeAttributeTriggers(relationship) // Not supported for relationships, but supported for nodes.
62+
63+
// Split associative entity into many relationships to reuse relationship trigger logic.
64+
for (const entity of relationship.entities) { // TODO: The logic is pretty much the same for associative entities, this deserves a refactoring.
65+
const rel: Rel = {
66+
id: getNameForNAryRelationship(relationship.id),
67+
entities: [
68+
{
69+
id: relationship.id,
70+
cardinality: "0,n",
71+
weak: false
72+
},
73+
{
74+
id: entity.id,
75+
cardinality: entity.cardinality,
76+
weak: false
77+
}
78+
],
79+
hasTimestamp: false,
80+
attributes: [],
81+
compositeAttributes: {},
82+
multivalued: {},
83+
pk: []
84+
}
85+
86+
schema += generateRelationship(rel, false)
87+
}
88+
89+
schema += generateAssociativeEntityRelationshipControl(relationship, true)
6190
}
6291

6392
schema += generateMultivaluedAttributeTriggers(relationship, true)

src/server/cypher/statements.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { indentation } from "../../shared/constants"
2-
import { generateTrigger, getNameForAEntRelationship, getTwoByTwoCombinations, hasNoAttributes } from "./helpers"
2+
import { generateTrigger, getNameForAEntRelationship, getNameForNAryRelationship, getTwoByTwoCombinations, hasNoAttributes } from "./helpers"
33
import { lower, normalize } from "../../shared/removeAccents"
44
import { getRelNameForCompAttribute, getEntNameForCompAttribute } from "../../client/utils/helpers"
55
import { generateRelationship } from "./relationships"
@@ -212,10 +212,10 @@ export const generateUnionTriggerForChildren = (union: Union): string => {
212212
return generateTrigger(`Union ${union.id} for children`, statement)
213213
}
214214

215-
export const generateAssociativeEntityRelationshipControl = (aent: AEnt): string => {
215+
export const generateAssociativeEntityRelationshipControl = (aent: AEnt | Rel, isNAryRelationship = false): string => {
216216
const { entities, id } = aent
217217

218-
const relName = getNameForAEntRelationship(id)
218+
const relName = isNAryRelationship ? getNameForNAryRelationship(id) : getNameForAEntRelationship(id)
219219

220220
let statement = `MATCH (n)-[:${relName}]-(:${lower(id)}) WHERE ` + "\n"
221221

0 commit comments

Comments
 (0)