law-common 10.18.2-beta.3 → 10.18.2-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { EntityEnum, EntityIndexMap,
|
|
1
|
+
import { EntityEnum, EntityIndexMap, VirtualEntityEnum } from "../interface/entity.utils.interface";
|
|
2
|
+
export declare enum RelationType {
|
|
3
|
+
ONE = "one",
|
|
4
|
+
MANY = "many"
|
|
5
|
+
}
|
|
2
6
|
export declare abstract class BaseEntityModel<T extends EntityEnum | VirtualEntityEnum> {
|
|
3
7
|
protected entityName: T;
|
|
4
8
|
constructor(entityName: T);
|
|
@@ -6,5 +10,5 @@ export declare abstract class BaseEntityModel<T extends EntityEnum | VirtualEnti
|
|
|
6
10
|
populateRelationsByIndex(entityIndexMap: EntityIndexMap): void;
|
|
7
11
|
static populateRelationsForEntities(entityIndexMap: EntityIndexMap, entityType: EntityEnum): void;
|
|
8
12
|
populateRelations(entityIndexMap: EntityIndexMap): void;
|
|
9
|
-
overwrite(entityIndexMap: EntityIndexMap, indexKey?:
|
|
13
|
+
overwrite(entityIndexMap: EntityIndexMap, indexKey?: string): this;
|
|
10
14
|
}
|
|
@@ -1,58 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseEntityModel = void 0;
|
|
4
|
-
|
|
3
|
+
exports.BaseEntityModel = exports.RelationType = void 0;
|
|
4
|
+
var RelationType;
|
|
5
|
+
(function (RelationType) {
|
|
6
|
+
RelationType["ONE"] = "one";
|
|
7
|
+
RelationType["MANY"] = "many";
|
|
8
|
+
})(RelationType || (exports.RelationType = RelationType = {}));
|
|
5
9
|
class BaseEntityModel {
|
|
6
10
|
constructor(entityName) {
|
|
7
11
|
this.entityName = entityName;
|
|
8
12
|
}
|
|
9
13
|
populateRelationsByIndex(entityIndexMap) {
|
|
10
14
|
for (const relationConfig of this.getRelationConfigs() || []) {
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
const thisMappingKey = relationConfig.mapKeyConfig.key;
|
|
16
|
+
const relatedMappingKey = relationConfig.mapKeyConfig.relationKey;
|
|
17
|
+
const thisKey = relationConfig.key;
|
|
18
|
+
// console.log('Populating relation for', relationConfig.name, 'on', this.entityName, relationConfig);
|
|
19
|
+
// debugger;
|
|
20
|
+
if (relationConfig.relation === RelationType.ONE) {
|
|
13
21
|
const relatedEntities = entityIndexMap[relationConfig.name] || {};
|
|
14
|
-
// @ts-ignore
|
|
15
22
|
let foundRelatedEntities = [];
|
|
16
|
-
if (
|
|
17
|
-
const [parentKey, childKey] =
|
|
18
|
-
|
|
19
|
-
if (this[parentKey] && this[parentKey][childKey]) {
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
foundRelatedEntities = [relatedEntities[this[parentKey][childKey]]];
|
|
22
|
-
}
|
|
23
|
+
if (thisMappingKey.includes(".")) {
|
|
24
|
+
const [parentKey, childKey] = thisMappingKey.split(".");
|
|
25
|
+
foundRelatedEntities = [relatedEntities[this[parentKey][childKey]]];
|
|
23
26
|
}
|
|
24
27
|
else {
|
|
25
|
-
foundRelatedEntities = relatedEntities[
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
// foundRelatedEntities = relatedEntities[this[thisKey]] ? [relatedEntities[this[thisKey]]] : [];
|
|
29
|
+
const relatedEntity = relatedEntities[this[thisMappingKey]];
|
|
30
|
+
if (relatedEntity) {
|
|
31
|
+
foundRelatedEntities = [relatedEntity];
|
|
32
|
+
}
|
|
33
|
+
else if (relatedMappingKey !== "id") {
|
|
34
|
+
foundRelatedEntities = Object.values(relatedEntities).filter((entity) => entity[relatedMappingKey] === this[thisMappingKey]);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
foundRelatedEntities = [];
|
|
38
|
+
}
|
|
31
39
|
}
|
|
32
40
|
if (foundRelatedEntities.length > 1) {
|
|
33
41
|
throw new Error(`Expected one related entity for ${relationConfig.name} but found ${foundRelatedEntities.length}`);
|
|
34
42
|
}
|
|
35
43
|
if (foundRelatedEntities.length === 0) {
|
|
36
|
-
console.warn(`No related entity found for ${relationConfig.name} with ${relationConfig.mapKeyConfig.
|
|
37
|
-
// @ts-ignore
|
|
38
|
-
}=${this[relationConfig.mapKeyConfig.foreignKey]}`);
|
|
44
|
+
console.warn(`No related entity found for ${relationConfig.name} with ${thisMappingKey}=${this[relationConfig.mapKeyConfig.foreignKey]}`);
|
|
39
45
|
}
|
|
40
|
-
|
|
41
|
-
this[relationConfig.key] = foundRelatedEntities[0];
|
|
46
|
+
this[thisKey] = foundRelatedEntities[0];
|
|
42
47
|
}
|
|
43
|
-
else if (relationConfig.relation ===
|
|
44
|
-
|
|
45
|
-
this[relationConfig.key] = [];
|
|
46
|
-
// @ts-ignore
|
|
48
|
+
else if (relationConfig.relation === RelationType.MANY) {
|
|
49
|
+
this[thisKey] = [];
|
|
47
50
|
const relatedEntities = entityIndexMap[relationConfig.name] || {};
|
|
48
51
|
for (const relatedEntity of Object.values(relatedEntities)) {
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
relatedEntity[relationConfig.mapKeyConfig.relationKey] ===
|
|
52
|
-
// @ts-ignore
|
|
53
|
-
this[relationConfig.mapKeyConfig.key]) {
|
|
54
|
-
// @ts-ignore
|
|
55
|
-
this[relationConfig.key].push(relatedEntity);
|
|
52
|
+
if (relatedEntity[relatedMappingKey] === this[thisMappingKey]) {
|
|
53
|
+
this[thisKey].push(relatedEntity);
|
|
56
54
|
}
|
|
57
55
|
}
|
|
58
56
|
}
|
|
@@ -60,7 +58,6 @@ class BaseEntityModel {
|
|
|
60
58
|
}
|
|
61
59
|
static populateRelationsForEntities(entityIndexMap, entityType) {
|
|
62
60
|
for (const key of Object.keys(entityIndexMap[entityType] || {})) {
|
|
63
|
-
// @ts-ignore
|
|
64
61
|
const entity = entityIndexMap[entityType][key];
|
|
65
62
|
entity.populateRelationsByIndex(entityIndexMap);
|
|
66
63
|
}
|
|
@@ -73,7 +70,6 @@ class BaseEntityModel {
|
|
|
73
70
|
console.warn(`Entity of type ${this.entityName} does not have ${indexKey}, cannot merge`);
|
|
74
71
|
return this;
|
|
75
72
|
}
|
|
76
|
-
// @ts-ignore
|
|
77
73
|
entityIndexMap[this.entityName][this[indexKey]] = this;
|
|
78
74
|
return this;
|
|
79
75
|
}
|