-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathrelationship.ts
More file actions
204 lines (171 loc) · 7.47 KB
/
relationship.ts
File metadata and controls
204 lines (171 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { ForeignKeyActions } from "@/lib/field"
import { DatabaseType } from "@/lib/schemas/database-schema"
import { FieldType } from "@/lib/schemas/field-schema"
import { Cardinality, RelationshipType } from "@/lib/schemas/relationship-schema"
import { tablesRelations, TableType } from "@/lib/schemas/table-schema"
import { v4 } from "uuid"
import pluralize from 'pluralize';
export const getRelationshipSourceAndTarget = (sourceTableId: string, sourceField: FieldType, targetTableId: string, targetField: FieldType) => {
if (sourceField.isPrimary || targetField.isPrimary) {
if (sourceField.isPrimary) {
return {
sourceTableId: sourceTableId,
targetTableId: targetTableId,
sourceFieldId: sourceField.id,
targetFieldId: targetField.id,
}
} else {
return {
sourceTableId: targetTableId,
targetTableId: sourceTableId,
sourceFieldId: targetField.id,
targetFieldId: sourceField.id,
}
}
}
else if (sourceField.unique || targetField.unique) {
if (sourceField.unique) {
return {
sourceTableId: sourceTableId,
targetTableId: targetTableId,
sourceFieldId: sourceField.id,
targetFieldId: targetField.id,
}
} else {
return {
sourceTableId: targetTableId,
targetTableId: sourceTableId,
sourceFieldId: targetField.id,
targetFieldId: sourceField.id,
}
}
} else {
return {
sourceTableId: sourceTableId,
targetTableId: targetTableId,
sourceFieldId: sourceField.id,
targetFieldId: targetField.id,
}
}
}
export const getForeignRelationships = (table: TableType): RelationshipType[] => {
let foreignRelationships = (table.sourceRelations ? table.sourceRelations?.filter((relationship: RelationshipType) =>
relationship.cardinality == Cardinality.many_to_one
) : []).map((relationship: RelationshipType) => {
return {
...relationship,
sourceTable: relationship.targetTable,
targetTable: relationship.sourceTable,
sourceField: relationship.targetField,
targetField: relationship.sourceField,
}
});
foreignRelationships = foreignRelationships.concat(
table.targetRelations ? table.targetRelations?.filter((relationship: RelationshipType) =>
relationship.cardinality == Cardinality.one_to_many || relationship.cardinality == Cardinality.one_to_one
) : []);
return foreignRelationships;
}
export const getDefaultRelationshipName = (relationship: RelationshipType) => {
if (!relationship.sourceTable || !relationship.targetTable || !relationship.sourceField || !relationship.targetField)
return "";
return `fk_${relationship.sourceTable?.name}_${relationship.targetTable?.name}`
}
export const decomposeManyToMany = (database: DatabaseType): DatabaseType => {
// Clone the tables and relationships to avoid mutating the original database object
let relationships: RelationshipType[] = [...database.relationships];
let tables: TableType[] = [...database.tables];
// Filter out all many-to-many relationships to process
let manyToManyRelationships = relationships.filter(
(relationship: RelationshipType) => relationship.cardinality == Cardinality.many_to_many
);
// Loop through each many-to-many relationship
for (const relationship of manyToManyRelationships) {
// Find the source and target tables
const sourceTable: TableType = tables.find(
(table: TableType) => table.id == relationship.sourceTableId
) as TableType;
const targetTable: TableType = tables.find(
(table: TableType) => table.id == relationship.targetTableId
) as TableType;
// Find the source and target fields involved in the relationship
const sourceField: FieldType = sourceTable.fields.find(
(field: FieldType) => field.id == relationship.sourceFieldId
) as FieldType;
const targetField: FieldType = targetTable.fields.find(
(field: FieldType) => field.id == relationship.targetFieldId
) as FieldType;
// Convert table names to singular if they are plural
const singularSourceTableName: string = pluralize.isPlural(sourceTable.name)
? pluralize.singular(sourceTable.name)
: sourceTable.name;
const singularTargetTableName: string = pluralize.isPlural(targetTable.name)
? pluralize.singular(targetTable.name)
: targetTable.name;
// Create a foreign key field pointing to the source table
const junctionSourceField: FieldType = {
id: v4(),
name: `${singularSourceTableName}_${sourceField.name}`,
nullable: false,
typeId: sourceField.typeId,
type: sourceField.type,
isPrimary: true
} as FieldType;
// Create a foreign key field pointing to the target table
const junctionTargetField: FieldType = {
id: v4(),
name: `${singularTargetTableName}_${targetField.name}`,
nullable: false,
typeId: targetField.typeId,
type: targetField.type,
isPrimary: true
} as FieldType;
// Define the new junction table combining both foreign keys
const junctionTable: TableType = {
id: v4(),
name: `${singularSourceTableName}_${targetTable.name}`,
fields: [
junctionSourceField,
junctionTargetField
]
} as TableType;
// Create a one-to-many relationship from the source table to the junction table
const junctionSourceRelationship: RelationshipType = {
id: v4(),
sourceTable: sourceTable,
targetTable: junctionTable,
sourceTableId: sourceTable.id,
targetTableId: junctionTable.id,
sourceField: sourceField,
sourceFieldId: sourceField.id,
targetField: junctionSourceField,
targetFieldId: junctionSourceField.id,
cardinality: Cardinality.one_to_many,
onDelete: ForeignKeyActions.CASCADE
} as RelationshipType;
// Create a one-to-many relationship from the target table to the junction table
const junctionTargetRelationship: RelationshipType = {
id: v4(),
sourceTable: targetTable,
targetTable: junctionTable,
sourceTableId: targetTable.id,
targetTableId: junctionTable.id,
sourceField: targetField,
sourceFieldId: targetField.id,
targetField: junctionTargetField,
targetFieldId: junctionTargetField.id,
cardinality: Cardinality.one_to_many,
onDelete: ForeignKeyActions.CASCADE
} as RelationshipType;
// Add the new junction table and the two relationships to the database
tables.push(junctionTable);
relationships.push(junctionSourceRelationship);
relationships.push(junctionTargetRelationship);
}
// Return the updated database object with the new junction tables and relationships added
return {
...database,
tables,
relationships
};
}