@woltz/rich-domain 1.2.0 → 1.2.1
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 +33 -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 +117 -86
- package/dist/base-entity.js.map +1 -1
- package/dist/criteria.d.ts +3 -3
- package/dist/criteria.d.ts.map +1 -1
- 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/entity-changes.d.ts +84 -0
- package/dist/entity-changes.d.ts.map +1 -0
- package/dist/entity-changes.js +135 -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 +219 -0
- package/dist/entity-schema-registry.js.map +1 -0
- package/dist/history-tracker.d.ts +97 -0
- package/dist/history-tracker.d.ts.map +1 -0
- package/dist/history-tracker.js +805 -0
- package/dist/history-tracker.js.map +1 -0
- 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 +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- 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/repository/base-repository.d.ts +6 -32
- 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/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 -25
- package/dist/repository/unit-of-work.js.map +1 -1
- package/dist/types/change-tracker.d.ts +186 -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/history-tracker.d.ts +11 -0
- package/dist/types/history-tracker.d.ts.map +1 -1
- package/dist/types/utils.d.ts +0 -1
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/validation-error.d.ts.map +1 -1
- package/dist/validation-error.js +0 -3
- 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 -21
- 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 +140 -100
- package/src/criteria.ts +2 -1
- package/src/crypto.ts +31 -0
- package/src/entity-changes.ts +151 -0
- package/src/entity-schema-registry.ts +275 -0
- package/src/history-tracker.ts +1114 -0
- package/src/id.ts +17 -26
- package/src/index.ts +1 -0
- package/src/mapper.ts +4 -1
- package/src/repository/base-repository.ts +6 -37
- package/src/repository/unit-of-work.ts +0 -25
- package/src/types/change-tracker.ts +221 -0
- package/src/types/criteria.ts +6 -1
- package/src/types/history-tracker.ts +13 -0
- package/src/types/utils.ts +0 -9
- package/src/validation-error.ts +0 -4
- package/src/value-object.ts +84 -23
- 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/src/deep-proxy.ts +0 -447
- package/tests/entity.test.ts +0 -33
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// Example Tests - Demonstrating Standard Schema Validation with Zod
|
|
3
|
-
// ============================================================================
|
|
4
|
-
|
|
5
1
|
import { z } from "zod";
|
|
6
2
|
import {
|
|
7
3
|
Id,
|
|
@@ -10,16 +6,6 @@ import {
|
|
|
10
6
|
EntityHooks,
|
|
11
7
|
ValidationError,
|
|
12
8
|
} from "../src";
|
|
13
|
-
import { Address } from "./utils";
|
|
14
|
-
import { BaseProps } from "../src/types";
|
|
15
|
-
|
|
16
|
-
interface UserProps extends BaseProps {
|
|
17
|
-
id: Id;
|
|
18
|
-
name: string;
|
|
19
|
-
email: string;
|
|
20
|
-
age: number;
|
|
21
|
-
status: "active" | "inactive";
|
|
22
|
-
}
|
|
23
9
|
|
|
24
10
|
const userSchema = z.object({
|
|
25
11
|
id: z.custom<Id>((val) => val instanceof Id, { message: "Invalid Id" }),
|
|
@@ -29,6 +15,7 @@ const userSchema = z.object({
|
|
|
29
15
|
status: z.enum(["active", "inactive"]),
|
|
30
16
|
});
|
|
31
17
|
|
|
18
|
+
interface UserProps extends z.infer<typeof userSchema> {}
|
|
32
19
|
class User extends Aggregate<UserProps> {
|
|
33
20
|
protected static validation: EntityValidation<UserProps> = {
|
|
34
21
|
schema: userSchema,
|
|
@@ -43,7 +30,7 @@ class User extends Aggregate<UserProps> {
|
|
|
43
30
|
onCreate: (entity) => {},
|
|
44
31
|
onBeforeUpdate: (entity, snapshot) => {
|
|
45
32
|
if (snapshot.email !== entity.email) {
|
|
46
|
-
return false;
|
|
33
|
+
return false;
|
|
47
34
|
}
|
|
48
35
|
return true;
|
|
49
36
|
},
|
|
@@ -91,17 +78,13 @@ class User extends Aggregate<UserProps> {
|
|
|
91
78
|
}
|
|
92
79
|
}
|
|
93
80
|
|
|
94
|
-
// ============================================================================
|
|
95
|
-
// Example: User with throwOnError: false
|
|
96
|
-
// ============================================================================
|
|
97
|
-
|
|
98
81
|
class UserSafe extends Aggregate<UserProps> {
|
|
99
82
|
protected static validation: EntityValidation<UserProps> = {
|
|
100
83
|
schema: userSchema,
|
|
101
84
|
config: {
|
|
102
85
|
onCreate: true,
|
|
103
86
|
onUpdate: true,
|
|
104
|
-
throwOnError: false,
|
|
87
|
+
throwOnError: false,
|
|
105
88
|
},
|
|
106
89
|
};
|
|
107
90
|
|
|
@@ -116,10 +99,6 @@ class UserSafe extends Aggregate<UserProps> {
|
|
|
116
99
|
}
|
|
117
100
|
}
|
|
118
101
|
|
|
119
|
-
// ============================================================================
|
|
120
|
-
// Tests
|
|
121
|
-
// ============================================================================
|
|
122
|
-
|
|
123
102
|
describe("Rich Domain with Standard Schema Validation", () => {
|
|
124
103
|
describe("User Creation with Validation", () => {
|
|
125
104
|
it("should create user with valid data", () => {
|
|
@@ -172,7 +151,7 @@ describe("Rich Domain with Standard Schema Validation", () => {
|
|
|
172
151
|
|
|
173
152
|
it("should not throw when throwOnError is false", () => {
|
|
174
153
|
const user = new UserSafe({
|
|
175
|
-
name: "J",
|
|
154
|
+
name: "J",
|
|
176
155
|
email: "invalid",
|
|
177
156
|
age: 30,
|
|
178
157
|
status: "active",
|
|
@@ -209,7 +188,7 @@ describe("Rich Domain with Standard Schema Validation", () => {
|
|
|
209
188
|
});
|
|
210
189
|
|
|
211
190
|
expect(() => {
|
|
212
|
-
user.name = "J";
|
|
191
|
+
user.name = "J";
|
|
213
192
|
}).toThrow(ValidationError);
|
|
214
193
|
});
|
|
215
194
|
|
|
@@ -233,10 +212,8 @@ describe("Rich Domain with Standard Schema Validation", () => {
|
|
|
233
212
|
status: "active",
|
|
234
213
|
});
|
|
235
214
|
|
|
236
|
-
// Email change should be blocked by onBeforeUpdate
|
|
237
215
|
user.email = "newemail@example.com";
|
|
238
216
|
|
|
239
|
-
// Email should remain unchanged
|
|
240
217
|
expect(user.email).toBe("john@example.com");
|
|
241
218
|
});
|
|
242
219
|
|
|
@@ -249,7 +226,7 @@ describe("Rich Domain with Standard Schema Validation", () => {
|
|
|
249
226
|
});
|
|
250
227
|
|
|
251
228
|
expect(() => {
|
|
252
|
-
user.name = "admin";
|
|
229
|
+
user.name = "admin";
|
|
253
230
|
}).toThrow(Error);
|
|
254
231
|
});
|
|
255
232
|
});
|
|
@@ -269,71 +246,7 @@ describe("Rich Domain with Standard Schema Validation", () => {
|
|
|
269
246
|
expect(json.email).toBe("john@example.com");
|
|
270
247
|
expect(json.age).toBe(30);
|
|
271
248
|
expect(json.status).toBe("active");
|
|
272
|
-
expect(typeof json.id).toBe("string");
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
describe("History Tracking", () => {
|
|
277
|
-
it("should track property changes", () => {
|
|
278
|
-
const user = new User({
|
|
279
|
-
name: "John Doe",
|
|
280
|
-
email: "john@example.com",
|
|
281
|
-
age: 30,
|
|
282
|
-
status: "active",
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
user.name = "Jane Doe";
|
|
286
|
-
user.age = 25;
|
|
287
|
-
|
|
288
|
-
const history = user.getHistory();
|
|
289
|
-
expect(history.length).toBe(2);
|
|
290
|
-
expect(history[0].path).toBe("name");
|
|
291
|
-
expect(history[0].previousValue).toBe("John Doe");
|
|
292
|
-
expect(history[0].currentValue).toBe("Jane Doe");
|
|
249
|
+
expect(typeof json.id).toBe("string");
|
|
293
250
|
});
|
|
294
251
|
});
|
|
295
252
|
});
|
|
296
|
-
|
|
297
|
-
// ============================================================================
|
|
298
|
-
// Example: Value Object with Validation
|
|
299
|
-
// ============================================================================
|
|
300
|
-
|
|
301
|
-
describe("Value Object", () => {
|
|
302
|
-
it("should be immutable", () => {
|
|
303
|
-
const address = new Address({
|
|
304
|
-
street: "123 Main St",
|
|
305
|
-
city: "New York",
|
|
306
|
-
zipCode: "10001",
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
expect(address.street).toBe("123 Main St");
|
|
310
|
-
|
|
311
|
-
// Props are frozen
|
|
312
|
-
expect(() => {
|
|
313
|
-
(address as any).props.street = "New Street";
|
|
314
|
-
}).toThrow();
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
it("should compare by value", () => {
|
|
318
|
-
const address1 = new Address({
|
|
319
|
-
street: "123 Main St",
|
|
320
|
-
city: "New York",
|
|
321
|
-
zipCode: "10001",
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
const address2 = new Address({
|
|
325
|
-
street: "123 Main St",
|
|
326
|
-
city: "New York",
|
|
327
|
-
zipCode: "10001",
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
const address3 = new Address({
|
|
331
|
-
street: "456 Oak Ave",
|
|
332
|
-
city: "Boston",
|
|
333
|
-
zipCode: "02101",
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
expect(address1.equals(address2)).toBe(true);
|
|
337
|
-
expect(address1.equals(address3)).toBe(false);
|
|
338
|
-
});
|
|
339
|
-
});
|