law-common 10.18.2-beta.2 → 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,8 +1,14 @@
1
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);
5
9
  abstract getRelationConfigs(): any[];
6
10
  populateRelationsByIndex(entityIndexMap: EntityIndexMap): void;
7
11
  static populateRelationsForEntities(entityIndexMap: EntityIndexMap, entityType: EntityEnum): void;
12
+ populateRelations(entityIndexMap: EntityIndexMap): void;
13
+ overwrite(entityIndexMap: EntityIndexMap, indexKey?: string): this;
8
14
  }
@@ -1,58 +1,56 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseEntityModel = void 0;
4
- const relation_type_enum_1 = require("../enums/relation-type.enum");
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
- if (relationConfig.relation === relation_type_enum_1.RelationType.ONE) {
12
- // @ts-ignore
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 (relationConfig.mapKeyConfig.key.includes(".")) {
17
- const [parentKey, childKey] = relationConfig.mapKeyConfig.key.split(".");
18
- // @ts-ignore
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
- // @ts-ignore
27
- this[relationConfig.mapKeyConfig.key]]
28
- ? // @ts-ignore
29
- [relatedEntities[this[relationConfig.mapKeyConfig.key]]]
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.key
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
- // @ts-ignore
41
- this[relationConfig.key] = foundRelatedEntities[0];
46
+ this[thisKey] = foundRelatedEntities[0];
42
47
  }
43
- else if (relationConfig.relation === relation_type_enum_1.RelationType.MANY) {
44
- // @ts-ignore
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
- // @ts-ignore
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,10 +58,20 @@ 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
  }
67
64
  }
65
+ populateRelations(entityIndexMap) {
66
+ this.populateRelationsByIndex(entityIndexMap);
67
+ }
68
+ overwrite(entityIndexMap, indexKey = "id") {
69
+ if (this[indexKey] === undefined) {
70
+ console.warn(`Entity of type ${this.entityName} does not have ${indexKey}, cannot merge`);
71
+ return this;
72
+ }
73
+ entityIndexMap[this.entityName][this[indexKey]] = this;
74
+ return this;
75
+ }
68
76
  }
69
77
  exports.BaseEntityModel = BaseEntityModel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "law-common",
3
- "version": "10.18.2-beta.2",
3
+ "version": "10.18.2-beta.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "files": [