@woltz/rich-domain 1.7.0 → 1.8.0

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.
Files changed (204) hide show
  1. package/dist/cjs/core/aggregate-changes.d.ts +206 -0
  2. package/dist/cjs/core/aggregate-changes.d.ts.map +1 -0
  3. package/dist/cjs/core/aggregate-changes.js +365 -0
  4. package/dist/cjs/core/aggregate-changes.js.map +1 -0
  5. package/dist/cjs/core/base-entity.d.ts +88 -0
  6. package/dist/cjs/core/base-entity.d.ts.map +1 -0
  7. package/dist/cjs/core/base-entity.js +329 -0
  8. package/dist/cjs/core/base-entity.js.map +1 -0
  9. package/dist/cjs/core/change-tracker.d.ts +96 -0
  10. package/dist/cjs/core/change-tracker.d.ts.map +1 -0
  11. package/dist/cjs/core/change-tracker.js +789 -0
  12. package/dist/cjs/core/change-tracker.js.map +1 -0
  13. package/dist/cjs/core/domain-event.d.ts +23 -0
  14. package/dist/cjs/core/domain-event.d.ts.map +1 -0
  15. package/dist/cjs/core/domain-event.js +36 -0
  16. package/dist/cjs/core/domain-event.js.map +1 -0
  17. package/dist/cjs/core/entity-changes.d.ts +84 -0
  18. package/dist/cjs/core/entity-changes.d.ts.map +1 -0
  19. package/dist/cjs/core/entity-changes.js +136 -0
  20. package/dist/cjs/core/entity-changes.js.map +1 -0
  21. package/dist/cjs/core/entity.d.ts +7 -0
  22. package/dist/cjs/core/entity.d.ts.map +1 -0
  23. package/dist/cjs/core/entity.js +11 -0
  24. package/dist/cjs/core/entity.js.map +1 -0
  25. package/dist/cjs/core/id.d.ts +59 -0
  26. package/dist/cjs/core/id.d.ts.map +1 -0
  27. package/dist/cjs/core/id.js +92 -0
  28. package/dist/cjs/core/id.js.map +1 -0
  29. package/dist/cjs/core/index.d.ts +9 -0
  30. package/dist/cjs/core/index.d.ts.map +1 -0
  31. package/dist/cjs/core/index.js +25 -0
  32. package/dist/cjs/core/index.js.map +1 -0
  33. package/dist/cjs/core/value-object.d.ts +49 -0
  34. package/dist/cjs/core/value-object.d.ts.map +1 -0
  35. package/dist/cjs/core/value-object.js +129 -0
  36. package/dist/cjs/core/value-object.js.map +1 -0
  37. package/dist/cjs/index.d.ts +5 -11
  38. package/dist/cjs/index.d.ts.map +1 -1
  39. package/dist/cjs/index.js +21 -19
  40. package/dist/cjs/index.js.map +1 -1
  41. package/dist/cjs/repository/base-repository.d.ts +49 -3
  42. package/dist/cjs/repository/base-repository.d.ts.map +1 -1
  43. package/dist/cjs/repository/base-repository.js.map +1 -1
  44. package/dist/cjs/repository/entity-schema-registry.d.ts +281 -0
  45. package/dist/cjs/repository/entity-schema-registry.d.ts.map +1 -0
  46. package/dist/cjs/repository/entity-schema-registry.js +370 -0
  47. package/dist/cjs/repository/entity-schema-registry.js.map +1 -0
  48. package/dist/cjs/repository/index.d.ts +4 -2
  49. package/dist/cjs/repository/index.d.ts.map +1 -1
  50. package/dist/cjs/repository/index.js +4 -6
  51. package/dist/cjs/repository/index.js.map +1 -1
  52. package/dist/cjs/repository/mapper.d.ts +4 -0
  53. package/dist/cjs/repository/mapper.d.ts.map +1 -0
  54. package/dist/cjs/repository/mapper.js +7 -0
  55. package/dist/cjs/repository/mapper.js.map +1 -0
  56. package/dist/cjs/repository/paginated-result.d.ts +54 -0
  57. package/dist/cjs/repository/paginated-result.d.ts.map +1 -0
  58. package/dist/cjs/repository/paginated-result.js +197 -0
  59. package/dist/cjs/repository/paginated-result.js.map +1 -0
  60. package/dist/cjs/types/change-tracker.d.ts +1 -2
  61. package/dist/cjs/types/change-tracker.d.ts.map +1 -1
  62. package/dist/cjs/types/criteria.d.ts +13 -5
  63. package/dist/cjs/types/criteria.d.ts.map +1 -1
  64. package/dist/cjs/types/criteria.js +2 -2
  65. package/dist/cjs/types/criteria.js.map +1 -1
  66. package/dist/cjs/types/domain.d.ts +1 -1
  67. package/dist/cjs/types/domain.d.ts.map +1 -1
  68. package/dist/cjs/types/unit-of-work.d.ts +1 -1
  69. package/dist/cjs/types/unit-of-work.d.ts.map +1 -1
  70. package/dist/cjs/types/utils.d.ts +1 -2
  71. package/dist/cjs/types/utils.d.ts.map +1 -1
  72. package/dist/cjs/utils/criteria-operator-validation.d.ts.map +1 -1
  73. package/dist/cjs/utils/criteria-operator-validation.js +1 -1
  74. package/dist/cjs/utils/criteria-operator-validation.js.map +1 -1
  75. package/dist/cjs/utils/crypto.d.ts +3 -0
  76. package/dist/cjs/utils/crypto.d.ts.map +1 -0
  77. package/dist/cjs/utils/crypto.js +32 -0
  78. package/dist/cjs/utils/crypto.js.map +1 -0
  79. package/dist/esm/core/aggregate-changes.d.ts +206 -0
  80. package/dist/esm/core/aggregate-changes.d.ts.map +1 -0
  81. package/dist/esm/core/aggregate-changes.js +361 -0
  82. package/dist/esm/core/aggregate-changes.js.map +1 -0
  83. package/dist/esm/core/base-entity.d.ts +88 -0
  84. package/dist/esm/core/base-entity.d.ts.map +1 -0
  85. package/dist/esm/core/base-entity.js +325 -0
  86. package/dist/esm/core/base-entity.js.map +1 -0
  87. package/dist/esm/core/change-tracker.d.ts +96 -0
  88. package/dist/esm/core/change-tracker.d.ts.map +1 -0
  89. package/dist/esm/core/change-tracker.js +785 -0
  90. package/dist/esm/core/change-tracker.js.map +1 -0
  91. package/dist/esm/core/domain-event.d.ts +23 -0
  92. package/dist/esm/core/domain-event.d.ts.map +1 -0
  93. package/dist/esm/core/domain-event.js +32 -0
  94. package/dist/esm/core/domain-event.js.map +1 -0
  95. package/dist/esm/core/entity-changes.d.ts +84 -0
  96. package/dist/esm/core/entity-changes.d.ts.map +1 -0
  97. package/dist/esm/core/entity-changes.js +132 -0
  98. package/dist/esm/core/entity-changes.js.map +1 -0
  99. package/dist/esm/core/entity.d.ts +7 -0
  100. package/dist/esm/core/entity.d.ts.map +1 -0
  101. package/dist/esm/core/entity.js +6 -0
  102. package/dist/esm/core/entity.js.map +1 -0
  103. package/dist/esm/core/id.d.ts +59 -0
  104. package/dist/esm/core/id.d.ts.map +1 -0
  105. package/dist/esm/core/id.js +85 -0
  106. package/dist/esm/core/id.js.map +1 -0
  107. package/dist/esm/core/index.d.ts +9 -0
  108. package/dist/esm/core/index.d.ts.map +1 -0
  109. package/dist/esm/core/index.js +9 -0
  110. package/dist/esm/core/index.js.map +1 -0
  111. package/dist/esm/core/value-object.d.ts +49 -0
  112. package/dist/esm/core/value-object.d.ts.map +1 -0
  113. package/dist/esm/core/value-object.js +125 -0
  114. package/dist/esm/core/value-object.js.map +1 -0
  115. package/dist/esm/index.d.ts +5 -11
  116. package/dist/esm/index.d.ts.map +1 -1
  117. package/dist/esm/index.js +4 -10
  118. package/dist/esm/index.js.map +1 -1
  119. package/dist/esm/repository/base-repository.d.ts +49 -3
  120. package/dist/esm/repository/base-repository.d.ts.map +1 -1
  121. package/dist/esm/repository/base-repository.js.map +1 -1
  122. package/dist/esm/repository/entity-schema-registry.d.ts +281 -0
  123. package/dist/esm/repository/entity-schema-registry.d.ts.map +1 -0
  124. package/dist/esm/repository/entity-schema-registry.js +366 -0
  125. package/dist/esm/repository/entity-schema-registry.js.map +1 -0
  126. package/dist/esm/repository/index.d.ts +4 -2
  127. package/dist/esm/repository/index.d.ts.map +1 -1
  128. package/dist/esm/repository/index.js +4 -2
  129. package/dist/esm/repository/index.js.map +1 -1
  130. package/dist/esm/repository/mapper.d.ts +4 -0
  131. package/dist/esm/repository/mapper.d.ts.map +1 -0
  132. package/dist/esm/repository/mapper.js +3 -0
  133. package/dist/esm/repository/mapper.js.map +1 -0
  134. package/dist/esm/repository/paginated-result.d.ts +54 -0
  135. package/dist/esm/repository/paginated-result.d.ts.map +1 -0
  136. package/dist/esm/repository/paginated-result.js +193 -0
  137. package/dist/esm/repository/paginated-result.js.map +1 -0
  138. package/dist/esm/types/change-tracker.d.ts +1 -2
  139. package/dist/esm/types/change-tracker.d.ts.map +1 -1
  140. package/dist/esm/types/criteria.d.ts +13 -5
  141. package/dist/esm/types/criteria.d.ts.map +1 -1
  142. package/dist/esm/types/criteria.js +3 -1
  143. package/dist/esm/types/criteria.js.map +1 -1
  144. package/dist/esm/types/domain.d.ts +1 -1
  145. package/dist/esm/types/domain.d.ts.map +1 -1
  146. package/dist/esm/types/unit-of-work.d.ts +1 -1
  147. package/dist/esm/types/unit-of-work.d.ts.map +1 -1
  148. package/dist/esm/types/utils.d.ts +1 -2
  149. package/dist/esm/types/utils.d.ts.map +1 -1
  150. package/dist/esm/utils/criteria-operator-validation.d.ts.map +1 -1
  151. package/dist/esm/utils/criteria-operator-validation.js +1 -1
  152. package/dist/esm/utils/criteria-operator-validation.js.map +1 -1
  153. package/dist/esm/utils/crypto.d.ts +3 -0
  154. package/dist/esm/utils/crypto.d.ts.map +1 -0
  155. package/dist/esm/utils/crypto.js +29 -0
  156. package/dist/esm/utils/crypto.js.map +1 -0
  157. package/dist/tsconfig.cjs.tsbuildinfo +1 -1
  158. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  159. package/dist/tsconfig.types.tsbuildinfo +1 -1
  160. package/dist/types/core/aggregate-changes.d.ts +206 -0
  161. package/dist/types/core/aggregate-changes.d.ts.map +1 -0
  162. package/dist/types/core/base-entity.d.ts +88 -0
  163. package/dist/types/core/base-entity.d.ts.map +1 -0
  164. package/dist/types/core/change-tracker.d.ts +96 -0
  165. package/dist/types/core/change-tracker.d.ts.map +1 -0
  166. package/dist/types/core/domain-event.d.ts +23 -0
  167. package/dist/types/core/domain-event.d.ts.map +1 -0
  168. package/dist/types/core/entity-changes.d.ts +84 -0
  169. package/dist/types/core/entity-changes.d.ts.map +1 -0
  170. package/dist/types/core/entity.d.ts +7 -0
  171. package/dist/types/core/entity.d.ts.map +1 -0
  172. package/dist/types/core/id.d.ts +59 -0
  173. package/dist/types/core/id.d.ts.map +1 -0
  174. package/dist/types/core/index.d.ts +9 -0
  175. package/dist/types/core/index.d.ts.map +1 -0
  176. package/dist/types/core/value-object.d.ts +49 -0
  177. package/dist/types/core/value-object.d.ts.map +1 -0
  178. package/dist/types/index.d.ts +5 -11
  179. package/dist/types/index.d.ts.map +1 -1
  180. package/dist/types/paginated-result.d.ts +54 -46
  181. package/dist/types/repository/base-repository.d.ts +49 -3
  182. package/dist/types/repository/base-repository.d.ts.map +1 -1
  183. package/dist/types/repository/entity-schema-registry.d.ts +281 -0
  184. package/dist/types/repository/entity-schema-registry.d.ts.map +1 -0
  185. package/dist/types/repository/index.d.ts +4 -2
  186. package/dist/types/repository/index.d.ts.map +1 -1
  187. package/dist/types/repository/mapper.d.ts +4 -0
  188. package/dist/types/repository/mapper.d.ts.map +1 -0
  189. package/dist/types/repository/paginated-result.d.ts +54 -0
  190. package/dist/types/repository/paginated-result.d.ts.map +1 -0
  191. package/dist/types/types/change-tracker.d.ts +1 -2
  192. package/dist/types/types/change-tracker.d.ts.map +1 -1
  193. package/dist/types/types/criteria.d.ts +13 -5
  194. package/dist/types/types/criteria.d.ts.map +1 -1
  195. package/dist/types/types/domain.d.ts +1 -1
  196. package/dist/types/types/domain.d.ts.map +1 -1
  197. package/dist/types/types/unit-of-work.d.ts +1 -1
  198. package/dist/types/types/unit-of-work.d.ts.map +1 -1
  199. package/dist/types/types/utils.d.ts +1 -2
  200. package/dist/types/types/utils.d.ts.map +1 -1
  201. package/dist/types/utils/criteria-operator-validation.d.ts.map +1 -1
  202. package/dist/types/utils/crypto.d.ts +3 -0
  203. package/dist/types/utils/crypto.d.ts.map +1 -0
  204. package/package.json +1 -1
