@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.
Files changed (105) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/aggregate-changes.d.ts +164 -0
  3. package/dist/aggregate-changes.d.ts.map +1 -0
  4. package/dist/aggregate-changes.js +281 -0
  5. package/dist/aggregate-changes.js.map +1 -0
  6. package/dist/base-entity.d.ts +32 -8
  7. package/dist/base-entity.d.ts.map +1 -1
  8. package/dist/base-entity.js +117 -86
  9. package/dist/base-entity.js.map +1 -1
  10. package/dist/criteria.d.ts +3 -3
  11. package/dist/criteria.d.ts.map +1 -1
  12. package/dist/criteria.js.map +1 -1
  13. package/dist/crypto.d.ts +3 -0
  14. package/dist/crypto.d.ts.map +1 -0
  15. package/dist/crypto.js +29 -0
  16. package/dist/crypto.js.map +1 -0
  17. package/dist/entity-changes.d.ts +84 -0
  18. package/dist/entity-changes.d.ts.map +1 -0
  19. package/dist/entity-changes.js +135 -0
  20. package/dist/entity-changes.js.map +1 -0
  21. package/dist/entity-schema-registry.d.ts +148 -0
  22. package/dist/entity-schema-registry.d.ts.map +1 -0
  23. package/dist/entity-schema-registry.js +219 -0
  24. package/dist/entity-schema-registry.js.map +1 -0
  25. package/dist/history-tracker.d.ts +97 -0
  26. package/dist/history-tracker.d.ts.map +1 -0
  27. package/dist/history-tracker.js +805 -0
  28. package/dist/history-tracker.js.map +1 -0
  29. package/dist/id.d.ts +11 -10
  30. package/dist/id.d.ts.map +1 -1
  31. package/dist/id.js +4 -28
  32. package/dist/id.js.map +1 -1
  33. package/dist/index.d.ts +1 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +1 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/mapper.d.ts +1 -1
  38. package/dist/mapper.d.ts.map +1 -1
  39. package/dist/mapper.js.map +1 -1
  40. package/dist/repository/base-repository.d.ts +6 -32
  41. package/dist/repository/base-repository.d.ts.map +1 -1
  42. package/dist/repository/base-repository.js +0 -27
  43. package/dist/repository/base-repository.js.map +1 -1
  44. package/dist/repository/unit-of-work.d.ts +0 -25
  45. package/dist/repository/unit-of-work.d.ts.map +1 -1
  46. package/dist/repository/unit-of-work.js +0 -25
  47. package/dist/repository/unit-of-work.js.map +1 -1
  48. package/dist/types/change-tracker.d.ts +186 -0
  49. package/dist/types/change-tracker.d.ts.map +1 -0
  50. package/dist/types/change-tracker.js +2 -0
  51. package/dist/types/change-tracker.js.map +1 -0
  52. package/dist/types/criteria.d.ts +5 -1
  53. package/dist/types/criteria.d.ts.map +1 -1
  54. package/dist/types/history-tracker.d.ts +11 -0
  55. package/dist/types/history-tracker.d.ts.map +1 -1
  56. package/dist/types/utils.d.ts +0 -1
  57. package/dist/types/utils.d.ts.map +1 -1
  58. package/dist/validation-error.d.ts.map +1 -1
  59. package/dist/validation-error.js +0 -3
  60. package/dist/validation-error.js.map +1 -1
  61. package/dist/value-object.d.ts +57 -8
  62. package/dist/value-object.d.ts.map +1 -1
  63. package/dist/value-object.js +49 -21
  64. package/dist/value-object.js.map +1 -1
  65. package/package.json +2 -1
  66. package/src/aggregate-changes.ts +335 -0
  67. package/src/base-entity.ts +140 -100
  68. package/src/criteria.ts +2 -1
  69. package/src/crypto.ts +31 -0
  70. package/src/entity-changes.ts +151 -0
  71. package/src/entity-schema-registry.ts +275 -0
  72. package/src/history-tracker.ts +1114 -0
  73. package/src/id.ts +17 -26
  74. package/src/index.ts +1 -0
  75. package/src/mapper.ts +4 -1
  76. package/src/repository/base-repository.ts +6 -37
  77. package/src/repository/unit-of-work.ts +0 -25
  78. package/src/types/change-tracker.ts +221 -0
  79. package/src/types/criteria.ts +6 -1
  80. package/src/types/history-tracker.ts +13 -0
  81. package/src/types/utils.ts +0 -9
  82. package/src/validation-error.ts +0 -4
  83. package/src/value-object.ts +84 -23
  84. package/tests/aggregate-changes.test.ts +284 -0
  85. package/tests/criteria.test.ts +122 -161
  86. package/tests/entity-equality.test.ts +38 -61
  87. package/tests/entity-schema-registry.test.ts +382 -0
  88. package/tests/entity-validation.test.ts +7 -94
  89. package/tests/history-tracker.spec.ts +349 -617
  90. package/tests/id.test.ts +41 -44
  91. package/tests/load-test/data.json +346041 -0
  92. package/tests/load-test/entities.ts +97 -0
  93. package/tests/load-test/generate-data.ts +81 -0
  94. package/tests/load-test/lead-to-domain.mapper.ts +24 -0
  95. package/tests/load-test/load.test.ts +38 -0
  96. package/tests/repository.test.ts +30 -54
  97. package/tests/to-json.test.ts +14 -18
  98. package/tests/utils.ts +138 -102
  99. package/tests/value-objects.test.ts +57 -29
  100. package/dist/deep-proxy.d.ts +0 -36
  101. package/dist/deep-proxy.d.ts.map +0 -1
  102. package/dist/deep-proxy.js +0 -384
  103. package/dist/deep-proxy.js.map +0 -1
  104. package/src/deep-proxy.ts +0 -447
  105. 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; // Block the update
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, // Does not throw, stores errors internally
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", // Too short
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"; // Too short
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"; // Blocked by custom rule
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"); // Id converted to 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
- });