@woltz/rich-domain 1.3.0 → 1.3.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.
Files changed (76) hide show
  1. package/dist/aggregate-changes.js +1 -1
  2. package/dist/aggregate-changes.js.map +1 -1
  3. package/dist/base-entity.d.ts.map +1 -1
  4. package/dist/base-entity.js +17 -5
  5. package/dist/base-entity.js.map +1 -1
  6. package/dist/change-tracker.d.ts +1 -1
  7. package/dist/change-tracker.d.ts.map +1 -1
  8. package/dist/change-tracker.js +20 -8
  9. package/dist/change-tracker.js.map +1 -1
  10. package/dist/criteria.js +6 -5
  11. package/dist/criteria.js.map +1 -1
  12. package/dist/domain-event-bus.js +4 -4
  13. package/dist/domain-event-bus.js.map +1 -1
  14. package/dist/domain-event.js +3 -0
  15. package/dist/domain-event.js.map +1 -1
  16. package/dist/entity-changes.js +1 -0
  17. package/dist/entity-changes.js.map +1 -1
  18. package/dist/entity-schema-registry.d.ts +4 -0
  19. package/dist/entity-schema-registry.d.ts.map +1 -1
  20. package/dist/entity-schema-registry.js +8 -6
  21. package/dist/entity-schema-registry.js.map +1 -1
  22. package/dist/exceptions.js +26 -1
  23. package/dist/exceptions.js.map +1 -1
  24. package/dist/id.js +2 -0
  25. package/dist/id.js.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js.map +1 -1
  29. package/dist/paginated-result.d.ts.map +1 -1
  30. package/dist/paginated-result.js +9 -0
  31. package/dist/paginated-result.js.map +1 -1
  32. package/dist/repository/unit-of-work.js +3 -7
  33. package/dist/repository/unit-of-work.js.map +1 -1
  34. package/dist/types/domain.d.ts +0 -1
  35. package/dist/types/domain.d.ts.map +1 -1
  36. package/dist/validation-error.d.ts +15 -1
  37. package/dist/validation-error.d.ts.map +1 -1
  38. package/dist/validation-error.js +46 -3
  39. package/dist/validation-error.js.map +1 -1
  40. package/dist/value-object.d.ts.map +1 -1
  41. package/dist/value-object.js +29 -1
  42. package/dist/value-object.js.map +1 -1
  43. package/package.json +9 -11
  44. package/eslint.config.js +0 -57
  45. package/jest.config.js +0 -21
  46. package/src/aggregate-changes.ts +0 -444
  47. package/src/base-entity.ts +0 -404
  48. package/src/change-tracker.ts +0 -1133
  49. package/src/constants.ts +0 -81
  50. package/src/criteria.ts +0 -521
  51. package/src/crypto.ts +0 -31
  52. package/src/domain-event-bus.ts +0 -152
  53. package/src/domain-event.ts +0 -49
  54. package/src/entity-changes.ts +0 -146
  55. package/src/entity-schema-registry.ts +0 -502
  56. package/src/entity.ts +0 -5
  57. package/src/exceptions.ts +0 -435
  58. package/src/id.ts +0 -98
  59. package/src/index.ts +0 -52
  60. package/src/mapper.ts +0 -6
  61. package/src/paginated-result.ts +0 -238
  62. package/src/repository/base-repository.ts +0 -33
  63. package/src/repository/index.ts +0 -3
  64. package/src/repository/unit-of-work.ts +0 -76
  65. package/src/types/change-tracker.ts +0 -264
  66. package/src/types/criteria.ts +0 -159
  67. package/src/types/domain-event.ts +0 -38
  68. package/src/types/domain.ts +0 -34
  69. package/src/types/index.ts +0 -7
  70. package/src/types/standard-schema.ts +0 -19
  71. package/src/types/unit-of-work.ts +0 -46
  72. package/src/types/utils.ts +0 -20
  73. package/src/utils/criteria-operator-validation.ts +0 -209
  74. package/src/utils/helpers.ts +0 -34
  75. package/src/validation-error.ts +0 -91
  76. package/src/value-object.ts +0 -244