@@ -0,0 +1,789 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChangeTracker = void 0;
4
+ const id_js_1 = require("./id.js");
5
+ const entity_js_1 = require("./entity.js");
6
+ const value_object_js_1 = require("./value-object.js");
7
+ const aggregate_changes_js_1 = require("./aggregate-changes.js");
8
+ /**
9
+ * Tracks changes in Aggregates using Proxy.
10
+ *
11
+ * Features:
12
+ * - Tracks changes in primitive properties
13
+ * - Tracks changes in nested entities (1:1)
14
+ * - Tracks changes in collections (1:N)
15
+ * - Calculates depth automatically
16
+ * - Generates AggregateChanges for persistence
17
+ * - Supports validation on change via onChangeValidator
18
+ */
19
+ class ChangeTracker {
20
+ target;
21
+ rootEntityName;
22
+ path;
23
+ depth;
24
+ parentId;
25
+ parentEntity;
26
+ rootTracker;
27
+ history = [];
28
+ originalValues = new Map();
29
+ trackedArrays = new Map();
30
+ trackedEntities = new Map();
31
+ onChangeValidator;
32
+ constructor(target, rootEntityName, path = "", depth = 0,
33
+ // @ts-expect-error - This is a private property
34
+ parentId,
35
+ // @ts-expect-error - This is a private property
36
+ parentEntity, rootTracker) {
37
+ this.target = target;
38
+ this.rootEntityName = rootEntityName;
39
+ this.path = path;
40
+ this.depth = depth;
41
+ this.parentId = parentId;
42
+ this.parentEntity = parentEntity;
43
+ this.rootTracker = rootTracker;
44
+ if (!rootTracker) {
45
+ this.rootTracker = this;
46
+ }
47
+ this.captureInitialState();
48
+ }
49
+ /**
50
+ * Sets a validator callback that will be called on every property change.
51
+ * The validator can:
52
+ * - Return false to reject the change (value will be reverted)
53
+ * - Throw an error to reject the change with an error
54
+ * - Return true/undefined to accept the change
55
+ */
56
+ setOnChangeValidator(validator) {
57
+ this.getRootTracker().onChangeValidator = validator;
58
+ }
59
+ captureInitialState() {
60
+ if (this.depth > 0)
61
+ return;
62
+ this.captureEntityState(this.target, this.rootEntityName, "", 0);
63
+ }
64
+ captureEntityState(obj, entityName, path, depth, parentId, parentEntity) {
65
+ if (!obj || typeof obj !== "object")
66
+ return;
67
+ const id = this.getEntityId(obj);
68
+ const key = path || "root";
69
+ this.trackedEntities.set(key, {
70
+ entity: obj,
71
+ metadata: {
72
+ entityName,
73
+ depth,
74
+ parentId,
75
+ parentEntity,
76
+ path,
77
+ },
78
+ originalState: this.deepClone(obj),
79
+ });
80
+ const propsToScan = obj.props || obj;
81
+ for (const [propName, value] of Object.entries(propsToScan)) {
82
+ if (propName === "id")
83
+ continue;
84
+ const propPath = path ? `${path}.${propName}` : propName;
85
+ if (Array.isArray(value)) {
86
+ this.captureArrayState(value, propPath, depth + 1, id, entityName);
87
+ }
88
+ else if (value instanceof entity_js_1.Entity) {
89
+ const nestedName = this.getEntityName(value);
90
+ this.captureEntityState(value, nestedName, propPath, depth + 1, id, entityName);
91
+ }
92
+ }
93
+ }
94
+ captureArrayState(arr, path, depth, parentId, parentEntity) {
95
+ const entityName = arr.length > 0 ? this.getEntityName(arr[0]) : "Unknown";
96
+ this.trackedArrays.set(path, {
97
+ cloned: this.cloneArray(arr),
98
+ original: arr.slice(),
99
+ metadata: {
100
+ entityName,
101
+ depth,
102
+ parentId,
103
+ parentEntity,
104
+ path,
105
+ },
106
+ });
107
+ arr.forEach((item, index) => {
108
+ if (item instanceof entity_js_1.Entity) {
109
+ const itemPath = `${path}[${index}]`;
110
+ this.captureEntityState(item, this.getEntityName(item), itemPath, depth, parentId, parentEntity);
111
+ }
112
+ });
113
+ }
114
+ createProxy() {
115
+ const handler = {
116
+ get: (target, prop, receiver) => {
117
+ const value = Reflect.get(target, prop, receiver);
118
+ if (this.shouldSkipProperty(prop)) {
119
+ return value;
120
+ }
121
+ if (typeof value === "function") {
122
+ return value.bind(target);
123
+ }
124
+ const currentPath = this.buildPath(String(prop));
125
+ if (Array.isArray(value)) {
126
+ return this.createArrayProxy(value, currentPath);
127
+ }
128
+ if (value instanceof entity_js_1.Entity) {
129
+ const nestedTracker = new ChangeTracker(value, this.getEntityName(value), currentPath, this.depth + 1, this.getEntityId(this.target), this.rootEntityName, this.rootTracker);
130
+ return nestedTracker.createProxy();
131
+ }
132
+ return value;
133
+ },
134
+ set: (target, prop, newValue, receiver) => {
135
+ const currentPath = this.buildPath(String(prop));
136
+ const oldValue = Reflect.get(target, prop, receiver);
137
+ if (!Array.isArray(newValue) && oldValue === newValue) {
138
+ return true;
139
+ }
140
+ const rootTracker = this.getRootTracker();
141
+ if (rootTracker.onChangeValidator) {
142
+ try {
143
+ const result = rootTracker.onChangeValidator(currentPath, newValue);
144
+ if (result === false) {
145
+ return true;
146
+ }
147
+ }
148
+ catch (error) {
149
+ throw error;
150
+ }
151
+ }
152
+ if (!rootTracker.originalValues.has(currentPath)) {
153
+ rootTracker.originalValues.set(currentPath, oldValue);
154
+ }
155
+ rootTracker.history.push({
156
+ path: currentPath,
157
+ previousValue: oldValue,
158
+ currentValue: newValue,
159
+ timestamp: Date.now(),
160
+ });
161
+ const result = Reflect.set(target, prop, newValue, receiver);
162
+ if (Array.isArray(newValue)) {
163
+ this.handleArrayAssignment(currentPath, oldValue);
164
+ }
165
+ else if (newValue instanceof entity_js_1.Entity || oldValue instanceof entity_js_1.Entity) {
166
+ this.handleEntityChange(currentPath, oldValue, newValue);
167
+ }
168
+ return result;
169
+ },
170
+ };
171
+ const proxy = new Proxy(this.target, handler);
172
+ Object.defineProperty(proxy, "__isProxy", { value: true, writable: false });
173
+ return proxy;
174
+ }
175
+ createArrayProxy(array, path) {
176
+ const tracker = this;
177
+ const rootTracker = this.getRootTracker();
178
+ if (!rootTracker.trackedArrays.has(path)) {
179
+ const parentId = this.getEntityId(this.target);
180
+ rootTracker.captureArrayState(array, path, this.depth + 1, parentId, this.rootEntityName);
181
+ }
182
+ return new Proxy(array, {
183
+ get(target, prop, receiver) {
184
+ const value = Reflect.get(target, prop, receiver);
185
+ if (typeof value === "function") {
186
+ const mutatingMethods = [
187
+ "push",
188
+ "pop",
189
+ "shift",
190
+ "unshift",
191
+ "splice",
192
+ "sort",
193
+ "reverse",
194
+ ];
195
+ if (mutatingMethods.includes(String(prop))) {
196
+ return function (...args) {
197
+ const oldArray = target.slice();
198
+ if (rootTracker.onChangeValidator) {
199
+ try {
200
+ const result = rootTracker.onChangeValidator(path, [
201
+ ...oldArray,
202
+ ...args,
203
+ ]);
204
+ if (result === false) {
205
+ return undefined;
206
+ }
207
+ }
208
+ catch (error) {
209
+ throw error;
210
+ }
211
+ }
212
+ const result = value.apply(target, args);
213
+ rootTracker.history.push({
214
+ path,
215
+ previousValue: oldArray,
216
+ currentValue: target.slice(),
217
+ timestamp: Date.now(),
218
+ });
219
+ return result;
220
+ };
221
+ }
222
+ return value.bind(target);
223
+ }
224
+ if (!isNaN(Number(prop)) && value instanceof entity_js_1.Entity) {
225
+ const nestedPath = `${path}[${String(prop)}]`;
226
+ const nestedTracker = new ChangeTracker(value, tracker.getEntityName(value), nestedPath, tracker.depth + 1, tracker.getEntityId(tracker.target), tracker.rootEntityName, rootTracker);
227
+ return nestedTracker.createProxy();
228
+ }
229
+ return value;
230
+ },
231
+ set(target, prop, newValue, receiver) {
232
+ if (!isNaN(Number(prop))) {
233
+ const oldArray = target.slice();
234
+ if (rootTracker.onChangeValidator) {
235
+ try {
236
+ const result = rootTracker.onChangeValidator(path, newValue);
237
+ if (result === false) {
238
+ return true;
239
+ }
240
+ }
241
+ catch (error) {
242
+ throw error;
243
+ }
244
+ }
245
+ const result = Reflect.set(target, prop, newValue, receiver);
246
+ rootTracker.history.push({
247
+ path,
248
+ previousValue: oldArray,
249
+ currentValue: target.slice(),
250
+ timestamp: Date.now(),
251
+ });
252
+ return result;
253
+ }
254
+ return Reflect.set(target, prop, newValue, receiver);
255
+ },
256
+ });
257
+ }
258
+ /**
259
+ * Returns all detected changes as AggregateChanges.
260
+ */
261
+ getChanges() {
262
+ const changes = new aggregate_changes_js_1.AggregateChanges();
263
+ const rootTracker = this.getRootTracker();
264
+ this.analyzeRootChanges(changes, rootTracker);
265
+ this.analyzeCollectionChanges(changes, rootTracker);
266
+ this.analyzeEntityChanges(changes, rootTracker);
267
+ return changes;
268
+ }
269
+ analyzeRootChanges(changes, rootTracker) {
270
+ const changedFields = {};
271
+ let hasChanges = false;
272
+ for (const [path, originalValue] of rootTracker.originalValues) {
273
+ if (path.includes(".") || path.includes("["))
274
+ continue;
275
+ const currentValue = this.target[path];
276
+ if (!this.isEqual(originalValue, currentValue)) {
277
+ changedFields[path] =
278
+ currentValue instanceof value_object_js_1.ValueObject
279
+ ? currentValue.value
280
+ : currentValue;
281
+ hasChanges = true;
282
+ }
283
+ }
284
+ if (hasChanges) {
285
+ const id = this.getEntityId(this.target);
286
+ if (id) {
287
+ changes.addUpdate(this.rootEntityName, id, this.target, changedFields, 0);
288
+ }
289
+ }
290
+ }
291
+ analyzeCollectionChanges(changes, rootTracker) {
292
+ const allTrackedArrays = new Map();
293
+ const processedArrays = new Set();
294
+ for (const [path, arrayState] of rootTracker.trackedArrays) {
295
+ const currentArray = this.getValueAtPath(this.target, path);
296
+ if (Array.isArray(currentArray) && !processedArrays.has(currentArray)) {
297
+ allTrackedArrays.set(path, arrayState);
298
+ processedArrays.add(currentArray);
299
+ }
300
+ }
301
+ this.collectNestedArrays(this.target, "", allTrackedArrays, processedArrays);
302
+ for (const [path, arrayState] of allTrackedArrays) {
303
+ const currentArray = this.getValueAtPath(this.target, path);
304
+ if (!Array.isArray(currentArray))
305
+ continue;
306
+ const { created, updated, deleted } = this.detectArrayChanges(arrayState.cloned, arrayState.original, currentArray);
307
+ const { depth, parentId, parentEntity } = arrayState.metadata;
308
+ const relationField = this.extractRelationField(path);
309
+ for (const item of created) {
310
+ const itemEntityName = this.getEntityName(item);
311
+ changes.addCreate(itemEntityName, item, depth, parentId, parentEntity, relationField);
312
+ this.markNestedItemsAsCreated(item, depth, changes);
313
+ }
314
+ for (const item of updated) {
315
+ const id = this.getEntityId(item);
316
+ if (id) {
317
+ const original = arrayState.cloned.find((o) => this.getEntityId(o) === id);
318
+ const changedFields = this.detectChangedFields(original, item);
319
+ if (Object.keys(changedFields).length > 0) {
320
+ const itemEntityName = this.getEntityName(item);
321
+ changes.addUpdate(itemEntityName, id, item, changedFields, depth);
322
+ }
323
+ }
324
+ }
325
+ for (const item of deleted) {
326
+ const id = this.getEntityId(item);
327
+ const key = this.getItemKey(item);
328
+ if (id || key) {
329
+ const itemEntityName = this.getEntityName(item);
330
+ const deleteId = id || key;
331
+ changes.addDelete(itemEntityName, deleteId, item, depth, relationField, parentId, parentEntity);
332
+ this.markNestedItemsAsDeleted(item, depth, changes, rootTracker);
333
+ }
334
+ }
335
+ }
336
+ }
337
+ /**
338
+ * Recursively marks all nested items as created when a parent is created.
339
+ */
340
+ markNestedItemsAsCreated(item, parentDepth, changes) {
341
+ if (!item || typeof item !== "object")
342
+ return;
343
+ const propsToScan = item.props || item;
344
+ const parentId = this.getEntityId(item);
345
+ const parentEntity = this.getEntityName(item);
346
+ for (const [propName, value] of Object.entries(propsToScan)) {
347
+ if (propName === "id")
348
+ continue;
349
+ if (Array.isArray(value)) {
350
+ const relationField = propName;
351
+ for (const child of value) {
352
+ if (child instanceof entity_js_1.Entity) {
353
+ const childEntityName = this.getEntityName(child);
354
+ changes.addCreate(childEntityName, child, parentDepth + 1, parentId, parentEntity, relationField);
355
+ this.markNestedItemsAsCreated(child, parentDepth + 1, changes);
356
+ }
357
+ }
358
+ }
359
+ else if (value instanceof entity_js_1.Entity) {
360
+ const childEntityName = this.getEntityName(value);
361
+ changes.addCreate(childEntityName, value, parentDepth + 1, parentId, parentEntity, propName);
362
+ this.markNestedItemsAsCreated(value, parentDepth + 1, changes);
363
+ }
364
+ }
365
+ }
366
+ /**
367
+ * Recursively marks all nested items as deleted when a parent is deleted.
368
+ * Uses the original captured state to find nested items.
369
+ */
370
+ markNestedItemsAsDeleted(item, parentDepth, changes, rootTracker) {
371
+ if (!item || typeof item !== "object")
372
+ return;
373
+ const itemId = this.getEntityId(item);
374
+ if (!itemId)
375
+ return;
376
+ for (const [path, arrayState] of rootTracker.trackedArrays) {
377
+ if (arrayState.metadata.parentId === itemId) {
378
+ const relationField = this.extractRelationField(path);
379
+ const parentEntity = arrayState.metadata.parentEntity;
380
+ const parentId = arrayState.metadata.parentId;
381
+ for (const nestedItem of arrayState.cloned) {
382
+ const id = typeof nestedItem === "object" && nestedItem !== null
383
+ ? nestedItem.id
384
+ : undefined;
385
+ if (id) {
386
+ const entityName = arrayState.metadata.entityName;
387
+ changes.addDelete(entityName, id, nestedItem, parentDepth + 1, relationField, parentEntity, parentId);
388
+ this.markNestedJsonItemAsDeleted(id, parentDepth + 1, changes, rootTracker);
389
+ }
390
+ }
391
+ }
392
+ }
393
+ }
394
+ /**
395
+ * Recursively marks nested items as deleted from a JSON object.
396
+ * This is used when processing cloned (JSON) state.
397
+ */
398
+ markNestedJsonItemAsDeleted(itemId, parentDepth, changes, rootTracker) {
399
+ for (const [path, arrayState] of rootTracker.trackedArrays) {
400
+ if (arrayState.metadata.parentId === itemId) {
401
+ const relationField = this.extractRelationField(path);
402
+ for (const nestedJsonItem of arrayState.cloned) {
403
+ if (typeof nestedJsonItem !== "object" || nestedJsonItem === null)
404
+ continue;
405
+ const nestedId = nestedJsonItem.id;
406
+ const entityName = arrayState.metadata.entityName;
407
+ const parentEntity = arrayState.metadata.parentEntity;
408
+ const parentId = arrayState.metadata.parentId;
409
+ if (nestedId) {
410
+ changes.addDelete(entityName, nestedId, nestedJsonItem, parentDepth + 1, relationField, parentId, parentEntity);
411
+ this.markNestedJsonItemAsDeleted(nestedId, parentDepth + 1, changes, rootTracker);
412
+ }
413
+ else {
414
+ const key = this.extractIdentityKeyFromJson(nestedJsonItem, arrayState.original);
415
+ if (key) {
416
+ changes.addDelete(entityName, key, nestedJsonItem, parentDepth + 1, relationField, parentId, parentEntity);
417
+ }
418
+ }
419
+ }
420
+ }
421
+ }
422
+ }
423
+ /**
424
+ * Extracts identity key from a JSON object by looking at the original Entity instances.
425
+ */
426
+ extractIdentityKeyFromJson(jsonItem, originalArray) {
427
+ for (const originalItem of originalArray) {
428
+ if (originalItem instanceof entity_js_1.Entity) {
429
+ const originalJson = this.deepClone(originalItem);
430
+ if (JSON.stringify(originalJson) === JSON.stringify(jsonItem)) {
431
+ const key = this.getItemKey(originalItem);
432
+ if (key)
433
+ return key;
434
+ }
435
+ }
436
+ }
437
+ if (jsonItem.id)
438
+ return jsonItem.id;
439
+ return undefined;
440
+ }
441
+ collectNestedArrays(obj, basePath, allArrays, processedArrays) {
442
+ if (!obj || typeof obj !== "object")
443
+ return;
444
+ for (const [propName, value] of Object.entries(obj)) {
445
+ if (propName === "id" || propName === "proxy" || propName === "_props")
446
+ continue;
447
+ const propPath = basePath ? `${basePath}.${propName}` : propName;
448
+ if (Array.isArray(value)) {
449
+ value.forEach((item, index) => {
450
+ if (item instanceof entity_js_1.Entity) {
451
+ this.collectNestedArrays(item, `${propPath}[${index}]`, allArrays, processedArrays);
452
+ }
453
+ });
454
+ }
455
+ else if (value instanceof entity_js_1.Entity) {
456
+ this.collectNestedArrays(value, propPath, allArrays, processedArrays);
457
+ }
458
+ }
459
+ }
460
+ analyzeEntityChanges(changes, rootTracker) {
461
+ for (const [path, trackedItem] of rootTracker.trackedEntities) {
462
+ if (path === "root")
463
+ continue;
464
+ if (path.includes("["))
465
+ continue;
466
+ const currentValue = this.getValueAtPath(this.target, path);
467
+ const originalValue = trackedItem.originalState;
468
+ const originalEntity = trackedItem.entity;
469
+ const { entityName, depth, parentId, parentEntity } = trackedItem.metadata;
470
+ const relationField = this.extractRelationField(path);
471
+ const state = this.detectEntityChangeState(originalValue, currentValue);
472
+ switch (state) {
473
+ case "created":
474
+ changes.addCreate(entityName, currentValue, depth, parentId, parentEntity, relationField);
475
+ break;
476
+ case "deleted":
477
+ const id = this.getEntityId(originalValue);
478
+ if (id) {
479
+ changes.addDelete(entityName, id, originalEntity, depth, relationField, parentId, parentEntity);
480
+ }
481
+ break;
482
+ case "replaced":
483
+ const oldId = this.getEntityId(originalValue);
484
+ if (oldId) {
485
+ changes.addDelete(entityName, oldId, originalEntity, depth, relationField, parentId, parentEntity);
486
+ }
487
+ changes.addCreate(entityName, currentValue, depth, parentId, parentEntity, relationField);
488
+ break;
489
+ case "updated":
490
+ const updateId = this.getEntityId(currentValue);
491
+ if (updateId) {
492
+ const changedFields = this.detectChangedFields(originalValue, currentValue);
493
+ if (Object.keys(changedFields).length > 0) {
494
+ changes.addUpdate(entityName, updateId, currentValue, changedFields, depth);
495
+ }
496
+ }
497
+ break;
498
+ }
499
+ }
500
+ }
501
+ detectEntityChangeState(previous, current) {
502
+ if (previous === null && current !== null) {
503
+ return "created";
504
+ }
505
+ if (previous !== null && current === null) {
506
+ return "deleted";
507
+ }
508
+ if (previous !== null && current !== null) {
509
+ const prevId = this.getEntityId(previous);
510
+ const currId = this.getEntityId(current);
511
+ if (prevId && currId && prevId === currId) {
512
+ return this.hasChanged(previous, current) ? "updated" : "unchanged";
513
+ }
514
+ else {
515
+ return "replaced";
516
+ }
517
+ }
518
+ return "unchanged";
519
+ }
520
+ detectArrayChanges(oldCloned, oldOriginal, newArray) {
521
+ const created = [];
522
+ const updated = [];
523
+ const deleted = [];
524
+ const oldMap = new Map();
525
+ const newMap = new Map();
526
+ oldCloned.forEach((item) => {
527
+ const key = this.getItemKey(item);
528
+ if (key)
529
+ oldMap.set(key, item);
530
+ });
531
+ newArray.forEach((item) => {
532
+ const key = this.getItemKey(item);
533
+ if (key)
534
+ newMap.set(key, item);
535
+ });
536
+ newArray.forEach((item) => {
537
+ const key = this.getItemKey(item);
538
+ if (!key) {
539
+ created.push(item);
540
+ }
541
+ else if (!oldMap.has(key)) {
542
+ created.push(item);
543
+ }
544
+ else if (this.hasChanged(oldMap.get(key), item)) {
545
+ updated.push(item);
546
+ }
547
+ });
548
+ oldOriginal.forEach((item) => {
549
+ const key = this.getItemKey(item);
550
+ if (key && !newMap.has(key)) {
551
+ deleted.push(item);
552
+ }
553
+ });
554
+ return { created, updated, deleted };
555
+ }
556
+ detectChangedFields(original, current) {
557
+ const changes = {};
558
+ if (!original || !current)
559
+ return changes;
560
+ const origProps = original.props || original;
561
+ const currProps = current.props || current;
562
+ for (const key of Object.keys(currProps)) {
563
+ if (key === "id")
564
+ continue;
565
+ const origValue = origProps[key];
566
+ const currValue = currProps[key];
567
+ if (Array.isArray(currValue) || currValue instanceof entity_js_1.Entity) {
568
+ continue;
569
+ }
570
+ if (!this.isEqual(origValue, currValue)) {
571
+ changes[key] =
572
+ currValue instanceof value_object_js_1.ValueObject ? currValue.value : currValue;
573
+ }
574
+ }
575
+ return changes;
576
+ }
577
+ handleArrayAssignment(path, oldValue) {
578
+ const rootTracker = this.getRootTracker();
579
+ if (!rootTracker.trackedArrays.has(path)) {
580
+ const parentId = this.getEntityId(this.target);
581
+ rootTracker.captureArrayState(Array.isArray(oldValue) ? oldValue : [], path, this.depth + 1, parentId, this.rootEntityName);
582
+ }
583
+ }
584
+ handleEntityChange(path, oldValue, newValue) {
585
+ const rootTracker = this.getRootTracker();
586
+ const entityName = newValue
587
+ ? this.getEntityName(newValue)
588
+ : this.getEntityName(oldValue);
589
+ const existingTracked = rootTracker.trackedEntities.get(path);
590
+ rootTracker.trackedEntities.set(path, {
591
+ entity: existingTracked?.entity || oldValue,
592
+ metadata: {
593
+ entityName,
594
+ depth: this.depth + 1,
595
+ parentId: this.getEntityId(this.target),
596
+ parentEntity: this.rootEntityName,
597
+ path,
598
+ },
599
+ originalState: existingTracked?.originalState,
600
+ });
601
+ }
602
+ getRootTracker() {
603
+ return this.rootTracker || this;
604
+ }
605
+ buildPath(prop) {
606
+ return this.path ? `${this.path}.${prop}` : prop;
607
+ }
608
+ shouldSkipProperty(prop) {
609
+ const skipProps = [
610
+ "__isProxy",
611
+ "__tracker",
612
+ "__originalTarget",
613
+ "__path",
614
+ "constructor",
615
+ "prototype",
616
+ ];
617
+ return skipProps.includes(String(prop));
618
+ }
619
+ getValueAtPath(obj, path) {
620
+ if (!path)
621
+ return obj;
622
+ const parts = path.split(/[.\[\]]+/).filter(Boolean);
623
+ let current = obj;
624
+ for (const part of parts) {
625
+ if (current === null || current === undefined)
626
+ return undefined;
627
+ const propsToAccess = current.props || current;
628
+ current = propsToAccess[part];
629
+ }
630
+ return current;
631
+ }
632
+ extractRelationField(path) {
633
+ const withoutIndices = path.replace(/\[\d+\]/g, "");
634
+ const parts = withoutIndices.split(".");
635
+ return parts[parts.length - 1];
636
+ }
637
+ getItemKey(item) {
638
+ const id = this.getEntityId(item);
639
+ if (id)
640
+ return id;
641
+ return undefined;
642
+ }
643
+ getEntityId(item) {
644
+ if (!item)
645
+ return undefined;
646
+ if (item.id instanceof id_js_1.Id)
647
+ return item.id.value;
648
+ if (item.id !== undefined)
649
+ return String(item.id);
650
+ return undefined;
651
+ }
652
+ getEntityName(item) {
653
+ if (!item)
654
+ return "Unknown";
655
+ return item.constructor?.name || "Unknown";
656
+ }
657
+ isEqual(a, b) {
658
+ if (a === b)
659
+ return true;
660
+ if (a instanceof id_js_1.Id && b instanceof id_js_1.Id)
661
+ return a.equals(b);
662
+ if (a instanceof value_object_js_1.ValueObject && b instanceof value_object_js_1.ValueObject)
663
+ return a.equals(b);
664
+ if (a instanceof Date && b instanceof Date)
665
+ return a.getTime() === b.getTime();
666
+ try {
667
+ return this.hasChanged(a, b) === false;
668
+ }
669
+ catch {
670
+ return this.deepEqual(a, b);
671
+ }
672
+ }
673
+ deepEqual(a, b) {
674
+ if (a === b)
675
+ return true;
676
+ if (a == null || b == null)
677
+ return a === b;
678
+ if (typeof a !== typeof b)
679
+ return false;
680
+ if (typeof a !== "object")
681
+ return a === b;
682
+ if (Array.isArray(a) !== Array.isArray(b))
683
+ return false;
684
+ if (Array.isArray(a)) {
685
+ if (a.length !== b.length)
686
+ return false;
687
+ for (let i = 0; i < a.length; i++) {
688
+ if (!this.deepEqual(a[i], b[i]))
689
+ return false;
690
+ }
691
+ return true;
692
+ }
693
+ const keysA = Object.keys(a).filter((key) => {
694
+ const value = a[key];
695
+ return (typeof value !== "object" ||
696
+ value instanceof Date ||
697
+ value instanceof id_js_1.Id ||
698
+ value === null);
699
+ });
700
+ const keysB = Object.keys(b).filter((key) => {
701
+ const value = b[key];
702
+ return (typeof value !== "object" ||
703
+ value instanceof Date ||
704
+ value instanceof id_js_1.Id ||
705
+ value === null);
706
+ });
707
+ if (keysA.length !== keysB.length)
708
+ return false;
709
+ for (const key of keysA) {
710
+ if (!keysB.includes(key))
711
+ return false;
712
+ if (!this.isEqual(a[key], b[key]))
713
+ return false;
714
+ }
715
+ return true;
716
+ }
717
+ hasChanged(obj1, obj2) {
718
+ const json1 = this.normalizeAndStringify(this.deepClone(obj1));
719
+ const json2 = this.normalizeAndStringify(this.deepClone(obj2));
720
+ return json1 !== json2;
721
+ }
722
+ cloneArray(arr) {
723
+ return arr.map((item) => this.deepClone(item));
724
+ }
725
+ deepClone(obj) {
726
+ if (obj === null || obj === undefined || typeof obj !== "object") {
727
+ return obj;
728
+ }
729
+ if (obj instanceof id_js_1.Id) {
730
+ return obj.value;
731
+ }
732
+ if (obj instanceof value_object_js_1.ValueObject) {
733
+ return obj.value;
734
+ }
735
+ if (typeof obj.toJSON === "function") {
736
+ return obj.toJSON();
737
+ }
738
+ if (Array.isArray(obj)) {
739
+ return obj.map((item) => this.deepClone(item));
740
+ }
741
+ if (obj instanceof Date) {
742
+ return new Date(obj.getTime());
743
+ }
744
+ try {
745
+ return structuredClone(obj);
746
+ }
747
+ catch {
748
+ const cloned = {};
749
+ for (const key in obj) {
750
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
751
+ cloned[key] = this.deepClone(obj[key]);
752
+ }
753
+ }
754
+ return cloned;
755
+ }
756
+ }
757
+ normalizeAndStringify(obj) {
758
+ if (obj === null || typeof obj !== "object") {
759
+ return JSON.stringify(obj);
760
+ }
761
+ if (Array.isArray(obj)) {
762
+ return `[${obj
763
+ .map((item) => this.normalizeAndStringify(item))
764
+ .join(",")}]`;
765
+ }
766
+ const keys = Object.keys(obj).sort();
767
+ const parts = keys.map((key) => `"${key}":${this.normalizeAndStringify(obj[key])}`);
768
+ return `{${parts.join(",")}}`;
769
+ }
770
+ getHistory() {
771
+ return [...this.getRootTracker().history];
772
+ }
773
+ clearHistory() {
774
+ const rootTracker = this.getRootTracker();
775
+ rootTracker.history = [];
776
+ rootTracker.originalValues.clear();
777
+ rootTracker.trackedArrays.clear();
778
+ rootTracker.trackedEntities.clear();
779
+ this.captureInitialState();
780
+ }
781
+ markAsClean() {
782
+ this.clearHistory();
783
+ }
784
+ getTarget() {
785
+ return this.target;
786
+ }
787
+ }
788
+ exports.ChangeTracker = ChangeTracker;
789
+ //# sourceMappingURL=change-tracker.js.map