@punks/backend-entity-manager 0.0.402 → 0.0.404

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.
@@ -1,3 +1,4 @@
1
+ import { EntityReference } from "../models";
1
2
  import { DeepPartial } from "../types";
2
3
  export interface IEntitiesDeleteResult {
3
4
  deletedCount: number;
@@ -19,4 +20,8 @@ export interface IEntityRepository<TEntity, TEntityId, TGetConditions, TFindCond
19
20
  update(id: TEntityId, entity: DeepPartial<TEntity>): Promise<TEntity>;
20
21
  updateBy(entity: DeepPartial<TEntity>, condition: TUpdateCondition): Promise<TEntity[]>;
21
22
  upsert(id: TEntityId, entity: DeepPartial<TEntity>): Promise<TEntity>;
23
+ upsertBy({ data, filter, }: {
24
+ data: Partial<TEntity>;
25
+ filter: TFindCondition;
26
+ }): Promise<EntityReference<string>>;
22
27
  }
@@ -1,6 +1,7 @@
1
1
  import { FindManyOptions, FindOneOptions, FindOptionsRelations, FindOptionsWhere, ObjectId, ObjectLiteral, Repository } from "typeorm";
2
2
  import { IEntityRepository } from "../../../abstractions";
3
3
  import { IEntitiesDeleteResult } from "../../../abstractions/repository";
4
+ import { EntityReference } from "../../../models";
4
5
  export type UpdateCriteria<TEntity extends ObjectLiteral> = string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<TEntity>;
5
6
  export type DeleteCriteria<TEntity extends ObjectLiteral> = string | string[] | number | number[] | Date | Date[] | ObjectId | ObjectId[] | FindOptionsWhere<TEntity>;
6
7
  export declare class TypeOrmRepository<TEntity extends ObjectLiteral, TEntityId> implements IEntityRepository<TEntity, TEntityId, FindOneOptions<TEntity>, FindManyOptions<TEntity>, UpdateCriteria<TEntity>, DeleteCriteria<TEntity>> {
@@ -27,4 +28,8 @@ export declare class TypeOrmRepository<TEntity extends ObjectLiteral, TEntityId>
27
28
  update(id: TEntityId, entity: Partial<TEntity>): Promise<TEntity>;
28
29
  updateBy(entity: Partial<TEntity>, condition: UpdateCriteria<TEntity>): Promise<TEntity[]>;
29
30
  upsert(id: TEntityId, entity: Partial<TEntity>): Promise<TEntity>;
31
+ upsertBy({ data, filter, }: {
32
+ data: Partial<TEntity>;
33
+ filter: FindManyOptions<TEntity>;
34
+ }): Promise<EntityReference<string>>;
30
35
  }
@@ -1,5 +1,6 @@
1
1
  import { FilterExpression } from "../../abstractions/filters";
2
2
  import { IEntitiesDeleteResult, IEntityRepository } from "../../abstractions/repository";
3
+ import { EntityReference } from "../../models";
3
4
  export declare class InMemoryRepository<TEntity> implements IEntityRepository<TEntity, string, FilterExpression<TEntity>, FilterExpression<TEntity>, FilterExpression<TEntity>, FilterExpression<TEntity>> {
4
5
  private entities;
5
6
  exists(id: string): Promise<boolean>;
@@ -18,6 +19,10 @@ export declare class InMemoryRepository<TEntity> implements IEntityRepository<TE
18
19
  update(id: string, entity: Partial<TEntity>): Promise<TEntity>;
19
20
  updateBy(entity: Partial<TEntity>, condition: FilterExpression<TEntity>): Promise<TEntity[]>;
20
21
  upsert(id: string, entity: TEntity): Promise<TEntity>;
22
+ upsertBy({ data, filter, }: {
23
+ data: Partial<TEntity>;
24
+ filter: FilterExpression<TEntity>;
25
+ }): Promise<EntityReference<string>>;
21
26
  private getId;
22
27
  private filterEntities;
23
28
  }
package/dist/esm/index.js CHANGED
@@ -4114,6 +4114,21 @@ class TypeOrmRepository {
4114
4114
  id,
4115
4115
  });
4116
4116
  }
4117
+ async upsertBy({ data, filter, }) {
4118
+ this.logger.debug("Upsert entities by condition", { filter, data });
4119
+ const current = await this.find(filter);
4120
+ if (current) {
4121
+ const id = current.id;
4122
+ await this.update(id, data);
4123
+ return {
4124
+ id,
4125
+ };
4126
+ }
4127
+ const entity = await this.create(data);
4128
+ return {
4129
+ id: entity.id,
4130
+ };
4131
+ }
4117
4132
  }
4118
4133
 
4119
4134
  class NestEntityAuthorizationMiddleware {