@@ -1,244 +0,0 @@
1
- import { ValidationError } from "./validation-error";
2
- import { IDomainEvent } from ".";
3
- import {
4
- VOHooks,
5
- ValidationConfig,
6
- StandardSchema,
7
- EntityValidation,
8
- } from "./types";
9
- import { DEFAULT_VALIDATION_CONFIG } from "./constants";
10
- import { DomainError } from "./exceptions";
11
-
12
- function getStaticProperty<T>(
13
- instance: any,
14
- propertyName: string
15
- ): T | undefined {
16
- return instance.constructor[propertyName];
17
- }
18
-
19
- /**
20
- * Identity key type for a Value Object. Can be a single key or an array of keys (composite key).
21
- */
22
- export type IdentityKeyDefinition<T> = (keyof T)[] | keyof T;
23
-
24
- export abstract class ValueObject<T> {
25
- protected readonly props!: T;
26
- private validationConfig: Required<ValidationConfig>;
27
- private domainHooks?: VOHooks<T, any>;
28
- private domainSchema?: StandardSchema<T>;
29
- private domainEvents: IDomainEvent[] = [];
30
-
31
- protected static validation?: EntityValidation<any>;
32
- protected static hooks?: VOHooks<any, any>;
33
-
34
- /**
35
- * Identity key for identification in collections.
36
- * Used by ChangeTracker to track changes in arrays of Value Objects.
37
- *
38
- * @example
39
- * ```typescript
40
- * // Simple key
41
- * class TagReference extends ValueObject<{ tagId: string }> {
42
- * static readonly identityKey = 'tagId';
43
- * }
44
- *
45
- * // Composite key
46
- * class Like extends ValueObject<{ postId: string; userId: string }> {
47
- * static readonly identityKey = ['postId', 'userId'];
48
- * }
49
- * ```
50
- */
51
- protected static identityKey?: IdentityKeyDefinition<any>;
52
-
53
- constructor(props: T) {
54
- const validation = getStaticProperty<EntityValidation<T>>(
55
- this,
56
- "validation"
57
- );
58
- const hooks = getStaticProperty<VOHooks<T, any>>(this, "hooks");
59
-
60
- this.domainHooks = hooks;
61
-
62
- if (validation?.schema) {
63
- this.domainSchema = validation.schema;
64
- }
65
-
66
- this.validationConfig = {
67
- ...DEFAULT_VALIDATION_CONFIG,
68
- ...validation?.config,
69
- };
70
-
71
- let finalProps = { ...props } as T;
72
-
73
- if (this.domainSchema && this.validationConfig.onCreate) {
74
- this.validateProps(finalProps);
75
- }
76
-
77
- (this as any).props = finalProps;
78
-
79
- if (hooks?.rules) {
80
- hooks.rules(this as any);
81
- }
82
-
83
- Object.freeze(this.props);
84
-
85
- if (hooks?.onCreate) {
86
- hooks.onCreate(this as any);
87
- }
88
- }
89
-
90
- private validateProps(props: T): void {
91
- if (!this.domainSchema) return;
92
-
93
- const result = this.domainSchema["~standard"].validate(props);
94
-
95
- if (result instanceof Promise) {
96
- throw new DomainError(
97
- "Async validation not supported in constructor. Use sync validation schema."
98
- );
99
- }
100
-
101
- if (result.issues && result.issues.length > 0) {
102
- const validationError = new ValidationError(
103
- result.issues.map((issue) => ({
104
- path: issue.path?.map((p) => this.extractPathKey(p)) || [],
105
- message: issue.message,
106
- }))
107
- );
108
-
109
- if (this.validationConfig.throwOnError) {
110
- throw validationError;
111
- }
112
-
113
- (this as any)._validationError = validationError;
114
- }
115
- }
116
-
117
- private extractPathKey(pathSegment: unknown): string {
118
- if (pathSegment === null || pathSegment === undefined) {
119
- return "";
120
- }
121
- if (typeof pathSegment === "string" || typeof pathSegment === "number") {
122
- return String(pathSegment);
123
- }
124
- if (typeof pathSegment === "symbol") {
125
- return pathSegment.toString();
126
- }
127
- if (typeof pathSegment === "object" && "key" in pathSegment) {
128
- return String((pathSegment as { key: unknown }).key);
129
- }
130
- return String(pathSegment);
131
- }
132
-
133
- /**
134
- * Returns true if the value object has validation errors (when throwOnError is false).
135
- */
136
- get hasValidationErrors(): boolean {
137
- return !!(this as any)._validationError;
138
- }
139
-
140
- /**
141
- * Returns the validation errors (when throwOnError is false).
142
- */
143
- get validationErrors(): ValidationError | undefined {
144
- return (this as any)._validationError;
145
- }
146
-
147
- /**
148
- * Compare this ValueObject with another for equality based on their properties.
149
- */
150
- equals(other: ValueObject<T>): boolean {
151
- if (!other || !(other instanceof ValueObject)) return false;
152
- return JSON.stringify(this.props) === JSON.stringify(other.props);
153
- }
154
-
155
- /**
156
- * Returns the identity key for this Value Object.
157
- * Used for identification in collections when identityKey is set.
158
- *
159
- * @returns String with the identity key or null if not defined
160
- *
161
- * @example
162
- * ```typescript
163
- * const like = new Like({ postId: 'p1', userId: 'u1' });
164
- * like.getIdentityKey(); // 'p1:u1'
165
- *
166
- * const tag = new TagReference({ tagId: 'tag-123' });
167
- * tag.getIdentityKey(); // 'tag-123'
168
- * ```
169
- */
170
- getIdentityKey(): string | null {
171
- const keyDef = getStaticProperty<IdentityKeyDefinition<T>>(
172
- this,
173
- "identityKey"
174
- );
175
-
176
- if (!keyDef) {
177
- return null;
178
- }
179
-
180
- if (Array.isArray(keyDef)) {
181
- return keyDef.map((k) => String(this.props[k])).join(":");
182
- }
183
-
184
- return String(this.props[keyDef]);
185
- }
186
-
187
- /**
188
- * Returns true if this Value Object has an identity key defined.
189
- */
190
- hasIdentityKey(): boolean {
191
- return (
192
- getStaticProperty<IdentityKeyDefinition<T>>(this, "identityKey") !==
193
- undefined
194
- );
195
- }
196
-
197
- /**
198
- * Returns the identity key definition (if any).
199
- */
200
- static getIdentityKeyDefinition<P>(): IdentityKeyDefinition<P> | undefined {
201
- return (this as any).identityKey;
202
- }
203
-
204
- /**
205
- * Adds a domain event to this value object.
206
- */
207
- protected addDomainEvent(event: IDomainEvent): void {
208
- this.domainEvents.push(event);
209
- }
210
-
211
- /**
212
- * Returns all uncommitted domain events.
213
- */
214
- getUncommittedEvents(): IDomainEvent[] {
215
- return [...this.domainEvents];
216
- }
217
-
218
- /**
219
- * Clears all domain events (call after publishing).
220
- */
221
- clearEvents(): void {
222
- this.domainEvents = [];
223
- }
224
-
225
- /**
226
- * Returns true if the value object has uncommitted events.
227
- */
228
- hasUncommittedEvents(): boolean {
229
- return this.domainEvents.length > 0;
230
- }
231
-
232
- toJSON(): T {
233
- return { ...this.props };
234
- }
235
-
236
- /**
237
- * Creates a new ValueObject with updated properties.
238
- * ValueObjects are immutable, so this returns a new instance.
239
- */
240
- protected clone(updates: Partial<T>): this {
241
- const Constructor = this.constructor as new (props: T) => this;
242
- return new Constructor({ ...this.props, ...updates });
243
- }
244
- }