@koalarx/nest 3.1.36 → 3.1.38
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/core/database/entity.base.js +34 -0
- package/core/database/entity.decorator.d.ts +1 -1
- package/core/database/entity.decorator.js +2 -0
- package/core/database/repository.base.js +19 -17
- package/core/utils/automap-cycle-context.js +11 -4
- package/package.json +1 -1
- package/tsconfig.lib.tsbuildinfo +1 -1
|
@@ -17,6 +17,38 @@ class EntityBase {
|
|
|
17
17
|
_action = EntityActionType.create;
|
|
18
18
|
_hasUpdate = false;
|
|
19
19
|
constructor(props) {
|
|
20
|
+
Object.defineProperties(this, {
|
|
21
|
+
_trackHasUpdateOnSet: {
|
|
22
|
+
enumerable: false,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: false,
|
|
26
|
+
},
|
|
27
|
+
_setTrackingProxy: {
|
|
28
|
+
enumerable: false,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: undefined,
|
|
32
|
+
},
|
|
33
|
+
_id: {
|
|
34
|
+
enumerable: false,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: this._id,
|
|
38
|
+
},
|
|
39
|
+
_action: {
|
|
40
|
+
enumerable: false,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: this._action,
|
|
44
|
+
},
|
|
45
|
+
_hasUpdate: {
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
value: this._hasUpdate,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
20
52
|
if (props) {
|
|
21
53
|
this.automap(props);
|
|
22
54
|
}
|
|
@@ -37,6 +69,8 @@ class EntityBase {
|
|
|
37
69
|
if (success &&
|
|
38
70
|
target._trackHasUpdateOnSet &&
|
|
39
71
|
property !== '_hasUpdate' &&
|
|
72
|
+
property !== '_action' &&
|
|
73
|
+
property !== '_id' &&
|
|
40
74
|
property !== '_trackHasUpdateOnSet' &&
|
|
41
75
|
property !== '_setTrackingProxy') {
|
|
42
76
|
target._hasUpdate = true;
|
|
@@ -8,5 +8,5 @@ export interface IdConfig<T extends EntityProps<any>> {
|
|
|
8
8
|
composite?: readonly (keyof T)[];
|
|
9
9
|
custom?: (props: T) => string | number | CompositeId;
|
|
10
10
|
}
|
|
11
|
-
export declare function Entity<T extends new (...args: any[]) => EntityBase<any>>(
|
|
11
|
+
export declare function Entity(id?: keyof EntityProps<any> | IdConfig<EntityProps<any>>): <T extends new (...args: any[]) => EntityBase<any>>(target: T) => T;
|
|
12
12
|
export {};
|
|
@@ -30,6 +30,8 @@ function Entity(id) {
|
|
|
30
30
|
});
|
|
31
31
|
const idConfig = normalizeIdConfig(id);
|
|
32
32
|
Reflect.defineMetadata('entity:id', idConfig, NewConstructor.prototype);
|
|
33
|
+
Reflect.defineMetadata('entity:decorated-constructor', NewConstructor, target.prototype);
|
|
34
|
+
Reflect.defineMetadata('entity:decorated-constructor', NewConstructor, NewConstructor.prototype);
|
|
33
35
|
return NewConstructor;
|
|
34
36
|
};
|
|
35
37
|
}
|
|
@@ -223,6 +223,17 @@ class RepositoryBase {
|
|
|
223
223
|
relations: this.listRelationEntities(entityInstance),
|
|
224
224
|
});
|
|
225
225
|
}
|
|
226
|
+
else if (entity[key]._action === entity_base_1.EntityActionType.create) {
|
|
227
|
+
relationCreates.push({
|
|
228
|
+
modelName: (0, KlString_1.toCamelCase)(modelName),
|
|
229
|
+
entityInstance,
|
|
230
|
+
schema: {
|
|
231
|
+
data: this.entityToPrisma(entityInstance),
|
|
232
|
+
select: this.getSelectRootPrismaSchema(entityInstance.constructor),
|
|
233
|
+
},
|
|
234
|
+
relations: this.listRelationEntities(entityInstance),
|
|
235
|
+
});
|
|
236
|
+
}
|
|
226
237
|
}
|
|
227
238
|
});
|
|
228
239
|
return { relationCreates, relationUpdates, relationDeletes };
|
|
@@ -285,8 +296,15 @@ class RepositoryBase {
|
|
|
285
296
|
}
|
|
286
297
|
createEntity(data, entityClass) {
|
|
287
298
|
const entity = new (entityClass || this._modelName)();
|
|
299
|
+
const trackedEntity = entity;
|
|
300
|
+
if (typeof trackedEntity.stopHasUpdateTracking === 'function') {
|
|
301
|
+
trackedEntity.stopHasUpdateTracking();
|
|
302
|
+
}
|
|
288
303
|
entity._action = entity_base_1.EntityActionType.update;
|
|
289
304
|
entity.automap(data);
|
|
305
|
+
if (typeof trackedEntity.startHasUpdateTracking === 'function') {
|
|
306
|
+
trackedEntity.startHasUpdateTracking();
|
|
307
|
+
}
|
|
290
308
|
return entity;
|
|
291
309
|
}
|
|
292
310
|
orphanRemoval(client, entity) {
|
|
@@ -405,23 +423,7 @@ class RepositoryBase {
|
|
|
405
423
|
relation[relationPropName] =
|
|
406
424
|
this.getConnectPrismaSchemaForRelation(relationCreate.entityInstance, response);
|
|
407
425
|
}
|
|
408
|
-
return transaction
|
|
409
|
-
.create({
|
|
410
|
-
data: this.entityToPrisma(relation),
|
|
411
|
-
select: this.getSelectRootPrismaSchema(relation.constructor),
|
|
412
|
-
})
|
|
413
|
-
.then((response) => {
|
|
414
|
-
const idPropName = this.getIdPropName(relation);
|
|
415
|
-
if (!Array.isArray(idPropName)) {
|
|
416
|
-
relation[idPropName] = response[idPropName];
|
|
417
|
-
}
|
|
418
|
-
else {
|
|
419
|
-
idPropName.forEach((propName) => {
|
|
420
|
-
relation[propName] = response[propName];
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
return this.persistRelations(transaction, relation);
|
|
424
|
-
});
|
|
426
|
+
return this.persistRelations(transaction, relation);
|
|
425
427
|
}));
|
|
426
428
|
})),
|
|
427
429
|
]);
|
|
@@ -15,12 +15,19 @@ function mapEntityReference(value, EntityOnPropKey, context, action) {
|
|
|
15
15
|
if (cachedEntity) {
|
|
16
16
|
return cachedEntity;
|
|
17
17
|
}
|
|
18
|
-
const
|
|
18
|
+
const DecoratedEntityConstructor = Reflect.getMetadata('entity:decorated-constructor', EntityOnPropKey.prototype) ?? EntityOnPropKey;
|
|
19
|
+
const entity = new DecoratedEntityConstructor();
|
|
20
|
+
const trackedEntity = entity;
|
|
19
21
|
if (entity && typeof entity.automap === 'function') {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
if (typeof trackedEntity.stopHasUpdateTracking === 'function') {
|
|
23
|
+
trackedEntity.stopHasUpdateTracking();
|
|
24
|
+
}
|
|
25
|
+
trackedEntity._action = action;
|
|
22
26
|
context.references.set(value, entity);
|
|
23
|
-
|
|
27
|
+
trackedEntity.automap(value, context);
|
|
28
|
+
if (typeof trackedEntity.startHasUpdateTracking === 'function') {
|
|
29
|
+
trackedEntity.startHasUpdateTracking();
|
|
30
|
+
}
|
|
24
31
|
}
|
|
25
32
|
return entity;
|
|
26
33
|
}
|