@koalarx/nest 3.1.11 → 3.1.12
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.
|
@@ -14,8 +14,10 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
|
|
|
14
14
|
private readonly _modelName;
|
|
15
15
|
private readonly _includeFindMany?;
|
|
16
16
|
constructor({ context, modelName }: RepositoryInitProps<TEntity, TContext>);
|
|
17
|
+
private getIdPropName;
|
|
17
18
|
private getConnectPrismaSchemaForRelation;
|
|
18
19
|
private getSelectRootPrismaSchema;
|
|
20
|
+
private getSelectWithRelationsId;
|
|
19
21
|
private getPropNameFromEntitySource;
|
|
20
22
|
private listRelationEntities;
|
|
21
23
|
private listToRelationActionList;
|
|
@@ -24,9 +26,8 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
|
|
|
24
26
|
private findManySchema;
|
|
25
27
|
private createEntity;
|
|
26
28
|
private orphanRemoval;
|
|
27
|
-
private getIdPropName;
|
|
28
|
-
private enrichEntityWithRelations;
|
|
29
29
|
private loadRelationForEntity;
|
|
30
|
+
private enrichEntityWithRelations;
|
|
30
31
|
private persistRelations;
|
|
31
32
|
protected context(transactionalClient?: TContext): TContext[TModelKey];
|
|
32
33
|
protected findById(id: IComparableId): Promise<TEntity | null>;
|
|
@@ -21,6 +21,9 @@ class RepositoryBase {
|
|
|
21
21
|
entity: this._modelName,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
|
+
getIdPropName(entity) {
|
|
25
|
+
return (Reflect.getMetadata('entity:id', entity ? entity.constructor.prototype : this._modelName.prototype) ?? 'id');
|
|
26
|
+
}
|
|
24
27
|
getConnectPrismaSchemaForRelation(entity, data) {
|
|
25
28
|
const propIdName = this.getIdPropName(entity);
|
|
26
29
|
return {
|
|
@@ -46,6 +49,39 @@ class RepositoryBase {
|
|
|
46
49
|
});
|
|
47
50
|
return selectSchema;
|
|
48
51
|
}
|
|
52
|
+
getSelectWithRelationsId(entity) {
|
|
53
|
+
const selectSchema = {};
|
|
54
|
+
const entityProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
|
|
55
|
+
entityProps.forEach((prop) => {
|
|
56
|
+
let instance;
|
|
57
|
+
try {
|
|
58
|
+
instance = new (prop.type())();
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
instance = null;
|
|
62
|
+
}
|
|
63
|
+
if (instance instanceof entity_base_1.EntityBase) {
|
|
64
|
+
selectSchema[prop.name] = {
|
|
65
|
+
select: {
|
|
66
|
+
[this.getIdPropName(entity[prop.name])]: true,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
else if (instance instanceof list_1.List) {
|
|
71
|
+
const list = new entity()[prop.name];
|
|
72
|
+
const entityInstance = list.entityType;
|
|
73
|
+
selectSchema[prop.name] = {
|
|
74
|
+
select: {
|
|
75
|
+
[this.getIdPropName(entityInstance)]: true,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
selectSchema[prop.name] = true;
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return selectSchema;
|
|
84
|
+
}
|
|
49
85
|
getPropNameFromEntitySource(source, entity) {
|
|
50
86
|
return Object.keys(source).find((key) => {
|
|
51
87
|
const propDefinitions = auto_mapping_list_1.AutoMappingList.getPropDefinitions(source.constructor, key);
|
|
@@ -201,47 +237,47 @@ class RepositoryBase {
|
|
|
201
237
|
.forEach((key) => (where[key] = entity[key]));
|
|
202
238
|
return client[(0, KlString_1.toCamelCase)(entity.constructor.name)].delete({ where });
|
|
203
239
|
}
|
|
204
|
-
|
|
205
|
-
return
|
|
240
|
+
async loadRelationForEntity(where, entity) {
|
|
241
|
+
return this._context[(0, KlString_1.toCamelCase)((0, KlString_1.toCamelCase)(entity.name))]
|
|
242
|
+
.findUnique({
|
|
243
|
+
select: this.getSelectWithRelationsId(entity),
|
|
244
|
+
where,
|
|
245
|
+
})
|
|
246
|
+
.then((data) => this.enrichEntityWithRelations(entity, data));
|
|
206
247
|
}
|
|
207
|
-
async enrichEntityWithRelations(entity) {
|
|
248
|
+
async enrichEntityWithRelations(entity, data) {
|
|
208
249
|
const relationQueries = [];
|
|
209
250
|
const relationKeys = [];
|
|
210
|
-
const allProps = auto_mapping_list_1.AutoMappingList.getAllProps(
|
|
251
|
+
const allProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity);
|
|
211
252
|
allProps.forEach((prop) => {
|
|
212
253
|
const propName = prop.name;
|
|
213
|
-
const propDef = auto_mapping_list_1.AutoMappingList.getPropDefinitions(
|
|
254
|
+
const propDef = auto_mapping_list_1.AutoMappingList.getPropDefinitions(entity, propName);
|
|
255
|
+
if (propDef?.type === list_1.List.name) {
|
|
256
|
+
const list = new entity()[prop.name];
|
|
257
|
+
const entityInstance = list.entityType;
|
|
258
|
+
relationKeys.push(propName);
|
|
259
|
+
const items = [];
|
|
260
|
+
data[propName].forEach((item) => {
|
|
261
|
+
items.push(this.loadRelationForEntity(item, entityInstance));
|
|
262
|
+
});
|
|
263
|
+
relationQueries.push(Promise.all(items));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
214
266
|
const relationEntity = auto_mapping_list_1.AutoMappingList.getSourceByName(propDef?.type ?? '');
|
|
215
267
|
if (relationEntity) {
|
|
216
268
|
relationKeys.push(propName);
|
|
217
|
-
relationQueries.push(this.loadRelationForEntity(
|
|
269
|
+
relationQueries.push(this.loadRelationForEntity({
|
|
270
|
+
[this.getIdPropName(relationEntity)]: data[propName][this.getIdPropName(relationEntity)],
|
|
271
|
+
}, relationEntity));
|
|
218
272
|
}
|
|
219
273
|
});
|
|
220
274
|
if (relationQueries.length > 0) {
|
|
221
275
|
const results = await Promise.all(relationQueries);
|
|
222
276
|
relationKeys.forEach((key, index) => {
|
|
223
|
-
|
|
277
|
+
data[key] = results[index];
|
|
224
278
|
});
|
|
225
279
|
}
|
|
226
|
-
return
|
|
227
|
-
}
|
|
228
|
-
async loadRelationForEntity(entityId, relationName) {
|
|
229
|
-
const result = await this.context().findUnique({
|
|
230
|
-
where: { [this.getIdPropName()]: entityId },
|
|
231
|
-
include: {
|
|
232
|
-
[relationName]: true,
|
|
233
|
-
},
|
|
234
|
-
});
|
|
235
|
-
const relationData = result?.[relationName];
|
|
236
|
-
if (relationData) {
|
|
237
|
-
if (Array.isArray(relationData)) {
|
|
238
|
-
return Promise.all(relationData.map((item) => this.enrichEntityWithRelations(item)));
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
return this.enrichEntityWithRelations(relationData);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
return relationData;
|
|
280
|
+
return data;
|
|
245
281
|
}
|
|
246
282
|
persistRelations(transaction, entity) {
|
|
247
283
|
const { relationCreates, relationUpdates, relationDeletes } = this.listToRelationActionList(entity);
|
|
@@ -283,30 +319,33 @@ class RepositoryBase {
|
|
|
283
319
|
return this._context[contextKey];
|
|
284
320
|
}
|
|
285
321
|
async findById(id) {
|
|
286
|
-
const
|
|
322
|
+
const data = await this.context().findFirst({
|
|
323
|
+
select: this.getSelectWithRelationsId(this._modelName.prototype.constructor),
|
|
287
324
|
where: { [this.getIdPropName()]: id },
|
|
288
325
|
});
|
|
289
|
-
if (!
|
|
326
|
+
if (!data)
|
|
290
327
|
return null;
|
|
291
|
-
const enrichedEntity = await this.enrichEntityWithRelations(
|
|
328
|
+
const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, data);
|
|
292
329
|
return this.createEntity(enrichedEntity);
|
|
293
330
|
}
|
|
294
331
|
async findFirst(where) {
|
|
295
|
-
const
|
|
332
|
+
const data = await this.context().findFirst({
|
|
333
|
+
select: this.getSelectWithRelationsId(this._modelName.prototype.constructor),
|
|
296
334
|
where,
|
|
297
335
|
});
|
|
298
|
-
if (!
|
|
336
|
+
if (!data)
|
|
299
337
|
return null;
|
|
300
|
-
const enrichedEntity = await this.enrichEntityWithRelations(
|
|
338
|
+
const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, data);
|
|
301
339
|
return this.createEntity(enrichedEntity);
|
|
302
340
|
}
|
|
303
341
|
async findUnique(where) {
|
|
304
|
-
const
|
|
342
|
+
const data = await this.context().findUnique({
|
|
343
|
+
select: this.getSelectWithRelationsId(this._modelName.prototype.constructor),
|
|
305
344
|
where,
|
|
306
345
|
});
|
|
307
|
-
if (!
|
|
346
|
+
if (!data)
|
|
308
347
|
return null;
|
|
309
|
-
const enrichedEntity = await this.enrichEntityWithRelations(
|
|
348
|
+
const enrichedEntity = await this.enrichEntityWithRelations(this._modelName.prototype.constructor, data);
|
|
310
349
|
return this.createEntity(enrichedEntity);
|
|
311
350
|
}
|
|
312
351
|
async findMany(where, pagination) {
|