@punks/backend-entity-manager 0.0.401 → 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;
@@ -9,6 +10,8 @@ export interface IEntityRepository<TEntity, TEntityId, TGetConditions, TFindCond
9
10
  getBy(condition: TGetConditions): Promise<TEntity | undefined>;
10
11
  all(): Promise<TEntity[]>;
11
12
  find(condition: TFindCondition): Promise<TEntity[]>;
13
+ findOne(condition: TFindCondition): Promise<TEntity | undefined>;
14
+ findOneOrFail(condition: TFindCondition): Promise<TEntity>;
12
15
  findById(id: TEntityId[]): Promise<TEntity[]>;
13
16
  count(condition: TFindCondition): Promise<number>;
14
17
  delete(id: TEntityId): Promise<void>;
@@ -17,4 +20,8 @@ export interface IEntityRepository<TEntity, TEntityId, TGetConditions, TFindCond
17
20
  update(id: TEntityId, entity: DeepPartial<TEntity>): Promise<TEntity>;
18
21
  updateBy(entity: DeepPartial<TEntity>, condition: TUpdateCondition): Promise<TEntity[]>;
19
22
  upsert(id: TEntityId, entity: DeepPartial<TEntity>): Promise<TEntity>;
23
+ upsertBy({ data, filter, }: {
24
+ data: Partial<TEntity>;
25
+ filter: TFindCondition;
26
+ }): Promise<EntityReference<string>>;
20
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>> {
@@ -18,7 +19,8 @@ export declare class TypeOrmRepository<TEntity extends ObjectLiteral, TEntityId>
18
19
  all(): Promise<TEntity[]>;
19
20
  count(condition: FindManyOptions<TEntity>): Promise<number>;
20
21
  find(condition: FindManyOptions<TEntity>): Promise<TEntity[]>;
21
- findOne(condition: FindManyOptions<TEntity>): Promise<TEntity>;
22
+ findOne(condition: FindManyOptions<TEntity>): Promise<TEntity | undefined>;
23
+ findOneOrFail(condition: FindManyOptions<TEntity>): Promise<TEntity>;
22
24
  findById(id: TEntityId[]): Promise<TEntity[]>;
23
25
  delete(id: TEntityId): Promise<void>;
24
26
  deleteBy(condition: DeleteCriteria<TEntity>): Promise<IEntitiesDeleteResult>;
@@ -26,4 +28,8 @@ export declare class TypeOrmRepository<TEntity extends ObjectLiteral, TEntityId>
26
28
  update(id: TEntityId, entity: Partial<TEntity>): Promise<TEntity>;
27
29
  updateBy(entity: Partial<TEntity>, condition: UpdateCriteria<TEntity>): Promise<TEntity[]>;
28
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>>;
29
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>;
@@ -8,6 +9,8 @@ export declare class InMemoryRepository<TEntity> implements IEntityRepository<TE
8
9
  getBy(condition: FilterExpression<TEntity>): Promise<TEntity | undefined>;
9
10
  all(): Promise<TEntity[]>;
10
11
  find(condition: FilterExpression<TEntity>): Promise<TEntity[]>;
12
+ findOne(condition: FilterExpression<TEntity>): Promise<TEntity | undefined>;
13
+ findOneOrFail(condition: FilterExpression<TEntity>): Promise<TEntity>;
11
14
  count(condition: FilterExpression<TEntity>): Promise<number>;
12
15
  findById(id: string[]): Promise<TEntity[]>;
13
16
  delete(id: string): Promise<void>;
@@ -16,6 +19,10 @@ export declare class InMemoryRepository<TEntity> implements IEntityRepository<TE
16
19
  update(id: string, entity: Partial<TEntity>): Promise<TEntity>;
17
20
  updateBy(entity: Partial<TEntity>, condition: FilterExpression<TEntity>): Promise<TEntity[]>;
18
21
  upsert(id: string, entity: TEntity): Promise<TEntity>;
22
+ upsertBy({ data, filter, }: {
23
+ data: Partial<TEntity>;
24
+ filter: FilterExpression<TEntity>;
25
+ }): Promise<EntityReference<string>>;
19
26
  private getId;
20
27
  private filterEntities;
21
28
  }
package/dist/esm/index.js CHANGED
@@ -4040,6 +4040,14 @@ class TypeOrmRepository {
4040
4040
  const items = await this.innerRepository.find(condition);
4041
4041
  return items[0];
4042
4042
  }
4043
+ async findOneOrFail(condition) {
4044
+ this.logger.debug("Finding entity by condition", { condition });
4045
+ const items = await this.innerRepository.find(condition);
4046
+ if (items.length === 0) {
4047
+ throw new Error("Entity not found");
4048
+ }
4049
+ return items[0];
4050
+ }
4043
4051
  async findById(id) {
4044
4052
  this.logger.debug("Finding entities by id", { id });
4045
4053
  return await this.innerRepository.findBy({
@@ -4106,6 +4114,21 @@ class TypeOrmRepository {
4106
4114
  id,
4107
4115
  });
4108
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
+ }
4109
4132
  }
4110
4133
 
4111
4134
  class NestEntityAuthorizationMiddleware {