@itrocks/lazy-loading 0.0.4 → 0.0.6

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.
@@ -23,7 +23,7 @@ function defineCollectionProperty(type, property, builtClass) {
23
23
  set(value) {
24
24
  delete this[property + '_ids'];
25
25
  Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
26
- Reflect.deleteMetadata(exports.PROTECT_GET, builtClass.prototype, property);
26
+ Reflect.defineMetadata(exports.PROTECT_GET, false, this, property);
27
27
  }
28
28
  };
29
29
  Object.defineProperty(builtClass.prototype, property, descriptor);
@@ -41,7 +41,7 @@ function defineObjectProperty(type, property, builtClass) {
41
41
  set(value) {
42
42
  delete this[property + '_id'];
43
43
  Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
44
- Reflect.deleteMetadata(exports.PROTECT_GET, builtClass.prototype, property);
44
+ Reflect.defineMetadata(exports.PROTECT_GET, false, this, property);
45
45
  }
46
46
  };
47
47
  Object.defineProperty(builtClass.prototype, property, descriptor);
@@ -62,7 +62,13 @@ function initClass(classType) {
62
62
  constructor(...args) {
63
63
  super(...args);
64
64
  for (const property of properties) {
65
- delete this[property];
65
+ const value = Object.getOwnPropertyDescriptor(this, property)?.value;
66
+ if ((value === undefined) || (Array.isArray(value) && !value.length)) {
67
+ delete this[property];
68
+ }
69
+ else {
70
+ Reflect.defineMetadata(exports.PROTECT_GET, false, this, property);
71
+ }
66
72
  }
67
73
  }
68
74
  })();
@@ -85,15 +91,25 @@ function initLazyLoading() {
85
91
  Module.prototype.require = function () {
86
92
  const original = superRequire.call(this, ...arguments);
87
93
  let module;
94
+ let replacements;
88
95
  for (const [name, type] of Object.entries(original)) {
89
96
  if (!(0, class_type_1.isAnyType)(type))
90
97
  continue;
98
+ if (module && replacements) {
99
+ const replacement = replacements.get(type);
100
+ if (replacement) {
101
+ module[name] = replacement;
102
+ continue;
103
+ }
104
+ }
91
105
  const withORM = initClass(type);
92
106
  if (!withORM)
93
107
  continue;
94
- if (!module) {
108
+ if (!replacements) {
95
109
  module = { ...original };
110
+ replacements = new Map();
96
111
  }
112
+ replacements.set(type, withORM);
97
113
  // @ts-ignore TS18048 but module is always initialized
98
114
  module[name] = withORM;
99
115
  }
@@ -20,7 +20,7 @@ function defineCollectionProperty(type, property, builtClass) {
20
20
  set(value) {
21
21
  delete this[property + '_ids'];
22
22
  Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
23
- Reflect.deleteMetadata(PROTECT_GET, builtClass.prototype, property);
23
+ Reflect.defineMetadata(PROTECT_GET, false, this, property);
24
24
  }
25
25
  };
26
26
  Object.defineProperty(builtClass.prototype, property, descriptor);
@@ -38,7 +38,7 @@ function defineObjectProperty(type, property, builtClass) {
38
38
  set(value) {
39
39
  delete this[property + '_id'];
40
40
  Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
41
- Reflect.deleteMetadata(PROTECT_GET, builtClass.prototype, property);
41
+ Reflect.defineMetadata(PROTECT_GET, false, this, property);
42
42
  }
43
43
  };
44
44
  Object.defineProperty(builtClass.prototype, property, descriptor);
@@ -59,7 +59,13 @@ export function initClass(classType) {
59
59
  constructor(...args) {
60
60
  super(...args);
61
61
  for (const property of properties) {
62
- delete this[property];
62
+ const value = Object.getOwnPropertyDescriptor(this, property)?.value;
63
+ if ((value === undefined) || (Array.isArray(value) && !value.length)) {
64
+ delete this[property];
65
+ }
66
+ else {
67
+ Reflect.defineMetadata(PROTECT_GET, false, this, property);
68
+ }
63
69
  }
64
70
  }
65
71
  })();
@@ -82,15 +88,25 @@ export function initLazyLoading() {
82
88
  Module.prototype.require = function () {
83
89
  const original = superRequire.call(this, ...arguments);
84
90
  let module;
91
+ let replacements;
85
92
  for (const [name, type] of Object.entries(original)) {
86
93
  if (!isAnyType(type))
87
94
  continue;
95
+ if (module && replacements) {
96
+ const replacement = replacements.get(type);
97
+ if (replacement) {
98
+ module[name] = replacement;
99
+ continue;
100
+ }
101
+ }
88
102
  const withORM = initClass(type);
89
103
  if (!withORM)
90
104
  continue;
91
- if (!module) {
105
+ if (!replacements) {
92
106
  module = { ...original };
107
+ replacements = new Map();
93
108
  }
109
+ replacements.set(type, withORM);
94
110
  // @ts-ignore TS18048 but module is always initialized
95
111
  module[name] = withORM;
96
112
  }
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  "build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
55
55
  },
56
56
  "types": "./esm/lazy-loading.d.ts",
57
- "version": "0.0.4"
57
+ "version": "0.0.6"
58
58
  }