@koalarx/nest 3.1.12 → 3.1.13

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.
@@ -2,7 +2,13 @@ import { EntityBase } from './entity.base';
2
2
  type EntityProps<T> = Omit<{
3
3
  [K in keyof T as T[K] extends Function ? never : K]: T[K];
4
4
  }, '_id' | '_action'>;
5
- export declare function Entity<T extends new (...args: any[]) => EntityBase<any>>(id?: keyof EntityProps<InstanceType<T>>): (target: T) => {
5
+ export type CompositeId = readonly (string | number)[];
6
+ export interface IdConfig<T extends EntityProps<any>> {
7
+ single?: keyof T;
8
+ composite?: readonly (keyof T)[];
9
+ custom?: (props: T) => string | number | CompositeId;
10
+ }
11
+ export declare function Entity<T extends new (...args: any[]) => EntityBase<any>>(id?: keyof EntityProps<InstanceType<T>> | IdConfig<EntityProps<InstanceType<T>>>): (target: T) => {
6
12
  new (...args: any[]): {
7
13
  _id: import("../utils/interfaces/icomparable").IComparableId;
8
14
  _action: import("./entity.base").EntityActionType;
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Entity = Entity;
4
+ function normalizeIdConfig(id) {
5
+ if (!id)
6
+ return undefined;
7
+ if (typeof id === 'string' || typeof id === 'symbol') {
8
+ return { single: id };
9
+ }
10
+ return id;
11
+ }
4
12
  function Entity(id) {
5
13
  return function (target) {
6
14
  class NewConstructor extends target {
@@ -17,7 +25,8 @@ function Entity(id) {
17
25
  value: target.name,
18
26
  writable: false,
19
27
  });
20
- Reflect.defineMetadata('entity:id', id, NewConstructor.prototype);
28
+ const idConfig = normalizeIdConfig(id);
29
+ Reflect.defineMetadata('entity:id', idConfig, NewConstructor.prototype);
21
30
  return NewConstructor;
22
31
  };
23
32
  }
@@ -15,7 +15,9 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
15
15
  private readonly _includeFindMany?;
16
16
  constructor({ context, modelName }: RepositoryInitProps<TEntity, TContext>);
17
17
  private getIdPropName;
18
+ private getWhereByIdSchema;
18
19
  private getConnectPrismaSchemaForRelation;
20
+ private checkIdHasValue;
19
21
  private getSelectRootPrismaSchema;
20
22
  private getSelectWithRelationsId;
21
23
  private getPropNameFromEntitySource;
@@ -24,14 +24,26 @@ class RepositoryBase {
24
24
  getIdPropName(entity) {
25
25
  return (Reflect.getMetadata('entity:id', entity ? entity.constructor.prototype : this._modelName.prototype) ?? 'id');
26
26
  }
27
- getConnectPrismaSchemaForRelation(entity, data) {
27
+ getWhereByIdSchema(entity, value) {
28
28
  const propIdName = this.getIdPropName(entity);
29
+ if (Array.isArray(propIdName)) {
30
+ const whereSchema = {};
31
+ propIdName.forEach((propName) => {
32
+ whereSchema[propName] = value[propName];
33
+ });
34
+ return whereSchema;
35
+ }
36
+ return { [propIdName]: value[propIdName] };
37
+ }
38
+ getConnectPrismaSchemaForRelation(entity, data) {
29
39
  return {
30
- connect: {
31
- [propIdName]: (data ?? entity)[propIdName],
32
- },
40
+ connect: this.getWhereByIdSchema(entity, data ?? entity),
33
41
  };
34
42
  }
43
+ checkIdHasValue(entity, value) {
44
+ const result = this.getWhereByIdSchema(entity, value);
45
+ return Object.values(result).every((val) => val !== undefined && val !== null);
46
+ }
35
47
  getSelectRootPrismaSchema(entity) {
36
48
  const selectSchema = {};
37
49
  Object.keys(entity)
@@ -62,18 +74,14 @@ class RepositoryBase {
62
74
  }
63
75
  if (instance instanceof entity_base_1.EntityBase) {
64
76
  selectSchema[prop.name] = {
65
- select: {
66
- [this.getIdPropName(entity[prop.name])]: true,
67
- },
77
+ select: this.getSelectRootPrismaSchema(instance),
68
78
  };
69
79
  }
70
80
  else if (instance instanceof list_1.List) {
71
81
  const list = new entity()[prop.name];
72
82
  const entityInstance = list.entityType;
73
83
  selectSchema[prop.name] = {
74
- select: {
75
- [this.getIdPropName(entityInstance)]: true,
76
- },
84
+ select: this.getSelectRootPrismaSchema(new entityInstance()),
77
85
  };
78
86
  }
79
87
  else {
@@ -174,12 +182,10 @@ class RepositoryBase {
174
182
  .forEach((key) => {
175
183
  if (entity[key] instanceof entity_base_1.EntityBase) {
176
184
  if (entity[key]._action === entity_base_1.EntityActionType.create) {
177
- if (entity[key][this.getIdPropName(entity)]) {
185
+ if (entity[key] && this.checkIdHasValue(entity, entity[key])) {
178
186
  prismaSchema[key] = {
179
187
  connectOrCreate: {
180
- where: {
181
- [this.getIdPropName(entity)]: entity[key][this.getIdPropName(entity)],
182
- },
188
+ where: this.getWhereByIdSchema(entity[key].constructor, entity[key]),
183
189
  create: this.entityToPrisma(entity[key]),
184
190
  },
185
191
  };
@@ -264,11 +270,9 @@ class RepositoryBase {
264
270
  return;
265
271
  }
266
272
  const relationEntity = auto_mapping_list_1.AutoMappingList.getSourceByName(propDef?.type ?? '');
267
- if (relationEntity) {
273
+ if (relationEntity && data[propName]) {
268
274
  relationKeys.push(propName);
269
- relationQueries.push(this.loadRelationForEntity({
270
- [this.getIdPropName(relationEntity)]: data[propName][this.getIdPropName(relationEntity)],
271
- }, relationEntity));
275
+ relationQueries.push(this.loadRelationForEntity(this.getWhereByIdSchema(relationEntity, data[propName]), relationEntity));
272
276
  }
273
277
  });
274
278
  if (relationQueries.length > 0) {
@@ -285,6 +289,9 @@ class RepositoryBase {
285
289
  ...relationCreates.map((relationCreate) => transaction[relationCreate.modelName]
286
290
  .create(relationCreate.schema)
287
291
  .then((response) => {
292
+ if (relationCreate.relations.length === 0) {
293
+ return Promise.all([]);
294
+ }
288
295
  return Promise.all(relationCreate.relations.map((relation) => {
289
296
  const relationPropName = this.getPropNameFromEntitySource(relation, relationCreate.entityInstance);
290
297
  if (relationPropName &&
@@ -298,8 +305,15 @@ class RepositoryBase {
298
305
  select: this.getSelectRootPrismaSchema(relation),
299
306
  })
300
307
  .then((response) => {
301
- relation[this.getIdPropName(relation)] =
302
- response[this.getIdPropName(relation)];
308
+ const idPropName = this.getIdPropName(relation);
309
+ if (!Array.isArray(idPropName)) {
310
+ relation[idPropName] = response[idPropName];
311
+ }
312
+ else {
313
+ idPropName.forEach((propName) => {
314
+ relation[propName] = response[propName];
315
+ });
316
+ }
303
317
  return this.persistRelations(transaction, relation);
304
318
  });
305
319
  }));
@@ -321,7 +335,9 @@ class RepositoryBase {
321
335
  async findById(id) {
322
336
  const data = await this.context().findFirst({
323
337
  select: this.getSelectWithRelationsId(this._modelName.prototype.constructor),
324
- where: { [this.getIdPropName()]: id },
338
+ where: this.getWhereByIdSchema(this._modelName.prototype.constructor, {
339
+ id,
340
+ }),
325
341
  });
326
342
  if (!data)
327
343
  return null;
@@ -370,7 +386,15 @@ class RepositoryBase {
370
386
  include: this.getInclude(),
371
387
  })
372
388
  .then((response) => {
373
- entity[this.getIdPropName()] = response[this.getIdPropName()];
389
+ const idPropName = this.getIdPropName(entity);
390
+ if (!Array.isArray(idPropName)) {
391
+ entity[idPropName] = response[idPropName];
392
+ }
393
+ else {
394
+ idPropName.forEach((propName) => {
395
+ entity[propName] = response[propName];
396
+ });
397
+ }
374
398
  return this.persistRelations(client, entity).then(() => entity);
375
399
  }));
376
400
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "3.1.12",
3
+ "version": "3.1.13",
4
4
  "description": "",
5
5
  "author": "Igor D. Rangel",
6
6
  "license": "MIT",