@koalarx/nest 3.1.41 → 3.1.43

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.
@@ -34,6 +34,8 @@ export declare abstract class RepositoryBase<TEntity extends EntityBase<TEntity>
34
34
  private loadEntityFromCache;
35
35
  private enrichEntityWithRelations;
36
36
  private persistRelations;
37
+ private setIdOnEntity;
38
+ private autofillCreatedId;
37
39
  protected context(transactionalClient?: TContext): TContext[TModelKey];
38
40
  protected findById(id: IComparableId): Promise<TEntity | null>;
39
41
  protected findFirst<T>(where: T): Promise<TEntity | null>;
@@ -455,34 +455,71 @@ class RepositoryBase {
455
455
  return Promise.all([]);
456
456
  }
457
457
  return Promise.all(relationCreate.relations.map((relation) => {
458
- const relationPropName = this.getPropNameFromEntitySource(relation.constructor, relationCreate.entityInstance);
459
- if (relationPropName &&
460
- !(relation[relationPropName] instanceof list_1.List)) {
461
- relation[relationPropName] =
462
- this.getConnectPrismaSchemaForRelation(relationCreate.entityInstance, response);
458
+ if (relation._action === entity_base_1.EntityActionType.update &&
459
+ relation._hasUpdate) {
460
+ return this.persistRelations(transaction, relation);
463
461
  }
464
- return transaction[(0, KlString_1.toCamelCase)(relation.constructor.name)]
465
- .create({
466
- data: this.entityToPrisma(relation),
467
- select: this.getSelectRootPrismaSchema(relation.constructor),
468
- })
469
- .then((response) => {
470
- const idPropName = this.getIdPropName(relation);
471
- if (!Array.isArray(idPropName)) {
472
- relation[idPropName] = response[idPropName];
473
- }
474
- else {
475
- idPropName.forEach((propName) => {
476
- relation[propName] = response[propName];
477
- });
462
+ else if (relation._action === entity_base_1.EntityActionType.create) {
463
+ const relationPropName = this.getPropNameFromEntitySource(relation.constructor, relationCreate.entityInstance);
464
+ if (relationPropName &&
465
+ !(relation[relationPropName] instanceof list_1.List)) {
466
+ relation[relationPropName] =
467
+ this.getConnectPrismaSchemaForRelation(relationCreate.entityInstance, response);
478
468
  }
479
- return this.persistRelations(transaction, relation);
480
- });
469
+ return transaction[(0, KlString_1.toCamelCase)(relation.constructor.name)]
470
+ .create({
471
+ data: this.entityToPrisma(relation),
472
+ select: this.getSelectRootPrismaSchema(relation.constructor),
473
+ })
474
+ .then((response) => {
475
+ const idPropName = this.getIdPropName(relation);
476
+ if (!Array.isArray(idPropName)) {
477
+ relation[idPropName] = response[idPropName];
478
+ }
479
+ else {
480
+ idPropName.forEach((propName) => {
481
+ relation[propName] = response[propName];
482
+ });
483
+ }
484
+ return this.persistRelations(transaction, relation);
485
+ });
486
+ }
481
487
  }));
482
488
  })),
483
489
  ]);
484
490
  }
485
491
  }
492
+ setIdOnEntity(entityInstance, data) {
493
+ const idPropName = this.getIdPropName(entityInstance);
494
+ if (!Array.isArray(idPropName)) {
495
+ entityInstance[idPropName] = data[idPropName];
496
+ entityInstance._id = data[idPropName];
497
+ }
498
+ else {
499
+ idPropName.forEach((propName) => {
500
+ entityInstance[propName] = data[propName];
501
+ });
502
+ entityInstance._id = idPropName.map((name) => data[name]).join('-');
503
+ }
504
+ entityInstance._action = entity_base_1.EntityActionType.update;
505
+ entityInstance._hasUpdate = false;
506
+ }
507
+ autofillCreatedId(entity, response) {
508
+ const entityProps = auto_mapping_list_1.AutoMappingList.getAllProps(entity.constructor);
509
+ this.setIdOnEntity(entity, response);
510
+ entityProps.forEach((prop) => {
511
+ let instance;
512
+ try {
513
+ instance = new (prop.type())();
514
+ }
515
+ catch {
516
+ instance = null;
517
+ }
518
+ if (instance instanceof entity_base_1.EntityBase) {
519
+ this.setIdOnEntity(entity[prop.name], response[prop.name]);
520
+ }
521
+ });
522
+ }
486
523
  context(transactionalClient) {
487
524
  const modelName = this._modelName.name;
488
525
  if (!modelName)
@@ -550,18 +587,10 @@ class RepositoryBase {
550
587
  return this.withTransaction((client) => this.context(client)
551
588
  .create({
552
589
  data: prismaEntity,
553
- include: this.getInclude(),
590
+ select: this.getSelectRootPrismaSchema(entity.constructor),
554
591
  })
555
592
  .then((response) => {
556
- const idPropName = this.getIdPropName(entity);
557
- if (!Array.isArray(idPropName)) {
558
- entity[idPropName] = response[idPropName];
559
- }
560
- else {
561
- idPropName.forEach((propName) => {
562
- entity[propName] = response[propName];
563
- });
564
- }
593
+ this.autofillCreatedId(entity, response);
565
594
  return this.persistRelations(client, entity).then(() => entity);
566
595
  }));
567
596
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koalarx/nest",
3
- "version": "3.1.41",
3
+ "version": "3.1.43",
4
4
  "description": "",
5
5
  "author": "Igor D. Rangel",
6
6
  "license": "MIT",