@hedhog/pagination 0.0.6 → 0.0.8
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.
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# <p align="center">hedhog/pagination</p>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://avatars.githubusercontent.com/u/177489127?s=200&v=4" alt="Hedhog Avatar" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
**Hedhog Pagination** is a powerful and flexible pagination library designed for use with HedHog-based projects. It simplifies the process of paginating data results from your database queries, making it easy to handle large datasets and provide users with a smooth and efficient browsing experience.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Flexible Pagination**: Supports pagination with configurable page sizes and sorting options.
|
|
12
|
+
- **Search Functionality**: Enables search functionality within paginated results.
|
|
13
|
+
- **Field Selection**: Allows selecting specific fields to be returned in the results.
|
|
14
|
+
- **Sorting**: Provides sorting capabilities based on specified fields and order.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
This library is an integral part of the HedHog framework and should be installed as a dependency in your HedHog project. Ensure that the necessary dependencies are configured in your HedHog project.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm i @hedhog/pagination
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Folder Structure
|
|
25
|
+
|
|
26
|
+
```plaintext
|
|
27
|
+
pagination/
|
|
28
|
+
├── dist/ # Compiled JavaScript files from build
|
|
29
|
+
├── node_modules/ # Discardable folder with all module dependencies
|
|
30
|
+
├── src/
|
|
31
|
+
│ ├── constants/
|
|
32
|
+
│ │ └── pagination.constants.ts # Constants related to pagination
|
|
33
|
+
│ ├── decorator/
|
|
34
|
+
│ │ └── pagination.decorator.ts # Decorators for pagination
|
|
35
|
+
│ ├── dto/
|
|
36
|
+
│ │ └── pagination.dto.ts # Data Transfer Object for pagination parameters
|
|
37
|
+
│ ├── enums/
|
|
38
|
+
│ │ └── pagination.enums.ts # Enums for pagination
|
|
39
|
+
│ ├── types/
|
|
40
|
+
│ │ └── pagination.types.ts # Type definitions for pagination
|
|
41
|
+
│ ├── index.ts # Entry point for pagination files
|
|
42
|
+
│ ├── pagination.module.ts # Module definition for pagination functionalities
|
|
43
|
+
│ ├── pagination.service.spec.ts # Unit tests for PaginationService
|
|
44
|
+
│ └── pagination.service.ts # Service class for pagination logic
|
|
45
|
+
├── .gitignore # Specifies which files Git should ignore
|
|
46
|
+
├── package.json # Manages dependencies and scripts for the library
|
|
47
|
+
├── package-lock.json # Lock file for dependencies
|
|
48
|
+
├── README.md # Documentation for the library
|
|
49
|
+
├── tsconfig.lib.json # TypeScript configuration for library builds
|
|
50
|
+
├── tsconfig.production.json # TypeScript configuration for production builds
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage Example
|
|
54
|
+
|
|
55
|
+
The core of the `@hedhog/pagination` library is the PaginationService. It provides the paginate method to handle pagination logic for your models.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { Injectable } from '@nestjs/common';
|
|
59
|
+
import { PaginationService } from '@hedhog/pagination';
|
|
60
|
+
import { PrismaService } from '@hedhog/prisma';
|
|
61
|
+
|
|
62
|
+
@Injectable()
|
|
63
|
+
export class MyService {
|
|
64
|
+
constructor(
|
|
65
|
+
private readonly paginationService: PaginationService,
|
|
66
|
+
private readonly prismaService: PrismaService,
|
|
67
|
+
) {}
|
|
68
|
+
|
|
69
|
+
async getPaginatedResults(paginationParams: PaginationParams) {
|
|
70
|
+
const model = this.prismaService.myModel; // Replace with your model
|
|
71
|
+
return this.paginationService.paginate(model, paginationParams);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Parameters
|
|
77
|
+
|
|
78
|
+
- **model**: The model to paginate, typically from your ORM (e.g., Prisma).
|
|
79
|
+
- **paginationParams**: An object containing pagination parameters:
|
|
80
|
+
- **page**: The current page number (default: 1).
|
|
81
|
+
- **pageSize**: The number of items per page (default: 10).
|
|
82
|
+
- **search**: The search query string.
|
|
83
|
+
- **sortField**: The field to sort by.
|
|
84
|
+
- **sortOrder**: The order of sorting (ascending or descending).
|
|
85
|
+
- **fields**: Comma-separated list of fields to include in the result.
|
|
@@ -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;AAElC,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,GAC5B,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,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;AAElC,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,GAC5B,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAwG9B,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,EAAE;IAavD,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"}
|
|
@@ -61,6 +61,7 @@ let PaginationService = PaginationService_1 = class PaginationService {
|
|
|
61
61
|
const query = {
|
|
62
62
|
select: selectCondition,
|
|
63
63
|
where: (customQuery === null || customQuery === void 0 ? void 0 : customQuery.where) || {},
|
|
64
|
+
include: (customQuery === null || customQuery === void 0 ? void 0 : customQuery.include) || {},
|
|
64
65
|
orderBy: sortOrderCondition,
|
|
65
66
|
take: pageSize,
|
|
66
67
|
skip,
|
|
@@ -92,17 +93,17 @@ let PaginationService = PaginationService_1 = class PaginationService {
|
|
|
92
93
|
const fieldNames = [];
|
|
93
94
|
const fields = model.fields;
|
|
94
95
|
for (const key in fields) {
|
|
95
|
-
if (fields.hasOwnProperty(key)) {
|
|
96
|
+
if (fields && fields.hasOwnProperty(key)) {
|
|
96
97
|
fieldNames.push(key);
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
return fieldNames;
|
|
100
101
|
}
|
|
101
102
|
isInvalidField(sortField, model) {
|
|
102
|
-
return !model.fields[sortField];
|
|
103
|
+
return model && model.fields ? !model.fields[sortField] : true;
|
|
103
104
|
}
|
|
104
105
|
isInvalidFields(fields, model) {
|
|
105
|
-
return !fields.every((field) => model.fields[field]);
|
|
106
|
+
return !fields.every((field) => model.fields ? model && model.fields[field] : false);
|
|
106
107
|
}
|
|
107
108
|
};
|
|
108
109
|
exports.PaginationService = PaginationService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.service.js","sourceRoot":"","sources":["../src/pagination.service.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAyE;AACzE,2EAG0C;AAC1C,+DAA8D;AASvD,IAAM,iBAAiB,yBAAvB,MAAM,iBAAiB;IAAvB;QACY,WAAM,GAAG,IAAI,eAAM,CAAC,mBAAiB,C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AAC;gBACzC,MAAM,KAAK,CAAC;YACd,CAAC;YAED,MAAM,IAAI,4BAAmB,CAAC,uBAAuB,KAAK,EAAE,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAED,iBAAiB,CAAC,KAA0B;QAC1C,MAAM,UAAU,GAAa,EAAE,CAAC;QAChC,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;QAE5B,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;YACzB,IAAI,MAAM,IAAI,MAAM,CAAC,cAAc,CAAC,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;AArIY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;GACA,iBAAiB,CAqI7B"}
|
package/package.json
CHANGED
|
@@ -90,6 +90,7 @@ export class PaginationService {
|
|
|
90
90
|
const query = {
|
|
91
91
|
select: selectCondition,
|
|
92
92
|
where: customQuery?.where || {},
|
|
93
|
+
include: customQuery?.include || {},
|
|
93
94
|
orderBy: sortOrderCondition,
|
|
94
95
|
take: pageSize,
|
|
95
96
|
skip,
|
|
@@ -124,11 +125,10 @@ export class PaginationService {
|
|
|
124
125
|
|
|
125
126
|
extractFieldNames(model: Record<string, any>): string[] {
|
|
126
127
|
const fieldNames: string[] = [];
|
|
127
|
-
|
|
128
128
|
const fields = model.fields;
|
|
129
129
|
|
|
130
130
|
for (const key in fields) {
|
|
131
|
-
if (fields.hasOwnProperty(key)) {
|
|
131
|
+
if (fields && fields.hasOwnProperty(key)) {
|
|
132
132
|
fieldNames.push(key);
|
|
133
133
|
}
|
|
134
134
|
}
|
|
@@ -137,10 +137,12 @@ export class PaginationService {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
isInvalidField(sortField: string, model: BaseModel): boolean {
|
|
140
|
-
return !model.fields[sortField];
|
|
140
|
+
return model && model.fields ? !model.fields[sortField] : true;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
isInvalidFields(fields: string[], model: BaseModel): boolean {
|
|
144
|
-
return !fields.every((field) =>
|
|
144
|
+
return !fields.every((field) =>
|
|
145
|
+
model.fields ? model && model.fields[field] : false,
|
|
146
|
+
);
|
|
145
147
|
}
|
|
146
148
|
}
|