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