@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.
- package/dist/aggregate-changes.js +1 -1
- package/dist/aggregate-changes.js.map +1 -1
- package/dist/base-entity.d.ts.map +1 -1
- package/dist/base-entity.js +17 -5
- package/dist/base-entity.js.map +1 -1
- package/dist/change-tracker.d.ts +1 -1
- package/dist/change-tracker.d.ts.map +1 -1
- package/dist/change-tracker.js +20 -8
- package/dist/change-tracker.js.map +1 -1
- package/dist/criteria.js +6 -5
- package/dist/criteria.js.map +1 -1
- package/dist/domain-event-bus.js +4 -4
- package/dist/domain-event-bus.js.map +1 -1
- package/dist/domain-event.js +3 -0
- package/dist/domain-event.js.map +1 -1
- package/dist/entity-changes.js +1 -0
- package/dist/entity-changes.js.map +1 -1
- package/dist/entity-schema-registry.d.ts +4 -0
- package/dist/entity-schema-registry.d.ts.map +1 -1
- package/dist/entity-schema-registry.js +8 -6
- package/dist/entity-schema-registry.js.map +1 -1
- package/dist/exceptions.js +26 -1
- package/dist/exceptions.js.map +1 -1
- package/dist/id.js +2 -0
- package/dist/id.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/paginated-result.d.ts.map +1 -1
- package/dist/paginated-result.js +9 -0
- package/dist/paginated-result.js.map +1 -1
- package/dist/repository/unit-of-work.js +3 -7
- package/dist/repository/unit-of-work.js.map +1 -1
- package/dist/types/domain.d.ts +0 -1
- package/dist/types/domain.d.ts.map +1 -1
- package/dist/validation-error.d.ts +15 -1
- package/dist/validation-error.d.ts.map +1 -1
- package/dist/validation-error.js +46 -3
- package/dist/validation-error.js.map +1 -1
- package/dist/value-object.d.ts.map +1 -1
- package/dist/value-object.js +29 -1
- package/dist/value-object.js.map +1 -1
- package/package.json +9 -11
- package/eslint.config.js +0 -57
- package/jest.config.js +0 -21
- package/src/aggregate-changes.ts +0 -444
- package/src/base-entity.ts +0 -404
- package/src/change-tracker.ts +0 -1133
- package/src/constants.ts +0 -81
- package/src/criteria.ts +0 -521
- package/src/crypto.ts +0 -31
- package/src/domain-event-bus.ts +0 -152
- package/src/domain-event.ts +0 -49
- package/src/entity-changes.ts +0 -146
- package/src/entity-schema-registry.ts +0 -502
- package/src/entity.ts +0 -5
- package/src/exceptions.ts +0 -435
- package/src/id.ts +0 -98
- package/src/index.ts +0 -52
- package/src/mapper.ts +0 -6
- package/src/paginated-result.ts +0 -238
- package/src/repository/base-repository.ts +0 -33
- package/src/repository/index.ts +0 -3
- package/src/repository/unit-of-work.ts +0 -76
- package/src/types/change-tracker.ts +0 -264
- package/src/types/criteria.ts +0 -159
- package/src/types/domain-event.ts +0 -38
- package/src/types/domain.ts +0 -34
- package/src/types/index.ts +0 -7
- package/src/types/standard-schema.ts +0 -19
- package/src/types/unit-of-work.ts +0 -46
- package/src/types/utils.ts +0 -20
- package/src/utils/criteria-operator-validation.ts +0 -209
- package/src/utils/helpers.ts +0 -34
- package/src/validation-error.ts +0 -91
- package/src/value-object.ts +0 -244
|
@@ -1,502 +0,0 @@
|
|
|
1
|
-
import { Entity } from "./entity";
|
|
2
|
-
import { ValueObject } from "./value-object";
|
|
3
|
-
import { Id } from "./id";
|
|
4
|
-
import { ConfigurationError } from "./exceptions";
|
|
5
|
-
import { levenshteinDistance } from "./utils/helpers";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Type of collection relationship.
|
|
9
|
-
* - 'owned': Parent owns the children (1:N). Delete parent = delete children.
|
|
10
|
-
* - 'reference': Parent references existing entities (N:N). Delete parent = unlink only.
|
|
11
|
-
*/
|
|
12
|
-
export type CollectionType = "owned" | "reference";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Configuration for a collection (1:N or N:N relationship).
|
|
16
|
-
*/
|
|
17
|
-
export interface CollectionConfig {
|
|
18
|
-
/**
|
|
19
|
-
* Type of relationship.
|
|
20
|
-
* - 'owned': Children are created/deleted with the parent (default for 1:N)
|
|
21
|
-
* - 'reference': Only the link is created/removed (for N:N)
|
|
22
|
-
* @default 'owned'
|
|
23
|
-
*/
|
|
24
|
-
type: CollectionType;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Target entity name (required for 'reference' type).
|
|
28
|
-
* @example 'Tag'
|
|
29
|
-
*/
|
|
30
|
-
entity?: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Junction table configuration (optional, for ORMs that need it like Drizzle).
|
|
34
|
-
* Prisma handles this automatically, so it's optional.
|
|
35
|
-
*/
|
|
36
|
-
junction?: {
|
|
37
|
-
/** Junction table name (e.g., 'post_tags', '_PostToTag') */
|
|
38
|
-
table: string;
|
|
39
|
-
/** FK field pointing to the source entity (e.g., 'post_id') */
|
|
40
|
-
sourceKey: string;
|
|
41
|
-
/** FK field pointing to the target entity (e.g., 'tag_id') */
|
|
42
|
-
targetKey: string;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Mapping schema for a domain entity.
|
|
48
|
-
*/
|
|
49
|
-
export interface EntitySchema {
|
|
50
|
-
/** Entity name in the domain (e.g., 'User', 'Post') */
|
|
51
|
-
entity: string;
|
|
52
|
-
/** Table name in the database (e.g., 'users', 'blog_posts') */
|
|
53
|
-
table: string;
|
|
54
|
-
/**
|
|
55
|
-
* Field mapping: domain → database.
|
|
56
|
-
* Only include fields with different names.
|
|
57
|
-
* @example { email: 'user_email', createdAt: 'created_at' }
|
|
58
|
-
*/
|
|
59
|
-
fields?: Record<string, string>;
|
|
60
|
-
/**
|
|
61
|
-
* FK configuration for parent relation (1:N owned).
|
|
62
|
-
*/
|
|
63
|
-
parentFk?: {
|
|
64
|
-
/** Name of the FK field in the database (e.g., 'author_id') */
|
|
65
|
-
field: string;
|
|
66
|
-
/** Name of the parent entity (e.g., 'User') */
|
|
67
|
-
parentEntity: string;
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Collection configurations for this entity's relations.
|
|
71
|
-
* Key is the property name in the domain entity.
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* collections: {
|
|
75
|
-
* comments: { type: 'owned' },
|
|
76
|
-
* tags: { type: 'reference', entity: 'Tag' }
|
|
77
|
-
* }
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
collections?: Record<string, CollectionConfig>;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Result of entity mapping.
|
|
85
|
-
*/
|
|
86
|
-
export interface MappedEntityData {
|
|
87
|
-
[key: string]: any;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Registry for mapping domain entities to database tables and fields.
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```typescript
|
|
95
|
-
* const registry = new EntitySchemaRegistry()
|
|
96
|
-
* .register({
|
|
97
|
-
* entity: 'User',
|
|
98
|
-
* table: 'users',
|
|
99
|
-
* fields: { email: 'user_email', name: 'user_name' },
|
|
100
|
-
* })
|
|
101
|
-
* .register({
|
|
102
|
-
* entity: 'Post',
|
|
103
|
-
* table: 'blog_posts',
|
|
104
|
-
* fields: { content: 'post_content' },
|
|
105
|
-
* parentFk: { field: 'author_id', parentEntity: 'User' },
|
|
106
|
-
* collections: {
|
|
107
|
-
* comments: { type: 'owned' },
|
|
108
|
-
* tags: { type: 'reference', entity: 'Tag' }
|
|
109
|
-
* }
|
|
110
|
-
* });
|
|
111
|
-
*
|
|
112
|
-
* const table = registry.getTable('Post'); // 'blog_posts'
|
|
113
|
-
* const tagConfig = registry.getCollectionConfig('Post', 'tags');
|
|
114
|
-
* // { type: 'reference', entity: 'Tag' }
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
|
-
export class EntitySchemaRegistry {
|
|
118
|
-
private schemas = new Map<string, EntitySchema>();
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Registers an entity schema.
|
|
122
|
-
* @param schema - Schema to be registered.
|
|
123
|
-
* @returns this (for chaining)
|
|
124
|
-
*/
|
|
125
|
-
register(schema: EntitySchema): this {
|
|
126
|
-
if (this.schemas.has(schema.entity)) {
|
|
127
|
-
console.warn(
|
|
128
|
-
`EntitySchemaRegistry: Schema for '${schema.entity}' is being overwritten`
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
this.schemas.set(schema.entity, schema);
|
|
132
|
-
return this;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Registers multiple schemas at once.
|
|
137
|
-
* @param schemas - Array of schemas.
|
|
138
|
-
* @returns this (for chaining)
|
|
139
|
-
*/
|
|
140
|
-
registerAll(schemas: EntitySchema[]): this {
|
|
141
|
-
schemas.forEach((schema) => this.register(schema));
|
|
142
|
-
return this;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Gets the schema of an entity.
|
|
147
|
-
* @param entity - Entity name.
|
|
148
|
-
* @throws Error if the entity is not registered.
|
|
149
|
-
*/
|
|
150
|
-
getSchema(entity: string): EntitySchema {
|
|
151
|
-
const schema = this.schemas.get(entity);
|
|
152
|
-
if (!schema) {
|
|
153
|
-
throw new ConfigurationError(
|
|
154
|
-
`EntitySchemaRegistry: No schema registered for entity '${entity}'. ` +
|
|
155
|
-
`Available entities: ${
|
|
156
|
-
Array.from(this.schemas.keys()).join(", ") || "none"
|
|
157
|
-
}`
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
return schema;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Tries to get the schema of an entity, returns null if not found.
|
|
165
|
-
* @param entity - Entity name.
|
|
166
|
-
*/
|
|
167
|
-
tryGetSchema(entity: string): EntitySchema | null {
|
|
168
|
-
return this.schemas.get(entity) ?? null;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Checks if an entity is registered.
|
|
173
|
-
* @param entity - Entity name.
|
|
174
|
-
*/
|
|
175
|
-
has(entity: string): boolean {
|
|
176
|
-
return this.schemas.has(entity);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Gets the table name for an entity.
|
|
181
|
-
* @param entity - Entity name.
|
|
182
|
-
*/
|
|
183
|
-
getTable(entity: string): string {
|
|
184
|
-
return this.getSchema(entity).table;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Gets the field mapping for an entity.
|
|
189
|
-
* @param entity - Entity name.
|
|
190
|
-
*/
|
|
191
|
-
getFieldsMap(entity: string): Record<string, string> {
|
|
192
|
-
return this.getSchema(entity).fields || {};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Maps a domain field name to the database field name.
|
|
197
|
-
* @param entity - Entity name.
|
|
198
|
-
* @param fieldName - Domain field name.
|
|
199
|
-
*/
|
|
200
|
-
mapFieldName(entity: string, fieldName: string): string {
|
|
201
|
-
const fields = this.getFieldsMap(entity);
|
|
202
|
-
return fields[fieldName] ?? fieldName;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Maps fields of a domain object to database field names.
|
|
207
|
-
* Ignores values that are Entity, ValueObject, or Arrays.
|
|
208
|
-
*
|
|
209
|
-
* @param entity - Entity name.
|
|
210
|
-
* @param data - Data to be mapped.
|
|
211
|
-
*/
|
|
212
|
-
mapFields(entity: string, data: Record<string, any>): MappedEntityData {
|
|
213
|
-
const fields = this.getFieldsMap(entity);
|
|
214
|
-
const result: MappedEntityData = {};
|
|
215
|
-
for (const [key, value] of Object.entries(data)) {
|
|
216
|
-
if (this.isEntityOrCollection(value)) continue;
|
|
217
|
-
const mappedKey = fields[key] ?? key;
|
|
218
|
-
result[mappedKey] = this.normalizeValue(value);
|
|
219
|
-
}
|
|
220
|
-
return result;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Maps a complete domain entity to database data.
|
|
225
|
-
* Used for CREATE operations.
|
|
226
|
-
*
|
|
227
|
-
* @param entity - Entity name.
|
|
228
|
-
* @param domainEntity - Domain entity instance.
|
|
229
|
-
*/
|
|
230
|
-
mapEntity(
|
|
231
|
-
entity: string,
|
|
232
|
-
domainEntity: Entity<any> | ValueObject<any>
|
|
233
|
-
): MappedEntityData {
|
|
234
|
-
const fields = this.getFieldsMap(entity);
|
|
235
|
-
const result: MappedEntityData = {};
|
|
236
|
-
const hasId = (domainEntity as any).id;
|
|
237
|
-
if (hasId) {
|
|
238
|
-
const idValue = hasId.value ?? hasId;
|
|
239
|
-
result["id"] = idValue;
|
|
240
|
-
}
|
|
241
|
-
const props = (domainEntity as any).props || domainEntity;
|
|
242
|
-
for (const [key, value] of Object.entries(props)) {
|
|
243
|
-
if (key === "id") continue;
|
|
244
|
-
if (this.isEntityOrCollection(value)) continue;
|
|
245
|
-
const mappedKey = fields[key] ?? key;
|
|
246
|
-
result[mappedKey] = this.normalizeValue(value);
|
|
247
|
-
}
|
|
248
|
-
return result;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Gets the FK object to relate with the parent.
|
|
253
|
-
*
|
|
254
|
-
* @param entity - Entity name.
|
|
255
|
-
* @param parentId - Parent ID.
|
|
256
|
-
* @returns Object with the FK field or null if there is no parent.
|
|
257
|
-
*/
|
|
258
|
-
getParentFk(entity: string, parentId: string): Record<string, string> | null {
|
|
259
|
-
const schema = this.getSchema(entity);
|
|
260
|
-
if (!schema.parentFk) return null;
|
|
261
|
-
return { [schema.parentFk.field]: parentId };
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Gets the name of the parent entity.
|
|
266
|
-
* @param entity - Entity name.
|
|
267
|
-
*/
|
|
268
|
-
getParentEntity(entity: string): string | null {
|
|
269
|
-
const schema = this.getSchema(entity);
|
|
270
|
-
return schema.parentFk?.parentEntity ?? null;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Gets the FK field name.
|
|
275
|
-
* @param entity - Entity name.
|
|
276
|
-
*/
|
|
277
|
-
getParentFkField(entity: string): string | null {
|
|
278
|
-
const schema = this.getSchema(entity);
|
|
279
|
-
return schema.parentFk?.field ?? null;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Gets the collection configuration for a specific field.
|
|
284
|
-
*
|
|
285
|
-
* @param entity - Parent entity name (e.g., 'Post')
|
|
286
|
-
* @param fieldName - Collection field name (e.g., 'tags')
|
|
287
|
-
* @returns CollectionConfig or null if not configured
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* ```typescript
|
|
291
|
-
* const config = registry.getCollectionConfig('Post', 'tags');
|
|
292
|
-
* if (config?.type === 'reference') {
|
|
293
|
-
* // Handle N:N relation - use connect/disconnect
|
|
294
|
-
* } else {
|
|
295
|
-
* // Handle 1:N relation - use create/delete
|
|
296
|
-
* }
|
|
297
|
-
* ```
|
|
298
|
-
*/
|
|
299
|
-
getCollectionConfig(
|
|
300
|
-
entity: string,
|
|
301
|
-
fieldName: string
|
|
302
|
-
): CollectionConfig | null {
|
|
303
|
-
const schema = this.tryGetSchema(entity);
|
|
304
|
-
if (!schema?.collections) return null;
|
|
305
|
-
return schema.collections[fieldName] ?? null;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Checks if a collection is a reference type (N:N).
|
|
310
|
-
*
|
|
311
|
-
* @param entity - Parent entity name
|
|
312
|
-
* @param fieldName - Collection field name
|
|
313
|
-
* @returns true if the collection is a reference (N:N), false otherwise
|
|
314
|
-
*
|
|
315
|
-
* @example
|
|
316
|
-
* ```typescript
|
|
317
|
-
* if (registry.isReferenceCollection('Post', 'tags')) {
|
|
318
|
-
* // Use connect/disconnect instead of create/delete
|
|
319
|
-
* }
|
|
320
|
-
* ```
|
|
321
|
-
*/
|
|
322
|
-
isReferenceCollection(entity: string, fieldName: string): boolean {
|
|
323
|
-
const config = this.getCollectionConfig(entity, fieldName);
|
|
324
|
-
return config?.type === "reference";
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Checks if a collection is owned (1:N).
|
|
329
|
-
* Returns true if explicitly configured as 'owned' or if not configured at all.
|
|
330
|
-
*
|
|
331
|
-
* @param entity - Parent entity name
|
|
332
|
-
* @param fieldName - Collection field name
|
|
333
|
-
* @returns true if the collection is owned (1:N), false if reference
|
|
334
|
-
*/
|
|
335
|
-
isOwnedCollection(entity: string, fieldName: string): boolean {
|
|
336
|
-
const config = this.getCollectionConfig(entity, fieldName);
|
|
337
|
-
// Default to owned if not configured
|
|
338
|
-
return config?.type !== "reference";
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Gets all collections configured for an entity.
|
|
343
|
-
*
|
|
344
|
-
* @param entity - Entity name
|
|
345
|
-
* @returns Record of field names to collection configs, or empty object
|
|
346
|
-
*/
|
|
347
|
-
getCollections(entity: string): Record<string, CollectionConfig> {
|
|
348
|
-
const schema = this.tryGetSchema(entity);
|
|
349
|
-
return schema?.collections ?? {};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Gets all reference (N:N) collections for an entity.
|
|
354
|
-
*
|
|
355
|
-
* @param entity - Entity name
|
|
356
|
-
* @returns Array of field names that are reference collections
|
|
357
|
-
*/
|
|
358
|
-
getReferenceCollections(entity: string): string[] {
|
|
359
|
-
const collections = this.getCollections(entity);
|
|
360
|
-
return Object.entries(collections)
|
|
361
|
-
.filter(([_, config]) => config.type === "reference")
|
|
362
|
-
.map(([field]) => field);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Gets the junction table configuration for a reference collection.
|
|
367
|
-
*
|
|
368
|
-
* @param entity - Parent entity name
|
|
369
|
-
* @param fieldName - Collection field name
|
|
370
|
-
* @returns Junction config or null
|
|
371
|
-
*/
|
|
372
|
-
getJunctionConfig(
|
|
373
|
-
entity: string,
|
|
374
|
-
fieldName: string
|
|
375
|
-
): CollectionConfig["junction"] | null {
|
|
376
|
-
const config = this.getCollectionConfig(entity, fieldName);
|
|
377
|
-
return config?.junction ?? null;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Lists all registered entities.
|
|
382
|
-
*/
|
|
383
|
-
getRegisteredEntities(): string[] {
|
|
384
|
-
return Array.from(this.schemas.keys());
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Clears all registered schemas.
|
|
389
|
-
*/
|
|
390
|
-
clear(): void {
|
|
391
|
-
this.schemas.clear();
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Checks if a value is Entity, ValueObject, or Array.
|
|
396
|
-
*/
|
|
397
|
-
private isEntityOrCollection(value: any): boolean {
|
|
398
|
-
if (value === null || value === undefined) return false;
|
|
399
|
-
if (Array.isArray(value)) return true;
|
|
400
|
-
if (value instanceof Entity) return true;
|
|
401
|
-
if (value instanceof ValueObject) return true;
|
|
402
|
-
if (
|
|
403
|
-
typeof value === "object" &&
|
|
404
|
-
value.id &&
|
|
405
|
-
typeof value.id === "object" &&
|
|
406
|
-
"value" in value.id
|
|
407
|
-
) {
|
|
408
|
-
return true;
|
|
409
|
-
}
|
|
410
|
-
return false;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Normalizes a value for persistence.
|
|
415
|
-
*/
|
|
416
|
-
private normalizeValue(value: any): any {
|
|
417
|
-
if (value === null || value === undefined) return value;
|
|
418
|
-
if (value instanceof Id) return value.value;
|
|
419
|
-
if (value instanceof Date) return value;
|
|
420
|
-
if (typeof value === "object" && "value" in value) {
|
|
421
|
-
return value.value;
|
|
422
|
-
}
|
|
423
|
-
return value;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Validates that a relation field exists in the entity's collections.
|
|
428
|
-
*
|
|
429
|
-
* @param entity - Parent entity name
|
|
430
|
-
* @param relationField - Relation field to validate
|
|
431
|
-
* @throws ConfigurationError if the field doesn't exist
|
|
432
|
-
*
|
|
433
|
-
*/
|
|
434
|
-
public validateRelationField(entity: string, relationField: string): void {
|
|
435
|
-
const schema = this.tryGetSchema(entity);
|
|
436
|
-
|
|
437
|
-
const uuidPattern =
|
|
438
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
439
|
-
|
|
440
|
-
if (uuidPattern.test(entity)) {
|
|
441
|
-
throw new ConfigurationError(
|
|
442
|
-
`EntitySchemaRegistry: Received an ID '${entity}' instead of an entity name. ` +
|
|
443
|
-
`This usually means 'parentEntity' is not being set correctly in the ChangeTracker. ` +
|
|
444
|
-
`Check that addDelete/addCreate are receiving the entity NAME (e.g., 'Post'), not the ID.`
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
if (!schema) {
|
|
449
|
-
throw new ConfigurationError(
|
|
450
|
-
`EntitySchemaRegistry: Cannot validate relation '${relationField}' - ` +
|
|
451
|
-
`entity '${entity}' is not registered. ` +
|
|
452
|
-
`Available entities: ${
|
|
453
|
-
this.getRegisteredEntities().join(", ") || "none"
|
|
454
|
-
}`
|
|
455
|
-
);
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
const collections = schema.collections ?? {};
|
|
459
|
-
const availableCollections = Object.keys(collections);
|
|
460
|
-
|
|
461
|
-
if (availableCollections.length === 0) {
|
|
462
|
-
throw new ConfigurationError(
|
|
463
|
-
`EntitySchemaRegistry: Entity '${entity}' has no collections configured, ` +
|
|
464
|
-
`but received operation for relation '${relationField}'. ` +
|
|
465
|
-
`Did you forget to configure collections in the schema?`
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
if (!collections[relationField]) {
|
|
470
|
-
const suggestions = this.findSimilarNames(
|
|
471
|
-
relationField,
|
|
472
|
-
availableCollections
|
|
473
|
-
);
|
|
474
|
-
const suggestionText =
|
|
475
|
-
suggestions.length > 0
|
|
476
|
-
? ` Did you mean: '${suggestions.join("' or '")}'?`
|
|
477
|
-
: "";
|
|
478
|
-
|
|
479
|
-
throw new ConfigurationError(
|
|
480
|
-
`EntitySchemaRegistry: Unknown relation '${relationField}' for entity '${entity}'. ` +
|
|
481
|
-
`Available collections: ${availableCollections.join(
|
|
482
|
-
", "
|
|
483
|
-
)}.${suggestionText}`
|
|
484
|
-
);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
private findSimilarNames(input: string, candidates: string[]): string[] {
|
|
489
|
-
return candidates
|
|
490
|
-
.map((candidate) => ({
|
|
491
|
-
name: candidate,
|
|
492
|
-
distance: levenshteinDistance(
|
|
493
|
-
input.toLowerCase(),
|
|
494
|
-
candidate.toLowerCase()
|
|
495
|
-
),
|
|
496
|
-
}))
|
|
497
|
-
.filter(({ distance }) => distance <= 3)
|
|
498
|
-
.sort((a, b) => a.distance - b.distance)
|
|
499
|
-
.slice(0, 2)
|
|
500
|
-
.map(({ name }) => name);
|
|
501
|
-
}
|
|
502
|
-
}
|