Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tfarras/nestjs-typeorm-pagination
https://github.com/tfarras/nestjs-typeorm-pagination
entity filter filter-data nestjs nestjs-backend nestjs-library pagination query typeorm
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tfarras/nestjs-typeorm-pagination
- Owner: tfarras
- License: mit
- Created: 2020-03-08T22:00:45.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-28T20:17:26.000Z (about 4 years ago)
- Last Synced: 2024-10-20T07:45:21.394Z (2 months ago)
- Topics: entity, filter, filter-data, nestjs, nestjs-backend, nestjs-library, pagination, query, typeorm
- Language: TypeScript
- Size: 71.3 KB
- Stars: 8
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
NestJS TypeORM Pagination
### Installation
```bash
npm install @tfarras/nestjs-typeorm-pagination
```### Pagination Query params
* `_start` - from which row to start on fetch
* `_limit` - how many rows to take
* `_sortBy`? - column for sorting
* `_order`? - order for sorting. Accepted values: `1 | -1 | ASC | DESC`### Filtration
You can filter your data by passing columns and values as query params.
For the moment we support `Equal` and `In` operator of typeorm.#### Usage of `Equal`
To filter data with `Equal` operator, you can simply add a parameter like `column=value`Examples:
`id=1`
`[email protected]`
`country=MD`#### Usage of `In`
To filter data with `In` operator, you can add more than one time parameter like `column=value`Examples:
`id=1&&id=2&&id=3`
`country=MD&&country=SE&&country=US`
### Usage
Extend your entity from `PaginateableBaseEntity` :
```typescript
@Entity({ name: 'user' })
export class UserEntity extends PaginateableBaseEntity {
```Add parameter decorator to your controller method :
```typescript
@Get()
getMany(
@PgParams() pg: PaginationParams,
) {...}
```And now you're able to use pagination:
```typescript
...
import { PgParams, PaginationParams, Pagination } from '@tfarras/nestjs-typeorm-pagination';@Get()
getMany(
@PgParams() pg: PaginationParams,
): Promise> {
return UserEntity.findAndPaginate(pg);
}
```You still have access to [TypeORM Find Options](https://github.com/typeorm/typeorm/blob/master/docs/find-options.md). Just pass them as the second parameter to the `findAndPaginate`
Example:
```typescript
UserEntity.findAndPaginate(pg, {
where: {
firstname: IsNull(),
},
});
```Example request:
```
/user?_limit=11&_start=0&_sortBy=id&_order=DESC&id=1&id=2&[email protected]
````Pagination` response:
```json
{
"data": [
{
"id": 2,
"email": "[email protected]",
"firstname": "Taimoor",
"lastname": "Farras",
"country": "MD",
}
],
"total": 1,
}
```