xcally-nest-library 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "xcally-nest-library",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "",
8
8
  "license": "UNLICENSED",
9
9
  "scripts": {
10
+ "bump:patch": "npm version patch -m \"Bump version to %s\"",
10
11
  "prepublishOnly": "pnpm build",
11
12
  "build": "tsc",
12
13
  "build:watch": "rimraf dist && tsc-watch -b -v",
@@ -1,20 +1,14 @@
1
1
  import { Repository, EntityTarget, EntityManager, Equal, FindOptionsWhere, ObjectLiteral, DeepPartial } from 'typeorm';
2
- import { Injectable } from '@nestjs/common';
3
2
  import { PaginateConfig, PaginateQuery, Paginated, paginate } from 'nestjs-paginate';
4
3
 
5
4
  interface IId {
6
5
  id: number;
7
6
  }
8
7
 
9
- interface IGenericRepository<E> {
10
- save<T extends DeepPartial<E>>(entity: T): Promise<E>;
11
- update<T extends DeepPartial<E> & IId>(entity: T): Promise<E>;
12
- count(options: FindOptionsWhere<E>): Promise<number>;
13
- getMany(query: PaginateQuery): Promise<Paginated<E>>;
14
- }
15
-
16
- @Injectable()
17
- export class GenericRepository<E extends ObjectLiteral> implements IGenericRepository<E> {
8
+ /**
9
+ * Represents a generic repository for managing entities.
10
+ */
11
+ export class GenericRepository<E extends ObjectLiteral> {
18
12
  protected repository: Repository<E>;
19
13
  protected paginateConfig: PaginateConfig<E>;
20
14
  constructor(
@@ -25,14 +19,33 @@ export class GenericRepository<E extends ObjectLiteral> implements IGenericRepos
25
19
  this.repository = entityManager.getRepository(entity);
26
20
  this.paginateConfig = paginateConfig;
27
21
  }
22
+ /**
23
+ * Retrieves a paginated list of entities based on the provided query.
24
+ *
25
+ * @param query - The pagination query parameters.
26
+ * @returns A promise that resolves to a paginated list of entities.
27
+ */
28
28
  getMany(query: PaginateQuery): Promise<Paginated<E>> {
29
29
  return paginate(query, this.repository, this.paginateConfig);
30
30
  }
31
+ /**
32
+ * Saves an entity to the database.
33
+ *
34
+ * @param entity - The entity to be saved.
35
+ * @returns A promise that resolves to the saved entity.
36
+ */
31
37
  save<T extends DeepPartial<E>>(entity: T): Promise<E> {
32
38
  const res = this.repository.create({ ...entity });
33
39
  return this.repository.save(res);
34
40
  }
35
41
 
42
+ /**
43
+ * Updates an entity in the database.
44
+ *
45
+ * @param dto - The updated entity data.
46
+ * @returns A promise that resolves to the updated entity.
47
+ * @throws Error if the entity is not found.
48
+ */
36
49
  async update<T extends DeepPartial<E> & IId>(dto: T): Promise<E> {
37
50
  const entity = await this.repository.findOneBy({
38
51
  id: Equal(dto.id),
@@ -40,11 +53,34 @@ export class GenericRepository<E extends ObjectLiteral> implements IGenericRepos
40
53
  if (!entity) {
41
54
  throw new Error('Entity not found');
42
55
  }
43
- const entityUpdated = await this.repository.save({ id: entity.id, ...dto });
44
- return Object.assign(entity, entityUpdated);
56
+ return this.repository.save({ id: entity.id, ...dto });
45
57
  }
46
58
 
59
+ /**
60
+ * Counts the number of entities that match the provided options.
61
+ *
62
+ * @param options - The options to filter the entities.
63
+ * @returns A promise that resolves to the number of entities that match the options.
64
+ */
47
65
  count(options: FindOptionsWhere<E>): Promise<number> {
48
66
  return this.repository.count(options);
49
67
  }
68
+
69
+ /**
70
+ * Retrieves all entities from the database.
71
+ *
72
+ * @returns A promise that resolves to an array of entities.
73
+ */
74
+ findAll(): Promise<E[]> {
75
+ return this.repository.find();
76
+ }
77
+
78
+ /**
79
+ * Retrieves the total count of all entities in the database.
80
+ *
81
+ * @returns A promise that resolves to the total count of entities.
82
+ */
83
+ countAll(): Promise<number> {
84
+ return this.repository.count();
85
+ }
50
86
  }