@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
@@ -0,0 +1,275 @@
1
+ import { Entity } from "./entity";
2
+ import { ValueObject } from "./value-object";
3
+ import { Id } from "./id";
4
+
5
+ /**
6
+ * Mapping schema for a domain entity.
7
+ */
8
+ export interface EntitySchema {
9
+ /** Entity name in the domain (e.g., 'User', 'Post') */
10
+ entity: string;
11
+
12
+ /** Table name in the database (e.g., 'users', 'blog_posts') */
13
+ table: string;
14
+
15
+ /**
16
+ * Field mapping: domain → database.
17
+ * Only include fields with different names.
18
+ * @example { email: 'user_email', createdAt: 'created_at' }
19
+ */
20
+ fields?: Record<string, string>;
21
+
22
+ /**
23
+ * FK configuration for parent relation.
24
+ */
25
+ parentFk?: {
26
+ /** Name of the FK field in the database (e.g., 'author_id') */
27
+ field: string;
28
+ /** Name of the parent entity (e.g., 'User') */
29
+ parentEntity: string;
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Result of entity mapping.
35
+ */
36
+ export interface MappedEntityData {
37
+ [key: string]: any;
38
+ }
39
+
40
+ /**
41
+ * Registry for mapping domain entities to database tables and fields.
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const registry = new EntitySchemaRegistry()
46
+ * .register({
47
+ * entity: 'User',
48
+ * table: 'users',
49
+ * fields: { email: 'user_email', name: 'user_name' },
50
+ * })
51
+ * .register({
52
+ * entity: 'Post',
53
+ * table: 'blog_posts',
54
+ * fields: { content: 'post_content' },
55
+ * parentFk: { field: 'author_id', parentEntity: 'User' },
56
+ * });
57
+ *
58
+ * const table = registry.getTable('Post'); // 'blog_posts'
59
+ * const mapped = registry.mapFields('User', { email: 'test@test.com' });
60
+ * // { user_email: 'test@test.com' }
61
+ * ```
62
+ */
63
+ export class EntitySchemaRegistry {
64
+ private schemas = new Map<string, EntitySchema>();
65
+
66
+ /**
67
+ * Registers an entity schema.
68
+ * @param schema - Schema to be registered.
69
+ * @returns this (for chaining)
70
+ */
71
+ register(schema: EntitySchema): this {
72
+ if (this.schemas.has(schema.entity)) {
73
+ console.warn(
74
+ `EntitySchemaRegistry: Schema for '${schema.entity}' is being overwritten`
75
+ );
76
+ }
77
+ this.schemas.set(schema.entity, schema);
78
+ return this;
79
+ }
80
+
81
+ /**
82
+ * Registers multiple schemas at once.
83
+ * @param schemas - Array of schemas.
84
+ * @returns this (for chaining)
85
+ */
86
+ registerAll(schemas: EntitySchema[]): this {
87
+ schemas.forEach((schema) => this.register(schema));
88
+ return this;
89
+ }
90
+
91
+ /**
92
+ * Gets the schema of an entity.
93
+ * @param entity - Entity name.
94
+ * @throws Error if the entity is not registered.
95
+ */
96
+ getSchema(entity: string): EntitySchema {
97
+ const schema = this.schemas.get(entity);
98
+ if (!schema) {
99
+ throw new Error(
100
+ `EntitySchemaRegistry: No schema registered for entity '${entity}'. ` +
101
+ `Available entities: ${Array.from(this.schemas.keys()).join(", ") || "none"}`
102
+ );
103
+ }
104
+ return schema;
105
+ }
106
+
107
+ /**
108
+ * Checks if an entity is registered.
109
+ * @param entity - Entity name.
110
+ */
111
+ has(entity: string): boolean {
112
+ return this.schemas.has(entity);
113
+ }
114
+
115
+ /**
116
+ * Gets the table name for an entity.
117
+ * @param entity - Entity name.
118
+ */
119
+ getTable(entity: string): string {
120
+ return this.getSchema(entity).table;
121
+ }
122
+
123
+ /**
124
+ * Gets the field mapping for an entity.
125
+ * @param entity - Entity name.
126
+ */
127
+ getFieldsMap(entity: string): Record<string, string> {
128
+ return this.getSchema(entity).fields || {};
129
+ }
130
+
131
+ /**
132
+ * Maps a domain field name to the database field name.
133
+ * @param entity - Entity name.
134
+ * @param fieldName - Domain field name.
135
+ */
136
+ mapFieldName(entity: string, fieldName: string): string {
137
+ const fields = this.getFieldsMap(entity);
138
+ return fields[fieldName] ?? fieldName;
139
+ }
140
+
141
+ /**
142
+ * Maps fields of a domain object to database field names.
143
+ * Ignores values that are Entity, ValueObject, or Arrays.
144
+ *
145
+ * @param entity - Entity name.
146
+ * @param data - Data to be mapped.
147
+ */
148
+ mapFields(entity: string, data: Record<string, any>): MappedEntityData {
149
+ const fields = this.getFieldsMap(entity);
150
+ const result: MappedEntityData = {};
151
+
152
+ for (const [key, value] of Object.entries(data)) {
153
+ if (this.isEntityOrCollection(value)) {
154
+ continue;
155
+ }
156
+ const mappedKey = fields[key] ?? key;
157
+ result[mappedKey] = this.normalizeValue(value);
158
+ }
159
+
160
+ return result;
161
+ }
162
+
163
+ /**
164
+ * Maps a complete domain entity to database data.
165
+ * Used for CREATE operations.
166
+ *
167
+ * @param entity - Entity name.
168
+ * @param domainEntity - Domain entity instance.
169
+ */
170
+ mapEntity(
171
+ entity: string,
172
+ domainEntity: Entity<any> | ValueObject<any>
173
+ ): MappedEntityData {
174
+ const fields = this.getFieldsMap(entity);
175
+ const result: MappedEntityData = {};
176
+
177
+ // Map ID if it is an Entity or has entity-like structure
178
+ const hasId = (domainEntity as any).id;
179
+ if (hasId) {
180
+ // Extract id value
181
+ const idValue = hasId.value ?? hasId;
182
+ result["id"] = idValue;
183
+ }
184
+
185
+ // Get props
186
+ const props = (domainEntity as any).props || domainEntity;
187
+
188
+ for (const [key, value] of Object.entries(props)) {
189
+ if (key === "id") continue; // ID already mapped
190
+ if (this.isEntityOrCollection(value)) continue;
191
+
192
+ const mappedKey = fields[key] ?? key;
193
+ result[mappedKey] = this.normalizeValue(value);
194
+ }
195
+
196
+ return result;
197
+ }
198
+
199
+ /**
200
+ * Gets the FK object to relate with the parent.
201
+ *
202
+ * @param entity - Entity name.
203
+ * @param parentId - Parent ID.
204
+ * @returns Object with the FK field or null if there is no parent.
205
+ */
206
+ getParentFk(entity: string, parentId: string): Record<string, string> | null {
207
+ const schema = this.getSchema(entity);
208
+ if (!schema.parentFk) return null;
209
+
210
+ return { [schema.parentFk.field]: parentId };
211
+ }
212
+
213
+ /**
214
+ * Gets the name of the parent entity.
215
+ * @param entity - Entity name.
216
+ */
217
+ getParentEntity(entity: string): string | null {
218
+ const schema = this.getSchema(entity);
219
+ return schema.parentFk?.parentEntity ?? null;
220
+ }
221
+
222
+ /**
223
+ * Gets the FK field name.
224
+ * @param entity - Entity name.
225
+ */
226
+ getParentFkField(entity: string): string | null {
227
+ const schema = this.getSchema(entity);
228
+ return schema.parentFk?.field ?? null;
229
+ }
230
+
231
+ /**
232
+ * Lists all registered entities.
233
+ */
234
+ getRegisteredEntities(): string[] {
235
+ return Array.from(this.schemas.keys());
236
+ }
237
+
238
+ /**
239
+ * Clears all registered schemas.
240
+ */
241
+ clear(): void {
242
+ this.schemas.clear();
243
+ }
244
+
245
+ /**
246
+ * Checks if a value is Entity, ValueObject, or Array.
247
+ */
248
+ private isEntityOrCollection(value: any): boolean {
249
+ if (value === null || value === undefined) return false;
250
+ if (Array.isArray(value)) return true;
251
+ if (value instanceof Entity) return true;
252
+ if (value instanceof ValueObject) return true;
253
+
254
+ // Checks if it has the structure of an Entity (object with 'id' that has 'value')
255
+ if (typeof value === 'object' && value.id && typeof value.id === 'object' && 'value' in value.id) {
256
+ return true;
257
+ }
258
+
259
+ return false;
260
+ }
261
+
262
+ /**
263
+ * Normalizes a value for persistence.
264
+ */
265
+ private normalizeValue(value: any): any {
266
+ if (value === null || value === undefined) return value;
267
+ if (value instanceof Id) return value.value;
268
+ if (value instanceof Date) return value;
269
+ if (typeof value === "object" && "value" in value) {
270
+ // Might be an ID or other wrapper
271
+ return value.value;
272
+ }
273
+ return value;
274
+ }
275
+ }