Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ntheanh201/nestjs-sequelize-pagination
NestJS + Sequelize + Pagination
https://github.com/ntheanh201/nestjs-sequelize-pagination
Last synced: 24 days ago
JSON representation
NestJS + Sequelize + Pagination
- Host: GitHub
- URL: https://github.com/ntheanh201/nestjs-sequelize-pagination
- Owner: ntheanh201
- License: mit
- Created: 2022-09-28T02:58:27.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-03-22T17:12:05.000Z (over 1 year ago)
- Last Synced: 2024-10-06T10:34:10.593Z (about 1 month ago)
- Language: TypeScript
- Size: 252 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
NestJS Sequelize Pagination## @ntheanh201/nestjs-sequelize-pagination
A pagination module for NestJS and Sequelize, inspired by **[dw-nest-sequelize-pagination](https://www.npmjs.com/package/dw-nest-sequelize-pagination)** with deeper customization
## Installation
```bash
$ npm install @ntheanh201/nestjs-sequelize-pagination
``````bash
$ yarn add @ntheanh201/nestjs-sequelize-pagination
```## Getting Started
### Import
Import and add StripeModule to the imports section of the consuming module (most likely AppModule).
```ts
import { PaginationModule } from '@ntheanh201/nestjs-sequelize-pagination';@Module({
imports: [PaginationModule.forRoot({ isGlobal: true })],
})
export class AppModule {}
```### Configuration
This module support forRoot patterns for configuration, with values:
| Name | Description | Type | Default |
| -------------- | --------------------------------------------------- | --------- | ------- |
| isGlobal | Use module globally | _boolean_ | `true` |
| limit | The number of rows returned | _number_ | `10` |
| page | The page to start pagination, one-based indexing | _number_ | `1` |
| orderBy | The key sorting returned data | _string_ | `null` |
| orderDirection | The sorting direction (ASC, DESC, NULLS FIRST, ...) | _string_ | `null` |### Service
Sequelize service override findAll method from Sequelize and allow you to handle pagination automaticaly.```ts
import { Injectable } from '@nestjs/common';
import { Includeable, Op } from 'sequelize';
import {
PaginationQuery,
PaginationResponse,
PaginationService,
} from '@ntheanh201/nestjs-sequelize-pagination';@Injectable()
export class ProductService {
constructor(private paginationService: PaginationService) {}findAll(
paginationOptions: PaginationQuery,
include: Includeable | Includeable[] = [],
): Promise> {
let whereCondition;
const keySearch = paginationOptions?.searchKey;
if (keySearch) {
whereCondition = {
[Op.or]: [
{ sku: { [Op.like]: `%${keySearch}%` } },
{ barcode: { [Op.like]: `%${keySearch}%` } },
{ name: { [Op.like]: `%${keySearch}%` } },
],
};
}return this.paginationService.findAll(
{
...paginationOptions,
model: Product,
},
{
where: whereCondition,
include,
},
);
}
}
```### Controller
```ts
@Controller('/products')
export class ProductController {
constructor(private readonly productService: ProductService) {}@Get()
@ApiOperation({ summary: 'Get products' })
getProducts(
@Pagination({
limit: 10,
page: 0,
orderBy: 'createdAt',
orderDirection: 'DESC',
searchKey: 'pro',
})
pagination: PaginationQuery,
): Promise> {
return this.productService.findAll(pagination);
}
}
```## Contributing
Contributions welcome! See [Contributing](CONTRIBUTING.md).
## Stay in touch
**The Anh Nguyen ([Facebook](https://facebook.com/ntheanh201))**