/
RelationMetadata.ts
549 lines (459 loc) · 22.7 KB
/
RelationMetadata.ts
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import {RelationType} from "./types/RelationTypes";
import {EntityMetadata} from "./EntityMetadata";
import {ForeignKeyMetadata} from "./ForeignKeyMetadata";
import {ObjectLiteral} from "../common/ObjectLiteral";
import {ColumnMetadata} from "./ColumnMetadata";
import {EmbeddedMetadata} from "./EmbeddedMetadata";
import {RelationMetadataArgs} from "../metadata-args/RelationMetadataArgs";
import {DeferrableType} from "./types/DeferrableType";
import {OnUpdateType} from "./types/OnUpdateType";
import {OnDeleteType} from "./types/OnDeleteType";
import {PropertyTypeFactory} from "./types/PropertyTypeInFunction";
/**
* Contains all information about some entity's relation.
*/
export class RelationMetadata {
// ---------------------------------------------------------------------
// Public Properties
// ---------------------------------------------------------------------
/**
* Entity metadata of the entity where this relation is placed.
*
* For example for @ManyToMany(type => Category) in Post, entityMetadata will be metadata of Post entity.
*/
entityMetadata: EntityMetadata;
/**
* Entity metadata of the entity that is targeted by this relation.
*
* For example for @ManyToMany(type => Category) in Post, inverseEntityMetadata will be metadata of Category entity.
*/
inverseEntityMetadata: EntityMetadata;
/**
* Entity metadata of the junction table.
* Junction tables have their own entity metadata objects.
* Defined only for many-to-many relations.
*/
junctionEntityMetadata?: EntityMetadata;
/**
* Embedded metadata where this relation is.
* If this relation is not in embed then this property value is undefined.
*/
embeddedMetadata?: EmbeddedMetadata;
/**
* Relation type, e.g. is it one-to-one, one-to-many, many-to-one or many-to-many.
*/
relationType: RelationType;
/**
* Target entity to which this relation is applied.
* Target IS NOT equal to entityMetadata.target, because relation
*
* For example for @ManyToMany(type => Category) in Post, target will be Post.
* If @ManyToMany(type => Category) is in Counters which is embedded into Post, target will be Counters.
* If @ManyToMany(type => Category) is in abstract class BaseUser which Post extends, target will be BaseUser.
* Target can be string if its defined in entity schema instead of class.
*/
target: Function|string;
/**
* Target's property name to which relation decorator is applied.
*/
propertyName: string;
/**
* Gets full path to this column property (including relation name).
* Full path is relevant when column is used in embeds (one or multiple nested).
* For example it will return "counters.subcounters.likes".
* If property is not in embeds then it returns just property name of the column.
*/
propertyPath: string;
/**
* Indicates if this is a parent (can be only many-to-one relation) relation in the tree tables.
*/
isTreeParent: boolean = false;
/**
* Indicates if this is a children (can be only one-to-many relation) relation in the tree tables.
*/
isTreeChildren: boolean = false;
/**
* Indicates if this relation's column is a primary key.
* Can be used only for many-to-one and owner one-to-one relations.
*/
isPrimary: boolean = false;
/**
* Indicates if this relation is lazily loaded.
*/
isLazy: boolean = false;
/**
* Indicates if this relation is eagerly loaded.
*/
isEager: boolean = false;
/**
* Indicates if persistence is enabled for the relation.
* By default its enabled, but if you want to avoid any changes in the relation to be reflected in the database you can disable it.
* If its disabled you can only change a relation from inverse side of a relation or using relation query builder functionality.
* This is useful for performance optimization since its disabling avoid multiple extra queries during entity save.
*/
persistenceEnabled: boolean = true;
/**
* If set to true then related objects are allowed to be inserted to the database.
*/
isCascadeInsert: boolean = false;
/**
* If set to true then related objects are allowed to be updated in the database.
*/
isCascadeUpdate: boolean = false;
/**
* If set to true then related objects are allowed to be remove from the database.
*/
isCascadeRemove: boolean = false;
/**
* If set to true then related objects are allowed to be soft-removed from the database.
*/
isCascadeSoftRemove: boolean = false;
/**
* If set to true then related objects are allowed to be recovered from the database.
*/
isCascadeRecover: boolean = false;
/**
* Indicates if relation column value can be nullable or not.
*/
isNullable: boolean = true;
/**
* What to do with a relation on deletion of the row containing a foreign key.
*/
onDelete?: OnDeleteType;
/**
* What to do with a relation on update of the row containing a foreign key.
*/
onUpdate?: OnUpdateType;
/**
* What to do with a relation on update of the row containing a foreign key.
*/
deferrable?: DeferrableType;
/**
* Gets the property's type to which this relation is applied.
*
* For example for @ManyToMany(type => Category) in Post, target will be Category.
*/
type: Function|string;
/**
* Indicates if this side is an owner of this relation.
*/
isOwning: boolean = false;
/**
* Checks if this relation's type is "one-to-one".
*/
isOneToOne: boolean = false;
/**
* Checks if this relation is owner side of the "one-to-one" relation.
* Owner side means this side of relation has a join column in the table.
*/
isOneToOneOwner: boolean = false;
/**
* Checks if this relation has a join column (e.g. is it many-to-one or one-to-one owner side).
*/
isWithJoinColumn: boolean = false;
/**
* Checks if this relation is NOT owner side of the "one-to-one" relation.
* NOT owner side means this side of relation does not have a join column in the table.
*/
isOneToOneNotOwner: boolean = false;
/**
* Checks if this relation's type is "one-to-many".
*/
isOneToMany: boolean = false;
/**
* Checks if this relation's type is "many-to-one".
*/
isManyToOne: boolean = false;
/**
* Checks if this relation's type is "many-to-many".
*/
isManyToMany: boolean = false;
/**
* Checks if this relation's type is "many-to-many", and is owner side of the relationship.
* Owner side means this side of relation has a join table.
*/
isManyToManyOwner: boolean = false;
/**
* Checks if this relation's type is "many-to-many", and is NOT owner side of the relationship.
* Not owner side means this side of relation does not have a join table.
*/
isManyToManyNotOwner: boolean = false;
/**
* Gets the property path of the inverse side of the relation.
*/
inverseSidePropertyPath: string;
/**
* Inverse side of the relation set by user.
*
* Inverse side set in the relation can be either string - property name of the column on inverse side,
* either can be a function that accepts a map of properties with the object and returns one of them.
* Second approach is used to achieve type-safety.
*/
givenInverseSidePropertyFactory: PropertyTypeFactory<any>;
/**
* Gets the relation metadata of the inverse side of this relation.
*/
inverseRelation?: RelationMetadata;
/**
* Join table name.
*/
joinTableName: string;
/**
* Foreign keys created for this relation.
*/
foreignKeys: ForeignKeyMetadata[] = [];
/**
* Join table columns.
* Join columns can be obtained only from owner side of the relation.
* From non-owner side of the relation join columns will be empty.
* If this relation is a many-to-one/one-to-one then it takes join columns from the current entity.
* If this relation is many-to-many then it takes all owner join columns from the junction entity.
*/
joinColumns: ColumnMetadata[] = [];
/**
* Inverse join table columns.
* Inverse join columns are supported only for many-to-many relations
* and can be obtained only from owner side of the relation.
* From non-owner side of the relation join columns will be undefined.
*/
inverseJoinColumns: ColumnMetadata[] = [];
// ---------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------
constructor(options: {
entityMetadata: EntityMetadata,
embeddedMetadata?: EmbeddedMetadata,
args: RelationMetadataArgs
}) {
this.entityMetadata = options.entityMetadata;
this.embeddedMetadata = options.embeddedMetadata!;
const args = options.args;
this.target = args.target;
this.propertyName = args.propertyName;
this.relationType = args.relationType;
if (args.inverseSideProperty)
this.givenInverseSidePropertyFactory = args.inverseSideProperty;
this.isLazy = args.isLazy || false;
// this.isCascadeInsert = args.options.cascade === true || (args.options.cascade instanceof Array && args.options.cascade.indexOf("insert") !== -1);
// this.isCascadeUpdate = args.options.cascade === true || (args.options.cascade instanceof Array && args.options.cascade.indexOf("update") !== -1);
// this.isCascadeRemove = args.options.cascade === true || (args.options.cascade instanceof Array && args.options.cascade.indexOf("remove") !== -1);
// this.isCascadeSoftRemove = args.options.cascade === true || (args.options.cascade instanceof Array && args.options.cascade.indexOf("soft-remove") !== -1);
// this.isCascadeRecover = args.options.cascade === true || (args.options.cascade instanceof Array && args.options.cascade.indexOf("recover") !== -1);
this.isCascadeInsert = args.options.cascade === true || (Array.isArray(args.options.cascade) && args.options.cascade.indexOf("insert") !== -1);
this.isCascadeUpdate = args.options.cascade === true || (Array.isArray(args.options.cascade) && args.options.cascade.indexOf("update") !== -1);
this.isCascadeRemove = args.options.cascade === true || (Array.isArray(args.options.cascade) && args.options.cascade.indexOf("remove") !== -1);
this.isCascadeSoftRemove = args.options.cascade === true || (Array.isArray(args.options.cascade) && args.options.cascade.indexOf("soft-remove") !== -1);
this.isCascadeRecover = args.options.cascade === true || (Array.isArray(args.options.cascade) && args.options.cascade.indexOf("recover") !== -1);
this.isPrimary = args.options.primary || false;
this.isNullable = args.options.nullable === false || this.isPrimary ? false : true;
this.onDelete = args.options.onDelete;
this.onUpdate = args.options.onUpdate;
this.deferrable = args.options.deferrable;
this.isEager = args.options.eager || false;
this.persistenceEnabled = args.options.persistence === false ? false : true;
this.isTreeParent = args.isTreeParent || false;
this.isTreeChildren = args.isTreeChildren || false;
this.type = args.type instanceof Function ? (args.type as () => any)() : args.type;
this.isOneToOne = this.relationType === "one-to-one";
this.isOneToMany = this.relationType === "one-to-many";
this.isManyToOne = this.relationType === "many-to-one";
this.isManyToMany = this.relationType === "many-to-many";
this.isOneToOneNotOwner = this.isOneToOne ? true : false;
this.isManyToManyNotOwner = this.isManyToMany ? true : false;
}
// ---------------------------------------------------------------------
// Public Methods
// ---------------------------------------------------------------------
/**
* Creates join column ids map from the given related entity ids array.
*/
getRelationIdMap(entity: ObjectLiteral): ObjectLiteral|undefined {
const joinColumns = this.isOwning ? this.joinColumns : this.inverseRelation!.joinColumns;
const referencedColumns = joinColumns.map(joinColumn => joinColumn.referencedColumn!);
// console.log("entity", entity);
// console.log("referencedColumns", referencedColumns);
return EntityMetadata.getValueMap(entity, referencedColumns);
}
/**
* Ensures that given object is an entity id map.
* If given id is an object then it means its already id map.
* If given id isn't an object then it means its a value of the id column
* and it creates a new id map with this value and name of the primary column.
*/
ensureRelationIdMap(id: any): ObjectLiteral {
if (id instanceof Object)
return id;
const joinColumns = this.isOwning ? this.joinColumns : this.inverseRelation!.joinColumns;
const referencedColumns = joinColumns.map(joinColumn => joinColumn.referencedColumn!);
if (referencedColumns.length > 1)
throw new Error(`Cannot create relation id map for a single value because relation contains multiple referenced columns.`);
return referencedColumns[0].createValueMap(id);
}
/**
* Extracts column value from the given entity.
* If column is in embedded (or recursive embedded) it extracts its value from there.
*/
getEntityValue(entity: ObjectLiteral, getLazyRelationsPromiseValue: boolean = false): any|undefined {
if (entity === null || entity === undefined) return undefined;
// extract column value from embeddeds of entity if column is in embedded
if (this.embeddedMetadata) {
// example: post[data][information][counters].id where "data", "information" and "counters" are embeddeds
// we need to get value of "id" column from the post real entity object
// first step - we extract all parent properties of the entity relative to this column, e.g. [data, information, counters]
const propertyNames = [...this.embeddedMetadata.parentPropertyNames];
// next we need to access post[data][information][counters][this.propertyName] to get column value from the counters
// this recursive function takes array of generated property names and gets the post[data][information][counters] embed
const extractEmbeddedColumnValue = (propertyNames: string[], value: ObjectLiteral): any => {
const propertyName = propertyNames.shift();
if (propertyName) {
if (value[propertyName]) {
return extractEmbeddedColumnValue(propertyNames, value[propertyName]);
}
return undefined;
}
return value;
};
// once we get nested embed object we get its column, e.g. post[data][information][counters][this.propertyName]
const embeddedObject = extractEmbeddedColumnValue(propertyNames, entity);
if (this.isLazy) {
if (embeddedObject["__" + this.propertyName + "__"] !== undefined)
return embeddedObject["__" + this.propertyName + "__"];
if (getLazyRelationsPromiseValue === true)
return embeddedObject[this.propertyName];
return undefined;
}
return embeddedObject ? embeddedObject[this.isLazy ? "__" + this.propertyName + "__" : this.propertyName] : undefined;
} else { // no embeds - no problems. Simply return column name by property name of the entity
if (this.isLazy) {
if (entity["__" + this.propertyName + "__"] !== undefined)
return entity["__" + this.propertyName + "__"];
if (getLazyRelationsPromiseValue === true)
return entity[this.propertyName];
return undefined;
}
return entity[this.propertyName];
}
}
/**
* Sets given entity's relation's value.
* Using of this method helps to set entity relation's value of the lazy and non-lazy relations.
*
* If merge is set to true, it merges given value into currently
*/
setEntityValue(entity: ObjectLiteral, value: any): void {
const propertyName = this.isLazy ? "__" + this.propertyName + "__" : this.propertyName;
if (this.embeddedMetadata) {
// first step - we extract all parent properties of the entity relative to this column, e.g. [data, information, counters]
const extractEmbeddedColumnValue = (embeddedMetadatas: EmbeddedMetadata[], map: ObjectLiteral): any => {
// if (!object[embeddedMetadata.propertyName])
// object[embeddedMetadata.propertyName] = embeddedMetadata.create();
const embeddedMetadata = embeddedMetadatas.shift();
if (embeddedMetadata) {
if (!map[embeddedMetadata.propertyName])
map[embeddedMetadata.propertyName] = embeddedMetadata.create();
extractEmbeddedColumnValue(embeddedMetadatas, map[embeddedMetadata.propertyName]);
return map;
}
map[propertyName] = value;
return map;
};
return extractEmbeddedColumnValue([...this.embeddedMetadata.embeddedMetadataTree], entity);
} else {
entity[propertyName] = value;
}
}
/**
* Creates entity id map from the given entity ids array.
*/
createValueMap(value: any) {
// extract column value from embeds of entity if column is in embedded
if (this.embeddedMetadata) {
// example: post[data][information][counters].id where "data", "information" and "counters" are embeddeds
// we need to get value of "id" column from the post real entity object and return it in a
// { data: { information: { counters: { id: ... } } } } format
// first step - we extract all parent properties of the entity relative to this column, e.g. [data, information, counters]
const propertyNames = [...this.embeddedMetadata.parentPropertyNames];
// now need to access post[data][information][counters] to get column value from the counters
// and on each step we need to create complex literal object, e.g. first { data },
// then { data: { information } }, then { data: { information: { counters } } },
// then { data: { information: { counters: [this.propertyName]: entity[data][information][counters][this.propertyName] } } }
// this recursive function helps doing that
const extractEmbeddedColumnValue = (propertyNames: string[], map: ObjectLiteral): any => {
const propertyName = propertyNames.shift();
if (propertyName) {
map[propertyName] = {};
extractEmbeddedColumnValue(propertyNames, map[propertyName]);
return map;
}
map[this.propertyName] = value;
return map;
};
return extractEmbeddedColumnValue(propertyNames, {});
} else { // no embeds - no problems. Simply return column property name and its value of the entity
return { [this.propertyName]: value };
}
}
// ---------------------------------------------------------------------
// Builder Methods
// ---------------------------------------------------------------------
/**
* Builds some depend relation metadata properties.
* This builder method should be used only after embedded metadata tree was build.
*/
build() {
this.propertyPath = this.buildPropertyPath();
}
/**
* Registers given foreign keys in the relation.
* This builder method should be used to register foreign key in the relation.
*/
registerForeignKeys(...foreignKeys: ForeignKeyMetadata[]) {
this.foreignKeys.push(...foreignKeys);
this.joinColumns = this.foreignKeys[0] ? this.foreignKeys[0].columns : [];
this.inverseJoinColumns = this.foreignKeys[1] ? this.foreignKeys[1].columns : [];
this.isOwning = this.isManyToOne || ((this.isManyToMany || this.isOneToOne) && this.joinColumns.length > 0);
this.isOneToOneOwner = this.isOneToOne && this.isOwning;
this.isOneToOneNotOwner = this.isOneToOne && !this.isOwning;
this.isManyToManyOwner = this.isManyToMany && this.isOwning;
this.isManyToManyNotOwner = this.isManyToMany && !this.isOwning;
this.isWithJoinColumn = this.isManyToOne || this.isOneToOneOwner;
}
/**
* Registers a given junction entity metadata.
* This builder method can be called after junction entity metadata for the many-to-many relation was created.
*/
registerJunctionEntityMetadata(junctionEntityMetadata: EntityMetadata) {
this.junctionEntityMetadata = junctionEntityMetadata;
this.joinTableName = junctionEntityMetadata.tableName;
if (this.inverseRelation) {
this.inverseRelation.junctionEntityMetadata = junctionEntityMetadata;
this.joinTableName = junctionEntityMetadata.tableName;
}
}
/**
* Builds inverse side property path based on given inverse side property factory.
* This builder method should be used only after properties map of the inverse entity metadata was build.
*/
buildInverseSidePropertyPath(): string {
if (this.givenInverseSidePropertyFactory) {
const ownerEntityPropertiesMap = this.inverseEntityMetadata.propertiesMap;
if (typeof this.givenInverseSidePropertyFactory === "function")
return this.givenInverseSidePropertyFactory(ownerEntityPropertiesMap);
if (typeof this.givenInverseSidePropertyFactory === "string")
return this.givenInverseSidePropertyFactory;
} else if (this.isTreeParent && this.entityMetadata.treeChildrenRelation) {
return this.entityMetadata.treeChildrenRelation.propertyName;
} else if (this.isTreeChildren && this.entityMetadata.treeParentRelation) {
return this.entityMetadata.treeParentRelation.propertyName;
}
return "";
}
/**
* Builds relation's property path based on its embedded tree.
*/
buildPropertyPath(): string {
if (!this.embeddedMetadata || !this.embeddedMetadata.parentPropertyNames.length)
return this.propertyName;
return this.embeddedMetadata.parentPropertyNames.join(".") + "." + this.propertyName;
}
}