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