@koalarx/nest 3.1.7 → 3.1.8
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.
|
@@ -8,14 +8,12 @@ interface RepositoryInitProps<TEntity extends EntityBase<TEntity>, TContext exte
|
|
|
8
8
|
context: TContext;
|
|
9
9
|
modelName: Type<TEntity>;
|
|
10
10
|
transactionContext?: Type<TContext>;
|
|
11
|
-
deepIncludeLimit?: number;
|
|
12
11
|
}
|
|
13
12
|
export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>, TContext extends PrismaTransactionalClient = PrismaTransactionalClient, TModelKey extends keyof TContext = keyof TContext> {
|
|
14
13
|
protected _context: TContext;
|
|
15
14
|
private readonly _modelName;
|
|
16
|
-
private readonly _include?;
|
|
17
15
|
private readonly _includeFindMany?;
|
|
18
|
-
constructor({ context, modelName,
|
|
16
|
+
constructor({ context, modelName, }: RepositoryInitProps<TEntity, TContext>);
|
|
19
17
|
private getConnectPrismaSchemaForRelation;
|
|
20
18
|
private getSelectRootPrismaSchema;
|
|
21
19
|
private getPropNameFromEntitySource;
|
|
@@ -27,6 +25,8 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
|
|
|
27
25
|
private createEntity;
|
|
28
26
|
private orphanRemoval;
|
|
29
27
|
private getIdPropName;
|
|
28
|
+
private enrichEntityWithRelations;
|
|
29
|
+
private loadRelationForEntity;
|
|
30
30
|
private persistRelations;
|
|
31
31
|
protected context(transactionalClient?: TContext): TContext[TModelKey];
|
|
32
32
|
protected findById(id: IComparableId): Promise<TEntity | null>;
|
|
@@ -11,18 +11,13 @@ const entity_base_1 = require("./entity.base");
|
|
|
11
11
|
class RepositoryBase {
|
|
12
12
|
_context;
|
|
13
13
|
_modelName;
|
|
14
|
-
_include;
|
|
15
14
|
_includeFindMany;
|
|
16
|
-
constructor({ context, modelName,
|
|
15
|
+
constructor({ context, modelName, }) {
|
|
17
16
|
this._context = context;
|
|
18
17
|
this._modelName = modelName;
|
|
19
|
-
this._include = (0, generate_prisma_include_schema_1.generateIncludeSchema)({
|
|
20
|
-
deepLimit: deepIncludeLimit || 5,
|
|
21
|
-
entity: this._modelName,
|
|
22
|
-
});
|
|
23
18
|
this._includeFindMany = (0, generate_prisma_include_schema_1.generateIncludeSchema)({
|
|
24
19
|
forList: true,
|
|
25
|
-
deepLimit:
|
|
20
|
+
deepLimit: 1,
|
|
26
21
|
entity: this._modelName,
|
|
27
22
|
});
|
|
28
23
|
}
|
|
@@ -170,7 +165,7 @@ class RepositoryBase {
|
|
|
170
165
|
return prismaSchema;
|
|
171
166
|
}
|
|
172
167
|
getInclude(include) {
|
|
173
|
-
include = include ??
|
|
168
|
+
include = include ?? {};
|
|
174
169
|
const result = {};
|
|
175
170
|
Object.keys(include).forEach((key) => {
|
|
176
171
|
if (typeof include[key] === 'boolean') {
|
|
@@ -209,6 +204,42 @@ class RepositoryBase {
|
|
|
209
204
|
getIdPropName(entity) {
|
|
210
205
|
return (Reflect.getMetadata('entity:id', entity ? entity.constructor.prototype : this._modelName.prototype) ?? 'id');
|
|
211
206
|
}
|
|
207
|
+
async enrichEntityWithRelations(entity) {
|
|
208
|
+
const relationQueries = [];
|
|
209
|
+
const relationKeys = [];
|
|
210
|
+
Object.keys(entity).forEach((key) => {
|
|
211
|
+
const propDef = auto_mapping_list_1.AutoMappingList.getPropDefinitions(this._modelName.prototype, key);
|
|
212
|
+
if (propDef) {
|
|
213
|
+
relationKeys.push(key);
|
|
214
|
+
relationQueries.push(this.loadRelationForEntity(entity[this.getIdPropName()], key));
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
if (relationQueries.length > 0) {
|
|
218
|
+
const results = await Promise.all(relationQueries);
|
|
219
|
+
relationKeys.forEach((key, index) => {
|
|
220
|
+
entity[key] = results[index];
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return entity;
|
|
224
|
+
}
|
|
225
|
+
async loadRelationForEntity(entityId, relationName) {
|
|
226
|
+
const result = await this.context().findUnique({
|
|
227
|
+
where: { [this.getIdPropName()]: entityId },
|
|
228
|
+
include: {
|
|
229
|
+
[relationName]: true,
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
const relationData = result?.[relationName];
|
|
233
|
+
if (relationData) {
|
|
234
|
+
if (Array.isArray(relationData)) {
|
|
235
|
+
return Promise.all(relationData.map((item) => this.enrichEntityWithRelations(item)));
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
return this.enrichEntityWithRelations(relationData);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return relationData;
|
|
242
|
+
}
|
|
212
243
|
persistRelations(transaction, entity) {
|
|
213
244
|
const { relationCreates, relationUpdates, relationDeletes } = this.listToRelationActionList(entity);
|
|
214
245
|
return Promise.all([
|
|
@@ -249,17 +280,13 @@ class RepositoryBase {
|
|
|
249
280
|
return this._context[contextKey];
|
|
250
281
|
}
|
|
251
282
|
async findById(id) {
|
|
252
|
-
|
|
253
|
-
.findFirst({
|
|
254
|
-
include: this.getInclude(),
|
|
283
|
+
const entity = await this.context().findFirst({
|
|
255
284
|
where: { [this.getIdPropName()]: id },
|
|
256
|
-
})
|
|
257
|
-
.then((response) => {
|
|
258
|
-
if (response) {
|
|
259
|
-
return this.createEntity(response);
|
|
260
|
-
}
|
|
261
|
-
return null;
|
|
262
285
|
});
|
|
286
|
+
if (!entity)
|
|
287
|
+
return null;
|
|
288
|
+
const enrichedEntity = await this.enrichEntityWithRelations(entity);
|
|
289
|
+
return this.createEntity(enrichedEntity);
|
|
263
290
|
}
|
|
264
291
|
async findFirst(where) {
|
|
265
292
|
return this.context()
|