xcally-nest-library 0.0.4 → 0.0.6
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.
|
3
|
+
"version": "0.0.6",
|
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,38 +1,52 @@
|
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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>;
|
14
|
+
|
20
15
|
constructor(
|
21
|
-
readonly entityManager: EntityManager,
|
16
|
+
private readonly entityManager: EntityManager,
|
22
17
|
entity: EntityTarget<E>,
|
23
18
|
paginateConfig: PaginateConfig<E>,
|
24
19
|
) {
|
25
|
-
this.repository = entityManager.getRepository(entity);
|
20
|
+
this.repository = this.entityManager.getRepository(entity);
|
26
21
|
this.paginateConfig = paginateConfig;
|
27
22
|
}
|
23
|
+
/**
|
24
|
+
* Retrieves a paginated list of entities based on the provided query.
|
25
|
+
*
|
26
|
+
* @param query - The pagination query parameters.
|
27
|
+
* @returns A promise that resolves to a paginated list of entities.
|
28
|
+
*/
|
28
29
|
getMany(query: PaginateQuery): Promise<Paginated<E>> {
|
29
30
|
return paginate(query, this.repository, this.paginateConfig);
|
30
31
|
}
|
32
|
+
/**
|
33
|
+
* Saves an entity to the database.
|
34
|
+
*
|
35
|
+
* @param entity - The entity to be saved.
|
36
|
+
* @returns A promise that resolves to the saved entity.
|
37
|
+
*/
|
31
38
|
save<T extends DeepPartial<E>>(entity: T): Promise<E> {
|
32
39
|
const res = this.repository.create({ ...entity });
|
33
40
|
return this.repository.save(res);
|
34
41
|
}
|
35
42
|
|
43
|
+
/**
|
44
|
+
* Updates an entity in the database.
|
45
|
+
*
|
46
|
+
* @param dto - The updated entity data.
|
47
|
+
* @returns A promise that resolves to the updated entity.
|
48
|
+
* @throws Error if the entity is not found.
|
49
|
+
*/
|
36
50
|
async update<T extends DeepPartial<E> & IId>(dto: T): Promise<E> {
|
37
51
|
const entity = await this.repository.findOneBy({
|
38
52
|
id: Equal(dto.id),
|
@@ -40,11 +54,40 @@ export class GenericRepository<E extends ObjectLiteral> implements IGenericRepos
|
|
40
54
|
if (!entity) {
|
41
55
|
throw new Error('Entity not found');
|
42
56
|
}
|
43
|
-
|
44
|
-
return Object.assign(entity, entityUpdated);
|
57
|
+
return this.repository.save({ id: entity.id, ...dto });
|
45
58
|
}
|
46
59
|
|
60
|
+
/**
|
61
|
+
* Counts the number of entities that match the provided options.
|
62
|
+
*
|
63
|
+
* @param options - The options to filter the entities.
|
64
|
+
* @returns A promise that resolves to the number of entities that match the options.
|
65
|
+
*/
|
47
66
|
count(options: FindOptionsWhere<E>): Promise<number> {
|
48
67
|
return this.repository.count(options);
|
49
68
|
}
|
69
|
+
|
70
|
+
/**
|
71
|
+
* Retrieves all entities from the database.
|
72
|
+
*
|
73
|
+
* @returns A promise that resolves to an array of entities.
|
74
|
+
*/
|
75
|
+
findAll(): Promise<E[]> {
|
76
|
+
return this.repository.find();
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Retrieves the total count of all entities in the database.
|
81
|
+
*
|
82
|
+
* @returns A promise that resolves to the total count of entities.
|
83
|
+
*/
|
84
|
+
countAll(): Promise<number> {
|
85
|
+
return this.repository.count();
|
86
|
+
}
|
87
|
+
|
88
|
+
findById(id: number): Promise<E> {
|
89
|
+
return this.repository.findOneBy({
|
90
|
+
id: Equal(id),
|
91
|
+
} as unknown as FindOptionsWhere<E>);
|
92
|
+
}
|
50
93
|
}
|