xcally-nest-library 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcally-nest-library",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -62,6 +62,7 @@
62
62
  "eslint-plugin-prettier": "^5.1.3",
63
63
  "jest": "^29.7.0",
64
64
  "prettier": "^3.2.5",
65
+ "rimraf": "^5.0.5",
65
66
  "source-map-support": "^0.5.21",
66
67
  "supertest": "^6.3.4",
67
68
  "ts-jest": "^29.1.2",
@@ -11,12 +11,13 @@ interface IId {
11
11
  export class GenericRepository<E extends ObjectLiteral> {
12
12
  protected repository: Repository<E>;
13
13
  protected paginateConfig: PaginateConfig<E>;
14
+
14
15
  constructor(
15
- readonly entityManager: EntityManager,
16
+ private readonly entityManager: EntityManager,
16
17
  entity: EntityTarget<E>,
17
18
  paginateConfig: PaginateConfig<E>,
18
19
  ) {
19
- this.repository = entityManager.getRepository(entity);
20
+ this.repository = this.entityManager.getRepository(entity);
20
21
  this.paginateConfig = paginateConfig;
21
22
  }
22
23
  /**
@@ -47,13 +48,11 @@ export class GenericRepository<E extends ObjectLiteral> {
47
48
  * @throws Error if the entity is not found.
48
49
  */
49
50
  async update<T extends DeepPartial<E> & IId>(dto: T): Promise<E> {
50
- const entity = await this.repository.findOneBy({
51
- id: Equal(dto.id),
52
- } as unknown as FindOptionsWhere<E>);
53
- if (!entity) {
51
+ const entityUpdated = await this.repository.update(dto.id, dto as any);
52
+ if (!entityUpdated.affected) {
54
53
  throw new Error('Entity not found');
55
54
  }
56
- return this.repository.save({ id: entity.id, ...dto });
55
+ return this.repository.findOneBy({ id: Equal(dto.id) } as unknown as FindOptionsWhere<E>);
57
56
  }
58
57
 
59
58
  /**
@@ -83,4 +82,10 @@ export class GenericRepository<E extends ObjectLiteral> {
83
82
  countAll(): Promise<number> {
84
83
  return this.repository.count();
85
84
  }
85
+
86
+ findById(id: number): Promise<E> {
87
+ return this.repository.findOneBy({
88
+ id: Equal(id),
89
+ } as unknown as FindOptionsWhere<E>);
90
+ }
86
91
  }