nestjs-paginate/README.md

179 lines
4.3 KiB
Markdown
Raw Normal View History

2020-06-26 21:25:03 +00:00
# Nest.js Paginate
2020-06-26 22:24:30 +00:00
[![npm](https://img.shields.io/npm/v/nestjs-paginate.svg)](https://www.npmjs.com/package/nestjs-paginate)
2020-06-26 21:25:03 +00:00
![Main CI](https://github.com/ppetzold/nestjs-paginate/workflows/Main%20CI/badge.svg)
2020-06-26 21:27:43 +00:00
[![codecov](https://codecov.io/gh/ppetzold/nestjs-paginate/branch/master/graph/badge.svg)](https://codecov.io/gh/ppetzold/nestjs-paginate)
2020-06-26 21:25:03 +00:00
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
2020-06-26 21:36:41 +00:00
![GitHub](https://img.shields.io/github/license/ppetzold/nestjs-paginate)
2020-06-26 21:25:03 +00:00
Pagination and filtering helper method for TypeORM repositories or query builders using [Nest.js](https://nestjs.com/) framework.
- Pagination conforms to [JSON:API](https://jsonapi.org/)
- Sort by multiple columns
2020-06-28 17:34:00 +00:00
- Filter using operators
- Search across columns
2020-06-26 21:25:03 +00:00
## Installation
```
npm install nestjs-paginate
```
## Usage
2020-06-26 21:39:18 +00:00
### Example
2020-06-26 21:25:03 +00:00
The following code exposes a route that can be utilized like so:
#### Endpoint
```url
2020-06-28 17:34:00 +00:00
http://localhost:3000/cats?limit=5&page=2&sortBy=color:DESC&search=i
2020-06-26 21:25:03 +00:00
```
#### Result
```json
{
"data": [
{
"id": 4,
"name": "George",
"color": "white"
},
{
"id": 5,
"name": "Leche",
"color": "white"
},
{
"id": 2,
"name": "Garfield",
"color": "ginger"
},
{
"id": 1,
"name": "Milo",
"color": "brown"
},
{
"id": 3,
2020-06-28 17:34:00 +00:00
"name": "Kitty",
2020-06-26 21:25:03 +00:00
"color": "black"
}
],
"meta": {
2020-06-27 07:44:48 +00:00
"itemsPerPage": 5,
"totalItems": 12,
2020-06-26 21:25:03 +00:00
"currentPage": 2,
"totalPages": 3,
"sortBy": [["color", "DESC"]]
},
"links": {
2020-06-28 17:34:00 +00:00
"first": "http://localhost:3000/cats?limit=5&page=1&sortBy=color:DESC&search=i",
"previous": "http://localhost:3000/cats?limit=5&page=1&sortBy=color:DESC&search=i",
"current": "http://localhost:3000/cats?limit=5&page=2&sortBy=color:DESC&search=i",
"next": "http://localhost:3000/cats?limit=5&page=3&sortBy=color:DESC&search=i",
"last": "http://localhost:3000/cats?limit=5&page=3&sortBy=color:DESC&search=i"
2020-06-26 21:25:03 +00:00
}
}
```
#### Code
```ts
import { Controller, Injectable, Get } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
import { Paginate, PaginateQuery, paginate, Paginated } from 'nestjs-paginate'
@Entity()
export class CatEntity {
@PrimaryGeneratedColumn()
id: number
@Column('text')
name: string
@Column('text')
color: string
}
@Injectable()
export class CatsService {
constructor(
@InjectRepository(CatEntity)
private readonly catsRepository: Repository<CatEntity>
) {}
public findAll(query: PaginateQuery): Promise<Paginated<CatEntity>> {
return paginate(query, this.catsRepository, {
sortableColumns: ['name', 'color'],
defaultOrderby: [['color', 'DESC']],
})
}
}
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get()
public findAll(@Paginate() query: PaginateQuery): Promise<Paginated<CatEntity>> {
return this.catsService.findAll(query)
}
}
```
### Config
```ts
const paginateConfig: PaginateConfig<CatEntity> {
/**
* Required: true (must have a minimum of one column)
2020-06-27 07:33:54 +00:00
* Type: (keyof CatEntity)[]
2020-06-26 21:25:03 +00:00
* Description: These are the columns that are valid to be sorted by.
*/
sortableColumns: ['id', 'name', 'color'],
2020-06-28 17:34:00 +00:00
/**
* Required: false
* Type: (keyof CatEntity)[]
* Description: These columns will be searched through when using the search query param.
*/
sortableColumns: ['name', 'color'],
2020-06-26 21:25:03 +00:00
/**
* Required: false
* Type: number
* Default: 100
* Description: The maximum amount of entities to return per page.
*/
maxLimit: 20,
/**
* Required: false
2020-06-27 07:33:54 +00:00
* Type: [keyof CatEntity, 'ASC' | 'DESC'][]
* Default: [[sortableColumns[0], 'ASC]]
2020-06-26 21:25:03 +00:00
* Description: The order to display the sorted entities.
*/
defaultSortBy: [['name', 'DESC']],
/**
* Required: false
* Type: number
* Default: 20
*/
defaultLimit: 50,
/**
* Required: false
* Type: TypeORM find options
* Default: None
* https://typeorm.io/#/find-optionsfind-options.md
*/
where: { color: 'ginger' }
}
```