@koalarx/nest 1.12.3 → 1.12.5

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.
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Entity = Entity;
4
4
  function Entity(id) {
5
5
  return function (target) {
6
- const originalConstructor = target;
7
6
  class NewConstructor extends target {
8
7
  constructor(...args) {
9
8
  super(...args);
@@ -12,13 +11,6 @@ function Entity(id) {
12
11
  }
13
12
  }
14
13
  }
15
- const newConstructor = function (...args) {
16
- const instance = Reflect.construct(originalConstructor, args, newConstructor);
17
- if (typeof instance.automap === 'function') {
18
- instance.automap(args[0]);
19
- }
20
- return instance;
21
- };
22
14
  Object.setPrototypeOf(NewConstructor.prototype, target.prototype);
23
15
  Object.setPrototypeOf(NewConstructor, target);
24
16
  Object.defineProperty(NewConstructor, 'name', {
@@ -2,12 +2,13 @@ import { Type } from '@nestjs/common';
2
2
  import { ListResponse } from '..';
3
3
  import { PaginationDto } from '../dtos/pagination.dto';
4
4
  import { IComparableId } from '../utils/interfaces/icomparable';
5
+ import { List } from '../utils/list';
5
6
  import { EntityBase } from './entity.base';
6
7
  import { PrismaTransactionalClient } from './prisma-transactional-client';
7
- type RepositoryInclude<TEntity> = {
8
- [key in keyof TEntity]?: boolean | RepositoryInclude<TEntity[keyof TEntity]>;
9
- };
10
- interface RepositoryInitProps<TEntity> {
8
+ type RepositoryInclude<TEntity> = Omit<{
9
+ [K in keyof TEntity as TEntity[K] extends Function ? never : K]?: boolean | (TEntity[K] extends List<infer U> ? RepositoryInclude<U> : RepositoryInclude<TEntity[K]>);
10
+ }, '_id' | '_action'>;
11
+ interface RepositoryInitProps<TEntity extends EntityBase<TEntity>> {
11
12
  context: PrismaTransactionalClient;
12
13
  modelName: Type<TEntity>;
13
14
  transactionContext?: Type<PrismaTransactionalClient>;
@@ -33,5 +34,6 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
33
34
  private createEntity;
34
35
  private orphanRemoval;
35
36
  private getIdPropName;
37
+ private getInclude;
36
38
  }
37
39
  export {};
@@ -23,7 +23,7 @@ class RepositoryBase {
23
23
  async findById(id) {
24
24
  return this.context()
25
25
  .findFirst({
26
- include: this._include,
26
+ include: this.getInclude(),
27
27
  where: { [this.getIdPropName()]: id },
28
28
  })
29
29
  .then((response) => {
@@ -36,7 +36,7 @@ class RepositoryBase {
36
36
  async findFirst(where) {
37
37
  return this.context()
38
38
  .findFirst({
39
- include: this._include,
39
+ include: this.getInclude(),
40
40
  where,
41
41
  })
42
42
  .then((response) => {
@@ -49,7 +49,7 @@ class RepositoryBase {
49
49
  async findUnique(where) {
50
50
  return this.context()
51
51
  .findUnique({
52
- include: this._include,
52
+ include: this.getInclude(),
53
53
  where,
54
54
  })
55
55
  .then((response) => {
@@ -78,7 +78,7 @@ class RepositoryBase {
78
78
  return this.context()
79
79
  .create({
80
80
  data: prismaEntity,
81
- include: this._include,
81
+ include: this.getInclude(),
82
82
  })
83
83
  .then((response) => this.createEntity(response));
84
84
  }
@@ -158,10 +158,28 @@ class RepositoryBase {
158
158
  }
159
159
  }
160
160
  else if (entity[key] instanceof entity_base_1.EntityBase) {
161
- prismaSchema[key] =
162
- entity[key]._action === entity_base_1.EntityActionType.create
163
- ? { create: this.entityToPrisma(entity[key]) }
164
- : { update: this.entityToPrisma(entity[key]) };
161
+ if (entity[key]._action === entity_base_1.EntityActionType.create) {
162
+ if (entity[key][this.getIdPropName()]) {
163
+ prismaSchema[key] = {
164
+ connectOrCreate: {
165
+ where: {
166
+ [this.getIdPropName()]: entity[key][this.getIdPropName()],
167
+ },
168
+ create: this.entityToPrisma(entity[key]),
169
+ },
170
+ };
171
+ }
172
+ else {
173
+ prismaSchema[key] = {
174
+ create: this.entityToPrisma(entity[key]),
175
+ };
176
+ }
177
+ }
178
+ else {
179
+ prismaSchema[key] = {
180
+ update: this.entityToPrisma(entity[key]),
181
+ };
182
+ }
165
183
  }
166
184
  else {
167
185
  prismaSchema[key] = entity[key];
@@ -180,7 +198,7 @@ class RepositoryBase {
180
198
  }
181
199
  findManySchema(where, pagination) {
182
200
  return {
183
- include: this._include,
201
+ include: this.getInclude(),
184
202
  where,
185
203
  orderBy: pagination?.generateOrderBy(),
186
204
  skip: pagination?.skip(),
@@ -203,5 +221,20 @@ class RepositoryBase {
203
221
  getIdPropName() {
204
222
  return Reflect.getMetadata('entity:id', this._modelName.prototype) ?? 'id';
205
223
  }
224
+ getInclude(include) {
225
+ include = include ?? this._include ?? {};
226
+ const result = {};
227
+ Object.keys(include).forEach((key) => {
228
+ if (typeof include[key] === 'boolean') {
229
+ result[key] = include[key];
230
+ }
231
+ else {
232
+ result[key] = {
233
+ include: this.getInclude(include[key]),
234
+ };
235
+ }
236
+ });
237
+ return result;
238
+ }
206
239
  }
207
240
  exports.RepositoryBase = RepositoryBase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "1.12.3",
3
+ "version": "1.12.5",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",