@hedhog/pagination 0.0.10 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
|
|
1
1
|
import type { BaseModel, FindManyArgs, PaginatedResult, PaginationParams } from './types/pagination.types';
|
2
2
|
export declare class PaginationService {
|
3
3
|
private readonly logger;
|
4
|
-
paginate<T, M extends BaseModel>(model: M, paginationParams: PaginationParams, customQuery?: FindManyArgs<M
|
4
|
+
paginate<T, M extends BaseModel>(model: M, paginationParams: PaginationParams, customQuery?: FindManyArgs<M>, translationKey?: string): Promise<PaginatedResult<T>>;
|
5
5
|
extractFieldNames(model: Record<string, any>): string[];
|
6
6
|
isInvalidField(sortField: string, model: BaseModel): boolean;
|
7
7
|
isInvalidFields(fields: string[], model: BaseModel): boolean;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"pagination.service.d.ts","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EACZ,eAAe,EACf,gBAAgB,EACjB,MAAM,0BAA0B,CAAC;
|
1
|
+
{"version":3,"file":"pagination.service.d.ts","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,SAAS,EACT,YAAY,EACZ,eAAe,EACf,gBAAgB,EACjB,MAAM,0BAA0B,CAAC;AAGlC,qBACa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;IAEvD,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,SAAS,EACnC,KAAK,EAAE,CAAC,EACR,gBAAgB,EAAE,gBAAgB,EAClC,WAAW,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,EAC7B,cAAc,CAAC,EAAE,MAAM,GACtB,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAoH9B,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EAAE;IAgBvD,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO;IAI5D,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO;CAK7D"}
|
@@ -11,11 +11,12 @@ exports.PaginationService = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
12
12
|
const pagination_constants_1 = require("./constants/pagination.constants");
|
13
13
|
const patination_enums_1 = require("./enums/patination.enums");
|
14
|
+
const utils_1 = require("@hedhog/utils");
|
14
15
|
let PaginationService = PaginationService_1 = class PaginationService {
|
15
16
|
constructor() {
|
16
17
|
this.logger = new common_1.Logger(PaginationService_1.name);
|
17
18
|
}
|
18
|
-
async paginate(model, paginationParams, customQuery) {
|
19
|
+
async paginate(model, paginationParams, customQuery, translationKey) {
|
19
20
|
try {
|
20
21
|
if (!model) {
|
21
22
|
throw new common_1.BadRequestException('Model is required');
|
@@ -35,7 +36,6 @@ let PaginationService = PaginationService_1 = class PaginationService {
|
|
35
36
|
let sortOrderCondition = {
|
36
37
|
id: paginationParams.sortOrder || patination_enums_1.PageOrderDirection.Asc,
|
37
38
|
};
|
38
|
-
console.log('sortField:', sortField);
|
39
39
|
if (sortField) {
|
40
40
|
const invalid = this.isInvalidField(sortField, model);
|
41
41
|
if (invalid) {
|
@@ -50,7 +50,6 @@ let PaginationService = PaginationService_1 = class PaginationService {
|
|
50
50
|
throw new common_1.BadRequestException('Search must be a string');
|
51
51
|
}
|
52
52
|
}
|
53
|
-
console.log('fields:', fields);
|
54
53
|
if (fields) {
|
55
54
|
const invalidFields = this.isInvalidFields(fields, model);
|
56
55
|
if (invalidFields) {
|
@@ -74,13 +73,14 @@ let PaginationService = PaginationService_1 = class PaginationService {
|
|
74
73
|
query.include = customQuery === null || customQuery === void 0 ? void 0 : customQuery.include;
|
75
74
|
delete query.select;
|
76
75
|
}
|
77
|
-
|
78
|
-
console.log('model:', model);
|
79
|
-
const [total, data] = await Promise.all([
|
76
|
+
let [total, data] = await Promise.all([
|
80
77
|
model.count({ where: (customQuery === null || customQuery === void 0 ? void 0 : customQuery.where) || {} }),
|
81
78
|
model.findMany(query),
|
82
79
|
]);
|
83
80
|
const lastPage = Math.ceil(total / pageSize);
|
81
|
+
if (translationKey) {
|
82
|
+
data = data.map((item) => (0, utils_1.itemTranslations)(translationKey, item));
|
83
|
+
}
|
84
84
|
return {
|
85
85
|
total,
|
86
86
|
lastPage,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"pagination.service.js","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,2EAG0C;AAC1C,+DAA8D;
|
1
|
+
{"version":3,"file":"pagination.service.js","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,2EAG0C;AAC1C,+DAA8D;AAO9D,yCAAiD;AAG1C,IAAM,iBAAiB,yBAAvB,MAAM,iBAAiB;IAAvB;QACY,WAAM,GAAG,IAAI,eAAM,CAAC,mBAAiB,CAAC,IAAI,CAAC,CAAC;IAoJ/D,CAAC;IAlJC,KAAK,CAAC,QAAQ,CACZ,KAAQ,EACR,gBAAkC,EAClC,WAA6B,EAC7B,cAAuB;QAEvB,IAAI,CAAC;YACH,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,MAAM,IAAI,4BAAmB,CAAC,mBAAmB,CAAC,CAAC;YACrD,CAAC;YAED,MAAM,IAAI,GAAG,MAAM,CAAC,gBAAgB,CAAC,IAAI,IAAI,mCAAY,CAAC,CAAC;YAC3D,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,QAAQ,IAAI,wCAAiB,CAAC,CAAC;YACxE,MAAM,MAAM,GAAG,gBAAgB,CAAC,MAAM,IAAI,IAAI,CAAC;YAC/C,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,IAAI,IAAI,CAAC;YACrD,MAAM,SAAS,GAAG,gBAA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AC,GAAG,CAAC,EAAE,CAAC;gBACzC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACvB,CAAC;QACH,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,cAAc,CAAC,SAAiB,EAAE,KAAgB;QAChD,OAAO,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACjE,CAAC;IAED,eAAe,CAAC,MAAgB,EAAE,KAAgB;QAChD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAC7B,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CACpD,CAAC;IACJ,CAAC;CACF,CAAA;AArJY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;GACA,iBAAiB,CAqJ7B"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hedhog/pagination",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.12",
|
4
4
|
"private": false,
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"scripts": {
|
@@ -13,8 +13,12 @@
|
|
13
13
|
"description": "",
|
14
14
|
"peerDependencies": {
|
15
15
|
"@prisma/client": "^5.17.0",
|
16
|
+
"@hedhog/utils": "latest",
|
16
17
|
"@hedhog/prisma": "latest",
|
17
18
|
"class-transformer": "^0.5.1",
|
18
19
|
"class-validator": "^0.14.1"
|
20
|
+
},
|
21
|
+
"devDependencies": {
|
22
|
+
"@hedhog/utils": "latest"
|
19
23
|
}
|
20
24
|
}
|
@@ -10,6 +10,7 @@ import type {
|
|
10
10
|
PaginatedResult,
|
11
11
|
PaginationParams,
|
12
12
|
} from './types/pagination.types';
|
13
|
+
import { itemTranslations } from '@hedhog/utils';
|
13
14
|
|
14
15
|
@Injectable()
|
15
16
|
export class PaginationService {
|
@@ -19,6 +20,7 @@ export class PaginationService {
|
|
19
20
|
model: M,
|
20
21
|
paginationParams: PaginationParams,
|
21
22
|
customQuery?: FindManyArgs<M>,
|
23
|
+
translationKey?: string,
|
22
24
|
): Promise<PaginatedResult<T>> {
|
23
25
|
try {
|
24
26
|
if (!model) {
|
@@ -45,8 +47,6 @@ export class PaginationService {
|
|
45
47
|
id: paginationParams.sortOrder || PageOrderDirection.Asc,
|
46
48
|
};
|
47
49
|
|
48
|
-
console.log('sortField:', sortField);
|
49
|
-
|
50
50
|
if (sortField) {
|
51
51
|
const invalid = this.isInvalidField(sortField, model);
|
52
52
|
if (invalid) {
|
@@ -68,8 +68,6 @@ export class PaginationService {
|
|
68
68
|
}
|
69
69
|
}
|
70
70
|
|
71
|
-
console.log('fields:', fields);
|
72
|
-
|
73
71
|
if (fields) {
|
74
72
|
const invalidFields = this.isInvalidFields(fields, model);
|
75
73
|
|
@@ -108,16 +106,17 @@ export class PaginationService {
|
|
108
106
|
delete query.select;
|
109
107
|
}
|
110
108
|
|
111
|
-
|
112
|
-
console.log('model:', model);
|
113
|
-
|
114
|
-
const [total, data] = await Promise.all([
|
109
|
+
let [total, data] = await Promise.all([
|
115
110
|
model.count({ where: customQuery?.where || {} }),
|
116
111
|
model.findMany(query),
|
117
112
|
]);
|
118
113
|
|
119
114
|
const lastPage = Math.ceil(total / pageSize);
|
120
115
|
|
116
|
+
if (translationKey) {
|
117
|
+
data = data.map((item: any) => itemTranslations(translationKey, item));
|
118
|
+
}
|
119
|
+
|
121
120
|
return {
|
122
121
|
total,
|
123
122
|
lastPage,
|