@woltz/rich-domain 1.1.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 +87 -21
- 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 +31 -15
- package/dist/criteria.d.ts.map +1 -1
- package/dist/criteria.js +151 -61
- 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 +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- 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/paginated-result.d.ts.map +1 -1
- package/dist/paginated-result.js +7 -6
- package/dist/paginated-result.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 +31 -7
- 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/utils/criteria-operator-validation.d.ts +5 -0
- package/dist/utils/criteria-operator-validation.d.ts.map +1 -0
- package/dist/utils/criteria-operator-validation.js +143 -0
- package/dist/utils/criteria-operator-validation.js.map +1 -0
- package/dist/utils/helpers.d.ts +2 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +10 -0
- package/dist/utils/helpers.js.map +1 -0
- 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/eslint.config.js +6 -0
- package/package.json +2 -1
- package/src/aggregate-changes.ts +335 -0
- package/src/base-entity.ts +140 -100
- package/src/criteria.ts +264 -82
- 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 +8 -2
- package/src/mapper.ts +4 -1
- package/src/paginated-result.ts +7 -8
- 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 +95 -17
- package/src/types/history-tracker.ts +13 -0
- package/src/types/utils.ts +0 -9
- package/src/utils/criteria-operator-validation.ts +171 -0
- package/src/utils/helpers.ts +6 -0
- 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 +366 -90
- 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
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Entity } from "./entity";
|
|
2
|
+
import { ValueObject } from "./value-object";
|
|
3
|
+
/**
|
|
4
|
+
* Mapping schema for a domain entity.
|
|
5
|
+
*/
|
|
6
|
+
export interface EntitySchema {
|
|
7
|
+
/** Entity name in the domain (e.g., 'User', 'Post') */
|
|
8
|
+
entity: string;
|
|
9
|
+
/** Table name in the database (e.g., 'users', 'blog_posts') */
|
|
10
|
+
table: string;
|
|
11
|
+
/**
|
|
12
|
+
* Field mapping: domain → database.
|
|
13
|
+
* Only include fields with different names.
|
|
14
|
+
* @example { email: 'user_email', createdAt: 'created_at' }
|
|
15
|
+
*/
|
|
16
|
+
fields?: Record<string, string>;
|
|
17
|
+
/**
|
|
18
|
+
* FK configuration for parent relation.
|
|
19
|
+
*/
|
|
20
|
+
parentFk?: {
|
|
21
|
+
/** Name of the FK field in the database (e.g., 'author_id') */
|
|
22
|
+
field: string;
|
|
23
|
+
/** Name of the parent entity (e.g., 'User') */
|
|
24
|
+
parentEntity: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Result of entity mapping.
|
|
29
|
+
*/
|
|
30
|
+
export interface MappedEntityData {
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Registry for mapping domain entities to database tables and fields.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const registry = new EntitySchemaRegistry()
|
|
39
|
+
* .register({
|
|
40
|
+
* entity: 'User',
|
|
41
|
+
* table: 'users',
|
|
42
|
+
* fields: { email: 'user_email', name: 'user_name' },
|
|
43
|
+
* })
|
|
44
|
+
* .register({
|
|
45
|
+
* entity: 'Post',
|
|
46
|
+
* table: 'blog_posts',
|
|
47
|
+
* fields: { content: 'post_content' },
|
|
48
|
+
* parentFk: { field: 'author_id', parentEntity: 'User' },
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* const table = registry.getTable('Post'); // 'blog_posts'
|
|
52
|
+
* const mapped = registry.mapFields('User', { email: 'test@test.com' });
|
|
53
|
+
* // { user_email: 'test@test.com' }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare class EntitySchemaRegistry {
|
|
57
|
+
private schemas;
|
|
58
|
+
/**
|
|
59
|
+
* Registers an entity schema.
|
|
60
|
+
* @param schema - Schema to be registered.
|
|
61
|
+
* @returns this (for chaining)
|
|
62
|
+
*/
|
|
63
|
+
register(schema: EntitySchema): this;
|
|
64
|
+
/**
|
|
65
|
+
* Registers multiple schemas at once.
|
|
66
|
+
* @param schemas - Array of schemas.
|
|
67
|
+
* @returns this (for chaining)
|
|
68
|
+
*/
|
|
69
|
+
registerAll(schemas: EntitySchema[]): this;
|
|
70
|
+
/**
|
|
71
|
+
* Gets the schema of an entity.
|
|
72
|
+
* @param entity - Entity name.
|
|
73
|
+
* @throws Error if the entity is not registered.
|
|
74
|
+
*/
|
|
75
|
+
getSchema(entity: string): EntitySchema;
|
|
76
|
+
/**
|
|
77
|
+
* Checks if an entity is registered.
|
|
78
|
+
* @param entity - Entity name.
|
|
79
|
+
*/
|
|
80
|
+
has(entity: string): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Gets the table name for an entity.
|
|
83
|
+
* @param entity - Entity name.
|
|
84
|
+
*/
|
|
85
|
+
getTable(entity: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Gets the field mapping for an entity.
|
|
88
|
+
* @param entity - Entity name.
|
|
89
|
+
*/
|
|
90
|
+
getFieldsMap(entity: string): Record<string, string>;
|
|
91
|
+
/**
|
|
92
|
+
* Maps a domain field name to the database field name.
|
|
93
|
+
* @param entity - Entity name.
|
|
94
|
+
* @param fieldName - Domain field name.
|
|
95
|
+
*/
|
|
96
|
+
mapFieldName(entity: string, fieldName: string): string;
|
|
97
|
+
/**
|
|
98
|
+
* Maps fields of a domain object to database field names.
|
|
99
|
+
* Ignores values that are Entity, ValueObject, or Arrays.
|
|
100
|
+
*
|
|
101
|
+
* @param entity - Entity name.
|
|
102
|
+
* @param data - Data to be mapped.
|
|
103
|
+
*/
|
|
104
|
+
mapFields(entity: string, data: Record<string, any>): MappedEntityData;
|
|
105
|
+
/**
|
|
106
|
+
* Maps a complete domain entity to database data.
|
|
107
|
+
* Used for CREATE operations.
|
|
108
|
+
*
|
|
109
|
+
* @param entity - Entity name.
|
|
110
|
+
* @param domainEntity - Domain entity instance.
|
|
111
|
+
*/
|
|
112
|
+
mapEntity(entity: string, domainEntity: Entity<any> | ValueObject<any>): MappedEntityData;
|
|
113
|
+
/**
|
|
114
|
+
* Gets the FK object to relate with the parent.
|
|
115
|
+
*
|
|
116
|
+
* @param entity - Entity name.
|
|
117
|
+
* @param parentId - Parent ID.
|
|
118
|
+
* @returns Object with the FK field or null if there is no parent.
|
|
119
|
+
*/
|
|
120
|
+
getParentFk(entity: string, parentId: string): Record<string, string> | null;
|
|
121
|
+
/**
|
|
122
|
+
* Gets the name of the parent entity.
|
|
123
|
+
* @param entity - Entity name.
|
|
124
|
+
*/
|
|
125
|
+
getParentEntity(entity: string): string | null;
|
|
126
|
+
/**
|
|
127
|
+
* Gets the FK field name.
|
|
128
|
+
* @param entity - Entity name.
|
|
129
|
+
*/
|
|
130
|
+
getParentFkField(entity: string): string | null;
|
|
131
|
+
/**
|
|
132
|
+
* Lists all registered entities.
|
|
133
|
+
*/
|
|
134
|
+
getRegisteredEntities(): string[];
|
|
135
|
+
/**
|
|
136
|
+
* Clears all registered schemas.
|
|
137
|
+
*/
|
|
138
|
+
clear(): void;
|
|
139
|
+
/**
|
|
140
|
+
* Checks if a value is Entity, ValueObject, or Array.
|
|
141
|
+
*/
|
|
142
|
+
private isEntityOrCollection;
|
|
143
|
+
/**
|
|
144
|
+
* Normalizes a value for persistence.
|
|
145
|
+
*/
|
|
146
|
+
private normalizeValue;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=entity-schema-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-schema-registry.d.ts","sourceRoot":"","sources":["../src/entity-schema-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,uDAAuD;IACvD,MAAM,EAAE,MAAM,CAAC;IAEf,+DAA+D;IAC/D,KAAK,EAAE,MAAM,CAAC;IAEd;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhC;;OAEG;IACH,QAAQ,CAAC,EAAE;QACT,+DAA+D;QAC/D,KAAK,EAAE,MAAM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;;;OAGG;IACH,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAK/C;;OAEG;IACH,qBAAqB,IAAI,MAAM,EAAE;IAIjC;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAc5B;;OAEG;IACH,OAAO,CAAC,cAAc;CAUvB"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { Entity } from "./entity";
|
|
2
|
+
import { ValueObject } from "./value-object";
|
|
3
|
+
import { Id } from "./id";
|
|
4
|
+
/**
|
|
5
|
+
* Registry for mapping domain entities to database tables and fields.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const registry = new EntitySchemaRegistry()
|
|
10
|
+
* .register({
|
|
11
|
+
* entity: 'User',
|
|
12
|
+
* table: 'users',
|
|
13
|
+
* fields: { email: 'user_email', name: 'user_name' },
|
|
14
|
+
* })
|
|
15
|
+
* .register({
|
|
16
|
+
* entity: 'Post',
|
|
17
|
+
* table: 'blog_posts',
|
|
18
|
+
* fields: { content: 'post_content' },
|
|
19
|
+
* parentFk: { field: 'author_id', parentEntity: 'User' },
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const table = registry.getTable('Post'); // 'blog_posts'
|
|
23
|
+
* const mapped = registry.mapFields('User', { email: 'test@test.com' });
|
|
24
|
+
* // { user_email: 'test@test.com' }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export class EntitySchemaRegistry {
|
|
28
|
+
constructor() {
|
|
29
|
+
this.schemas = new Map();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Registers an entity schema.
|
|
33
|
+
* @param schema - Schema to be registered.
|
|
34
|
+
* @returns this (for chaining)
|
|
35
|
+
*/
|
|
36
|
+
register(schema) {
|
|
37
|
+
if (this.schemas.has(schema.entity)) {
|
|
38
|
+
console.warn(`EntitySchemaRegistry: Schema for '${schema.entity}' is being overwritten`);
|
|
39
|
+
}
|
|
40
|
+
this.schemas.set(schema.entity, schema);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Registers multiple schemas at once.
|
|
45
|
+
* @param schemas - Array of schemas.
|
|
46
|
+
* @returns this (for chaining)
|
|
47
|
+
*/
|
|
48
|
+
registerAll(schemas) {
|
|
49
|
+
schemas.forEach((schema) => this.register(schema));
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets the schema of an entity.
|
|
54
|
+
* @param entity - Entity name.
|
|
55
|
+
* @throws Error if the entity is not registered.
|
|
56
|
+
*/
|
|
57
|
+
getSchema(entity) {
|
|
58
|
+
const schema = this.schemas.get(entity);
|
|
59
|
+
if (!schema) {
|
|
60
|
+
throw new Error(`EntitySchemaRegistry: No schema registered for entity '${entity}'. ` +
|
|
61
|
+
`Available entities: ${Array.from(this.schemas.keys()).join(", ") || "none"}`);
|
|
62
|
+
}
|
|
63
|
+
return schema;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if an entity is registered.
|
|
67
|
+
* @param entity - Entity name.
|
|
68
|
+
*/
|
|
69
|
+
has(entity) {
|
|
70
|
+
return this.schemas.has(entity);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Gets the table name for an entity.
|
|
74
|
+
* @param entity - Entity name.
|
|
75
|
+
*/
|
|
76
|
+
getTable(entity) {
|
|
77
|
+
return this.getSchema(entity).table;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Gets the field mapping for an entity.
|
|
81
|
+
* @param entity - Entity name.
|
|
82
|
+
*/
|
|
83
|
+
getFieldsMap(entity) {
|
|
84
|
+
return this.getSchema(entity).fields || {};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Maps a domain field name to the database field name.
|
|
88
|
+
* @param entity - Entity name.
|
|
89
|
+
* @param fieldName - Domain field name.
|
|
90
|
+
*/
|
|
91
|
+
mapFieldName(entity, fieldName) {
|
|
92
|
+
const fields = this.getFieldsMap(entity);
|
|
93
|
+
return fields[fieldName] ?? fieldName;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Maps fields of a domain object to database field names.
|
|
97
|
+
* Ignores values that are Entity, ValueObject, or Arrays.
|
|
98
|
+
*
|
|
99
|
+
* @param entity - Entity name.
|
|
100
|
+
* @param data - Data to be mapped.
|
|
101
|
+
*/
|
|
102
|
+
mapFields(entity, data) {
|
|
103
|
+
const fields = this.getFieldsMap(entity);
|
|
104
|
+
const result = {};
|
|
105
|
+
for (const [key, value] of Object.entries(data)) {
|
|
106
|
+
if (this.isEntityOrCollection(value)) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const mappedKey = fields[key] ?? key;
|
|
110
|
+
result[mappedKey] = this.normalizeValue(value);
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Maps a complete domain entity to database data.
|
|
116
|
+
* Used for CREATE operations.
|
|
117
|
+
*
|
|
118
|
+
* @param entity - Entity name.
|
|
119
|
+
* @param domainEntity - Domain entity instance.
|
|
120
|
+
*/
|
|
121
|
+
mapEntity(entity, domainEntity) {
|
|
122
|
+
const fields = this.getFieldsMap(entity);
|
|
123
|
+
const result = {};
|
|
124
|
+
// Map ID if it is an Entity or has entity-like structure
|
|
125
|
+
const hasId = domainEntity.id;
|
|
126
|
+
if (hasId) {
|
|
127
|
+
// Extract id value
|
|
128
|
+
const idValue = hasId.value ?? hasId;
|
|
129
|
+
result["id"] = idValue;
|
|
130
|
+
}
|
|
131
|
+
// Get props
|
|
132
|
+
const props = domainEntity.props || domainEntity;
|
|
133
|
+
for (const [key, value] of Object.entries(props)) {
|
|
134
|
+
if (key === "id")
|
|
135
|
+
continue; // ID already mapped
|
|
136
|
+
if (this.isEntityOrCollection(value))
|
|
137
|
+
continue;
|
|
138
|
+
const mappedKey = fields[key] ?? key;
|
|
139
|
+
result[mappedKey] = this.normalizeValue(value);
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Gets the FK object to relate with the parent.
|
|
145
|
+
*
|
|
146
|
+
* @param entity - Entity name.
|
|
147
|
+
* @param parentId - Parent ID.
|
|
148
|
+
* @returns Object with the FK field or null if there is no parent.
|
|
149
|
+
*/
|
|
150
|
+
getParentFk(entity, parentId) {
|
|
151
|
+
const schema = this.getSchema(entity);
|
|
152
|
+
if (!schema.parentFk)
|
|
153
|
+
return null;
|
|
154
|
+
return { [schema.parentFk.field]: parentId };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Gets the name of the parent entity.
|
|
158
|
+
* @param entity - Entity name.
|
|
159
|
+
*/
|
|
160
|
+
getParentEntity(entity) {
|
|
161
|
+
const schema = this.getSchema(entity);
|
|
162
|
+
return schema.parentFk?.parentEntity ?? null;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Gets the FK field name.
|
|
166
|
+
* @param entity - Entity name.
|
|
167
|
+
*/
|
|
168
|
+
getParentFkField(entity) {
|
|
169
|
+
const schema = this.getSchema(entity);
|
|
170
|
+
return schema.parentFk?.field ?? null;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Lists all registered entities.
|
|
174
|
+
*/
|
|
175
|
+
getRegisteredEntities() {
|
|
176
|
+
return Array.from(this.schemas.keys());
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Clears all registered schemas.
|
|
180
|
+
*/
|
|
181
|
+
clear() {
|
|
182
|
+
this.schemas.clear();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Checks if a value is Entity, ValueObject, or Array.
|
|
186
|
+
*/
|
|
187
|
+
isEntityOrCollection(value) {
|
|
188
|
+
if (value === null || value === undefined)
|
|
189
|
+
return false;
|
|
190
|
+
if (Array.isArray(value))
|
|
191
|
+
return true;
|
|
192
|
+
if (value instanceof Entity)
|
|
193
|
+
return true;
|
|
194
|
+
if (value instanceof ValueObject)
|
|
195
|
+
return true;
|
|
196
|
+
// Checks if it has the structure of an Entity (object with 'id' that has 'value')
|
|
197
|
+
if (typeof value === 'object' && value.id && typeof value.id === 'object' && 'value' in value.id) {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Normalizes a value for persistence.
|
|
204
|
+
*/
|
|
205
|
+
normalizeValue(value) {
|
|
206
|
+
if (value === null || value === undefined)
|
|
207
|
+
return value;
|
|
208
|
+
if (value instanceof Id)
|
|
209
|
+
return value.value;
|
|
210
|
+
if (value instanceof Date)
|
|
211
|
+
return value;
|
|
212
|
+
if (typeof value === "object" && "value" in value) {
|
|
213
|
+
// Might be an ID or other wrapper
|
|
214
|
+
return value.value;
|
|
215
|
+
}
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=entity-schema-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-schema-registry.js","sourceRoot":"","sources":["../src/entity-schema-registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAqC1B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,oBAAoB;IAAjC;QACU,YAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IAmNpD,CAAC;IAjNC;;;;OAIG;IACH,QAAQ,CAAC,MAAoB;QAC3B,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;YACpC,OAAO,CAAC,IAAI,CACV,qCAAqC,MAAM,CAAC,MAAM,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AI,CAAC;QACtC,IAAI,KAAK,YAAY,MAAM;YAAE,OAAO,IAAI,CAAC;QACzC,IAAI,KAAK,YAAY,WAAW;YAAE,OAAO,IAAI,CAAC;QAE9C,kFAAkF;QAClF,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,EAAE,IAAI,OAAO,KAAK,CAAC,EAAE,KAAK,QAAQ,IAAI,OAAO,IAAI,KAAK,CAAC,EAAE,EAAE,CAAC;YACjG,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACK,cAAc,CAAC,KAAU;QAC/B,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO,KAAK,CAAC;QACxD,IAAI,KAAK,YAAY,EAAE;YAAE,OAAO,KAAK,CAAC,KAAK,CAAC;QAC5C,IAAI,KAAK,YAAY,IAAI;YAAE,OAAO,KAAK,CAAC;QACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,IAAI,KAAK,EAAE,CAAC;YAClD,kCAAkC;YAClC,OAAO,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { HistoryEntry } from "./types";
|
|
2
|
+
import { AggregateChanges } from "./aggregate-changes";
|
|
3
|
+
/**
|
|
4
|
+
* Callback for validation on property change.
|
|
5
|
+
* Return false to reject the change, or throw an error.
|
|
6
|
+
*/
|
|
7
|
+
export type OnChangeValidator = (path: string, oldValue: any, newValue: any) => boolean | void;
|
|
8
|
+
/**
|
|
9
|
+
* Tracks changes in Aggregates using Proxy.
|
|
10
|
+
*
|
|
11
|
+
* Features:
|
|
12
|
+
* - Tracks changes in primitive properties
|
|
13
|
+
* - Tracks changes in nested entities (1:1)
|
|
14
|
+
* - Tracks changes in collections (1:N)
|
|
15
|
+
* - Supports Value Objects with identityKey
|
|
16
|
+
* - Calculates depth automatically
|
|
17
|
+
* - Generates AggregateChanges for persistence
|
|
18
|
+
* - Supports validation on change via onChangeValidator
|
|
19
|
+
*/
|
|
20
|
+
export declare class HistoryTracker {
|
|
21
|
+
private target;
|
|
22
|
+
private rootEntityName;
|
|
23
|
+
private path;
|
|
24
|
+
private depth;
|
|
25
|
+
private parentId?;
|
|
26
|
+
private parentEntity?;
|
|
27
|
+
private rootTracker?;
|
|
28
|
+
private history;
|
|
29
|
+
private originalValues;
|
|
30
|
+
private trackedArrays;
|
|
31
|
+
private trackedEntities;
|
|
32
|
+
private onChangeValidator?;
|
|
33
|
+
constructor(target: any, rootEntityName: string, path?: string, depth?: number, parentId?: string | undefined, parentEntity?: string | undefined, rootTracker?: HistoryTracker | undefined);
|
|
34
|
+
/**
|
|
35
|
+
* Sets a validator callback that will be called on every property change.
|
|
36
|
+
* The validator can:
|
|
37
|
+
* - Return false to reject the change (value will be reverted)
|
|
38
|
+
* - Throw an error to reject the change with an error
|
|
39
|
+
* - Return true/undefined to accept the change
|
|
40
|
+
*/
|
|
41
|
+
setOnChangeValidator(validator: OnChangeValidator): void;
|
|
42
|
+
private captureInitialState;
|
|
43
|
+
private captureEntityState;
|
|
44
|
+
private captureArrayState;
|
|
45
|
+
createProxy(): any;
|
|
46
|
+
private createArrayProxy;
|
|
47
|
+
/**
|
|
48
|
+
* Returns all detected changes as AggregateChanges.
|
|
49
|
+
*/
|
|
50
|
+
getChanges<TEntityMap = Record<string, any>>(): AggregateChanges<TEntityMap>;
|
|
51
|
+
private analyzeRootChanges;
|
|
52
|
+
private analyzeCollectionChanges;
|
|
53
|
+
/**
|
|
54
|
+
* Recursively marks all nested items as created when a parent is created.
|
|
55
|
+
*/
|
|
56
|
+
private markNestedItemsAsCreated;
|
|
57
|
+
/**
|
|
58
|
+
* Recursively marks all nested items as deleted when a parent is deleted.
|
|
59
|
+
* Uses the original captured state to find nested items.
|
|
60
|
+
*/
|
|
61
|
+
private markNestedItemsAsDeleted;
|
|
62
|
+
/**
|
|
63
|
+
* Recursively marks nested items as deleted from a JSON object.
|
|
64
|
+
* This is used when processing cloned (JSON) state.
|
|
65
|
+
*/
|
|
66
|
+
private markNestedJsonItemAsDeleted;
|
|
67
|
+
/**
|
|
68
|
+
* Extracts identity key from a JSON object by looking at the original ValueObject instances.
|
|
69
|
+
*/
|
|
70
|
+
private extractIdentityKeyFromJson;
|
|
71
|
+
private collectNestedArrays;
|
|
72
|
+
private analyzeEntityChanges;
|
|
73
|
+
private detectEntityChangeState;
|
|
74
|
+
private detectArrayChanges;
|
|
75
|
+
private detectChangedFields;
|
|
76
|
+
private handleArrayAssignment;
|
|
77
|
+
private handleEntityChange;
|
|
78
|
+
private getRootTracker;
|
|
79
|
+
private buildPath;
|
|
80
|
+
private shouldSkipProperty;
|
|
81
|
+
private getValueAtPath;
|
|
82
|
+
private getItemKey;
|
|
83
|
+
private getEntityId;
|
|
84
|
+
private getEntityName;
|
|
85
|
+
private isEntityOrVO;
|
|
86
|
+
private isEqual;
|
|
87
|
+
private deepEqual;
|
|
88
|
+
private hasChanged;
|
|
89
|
+
private cloneArray;
|
|
90
|
+
private deepClone;
|
|
91
|
+
private normalizeAndStringify;
|
|
92
|
+
getHistory(): HistoryEntry[];
|
|
93
|
+
clearHistory(): void;
|
|
94
|
+
markAsClean(): void;
|
|
95
|
+
getTarget(): any;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=history-tracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history-tracker.d.ts","sourceRoot":"","sources":["../src/history-tracker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAc,YAAY,EAAe,MAAM,SAAS,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAC9B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,GAAG,EACb,QAAQ,EAAE,GAAG,KACV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