Sequelize pagination module for NestJS.
NestJS database connection must be established before using nestjs-sequelize-pagination. You can use the database module for this. You can review the sequelize document.
To start using it, we first install the required dependencies. In this chapter we will demonstrate the use of the pagination for nestjs.
You simply need to install the package !
$ npm install --save nestjs-sequelize-pagination
Once the installation process is complete, we can import the PaginationModule into the root AppModule
import { Module } from '@nestjs/common';
import { SequelizeModule } from '@nestjs/sequelize';
import { ItemsModule } from './items/items.module';
import { PaginationModule } from 'nestjs-sequelize-pagination';
import * as SQLite from 'sqlite3';
@Module({
imports: [
SequelizeModule.forRoot({
logging: false,
synchronize: true,
autoLoadModels: true,
retryAttempts: 2,
retryDelay: 1000,
dialect: 'sqlite',
storage: 'tests/db.sqlite',
dialectOptions: {
mode:
SQLite.OPEN_READWRITE | SQLite.OPEN_CREATE | SQLite.OPEN_FULLMUTEX,
},
}),
ItemsModule,
PaginationModule.forRoot({
isGlobal: true,
offset: 50,
page: 1,
details: true,
url: 'http://localhost:3001/',
}),
],
})
export class AppModule {
}
The forRoot() method supports all the configuration properties exposed by the pagination constuctor . In addition, there are several extra configuration properties described below.
Name | Description | Type | Default |
---|---|---|---|
url | If you want a global url | string | null |
isGlobal | If you want the module globally | boolean | false |
page | It is used by default when page information is not sent. | number | 1 |
offset | It is used by default when offset information is not sent. | number | 50 |
details | Used to detail meta information | 'necessary' | 'complete' | necessary |
Sequelize implements the Active Record pattern. With this pattern, you use model classes directly to interact with the database. To continue the example, we need at least one model. Let's define the Item Model.
import { Injectable } from '@nestjs/common'
import { PaginationService, PaginationOptions } from 'nestjs-sequelize-pagination'
import { FindAndCountOptions } from 'sequelize';
import { ItemEntity } from './item.entity'
@Injectable()
export class ItemService {
constructor(
private readonly paginationService: PaginationService,
@InjectModel(ItemEntity)
private readonly itemRepository: typeof ItemEntity,
) {
}
findAll(
options: PaginationOptions,
optionsSequelize: FindAndCountOptions<ItemEntity> = {},
) {
return this.paginationService.findAllPaginate(
this.itemRepository,
options,
optionsSequelize,
);
}
}
Next, let's look at the ItemsController:
import { Controller, Get, Res, HttpStatus } from '@nestjs/common'
import { ItemService } from './item.service'
import { PaginationOptions, PaginationQuery } from 'nestjs-sequelize-pagination'
@Controller('items')
export class ItemsController {
constructor(private readonly itemService: ItemService) {
}
@Get('/')
findAll(@PaginationQuery() paginateQuery: PaginationOptions) {
return this.itemService.findAll(paginateQuery);
}
}
Next, let's look at the ItemsModule:
import { Module } from '@nestjs/common';
import { SequelizeModule } from '@nestjs/sequelize';
import { ItemEntity } from './item.entity';
import { ItemController } from './item.controller';
import { ItemService } from './item.service';
@Module({
imports: [SequelizeModule.forFeature([ItemEntity])],
controllers: [ItemController],
providers: [ItemService],
})
export class ItemsModule {
}
PaginationQuery decorator extracts the page and offset value from the querystring. This decorator can only be used in the http context.
import { PaginationOptions } from "nestjs-sequelize-pagination";
const paginateQuery: PaginationOptions = {
path: '/item',
page: 2, // http://localhost:3000/item?page=2
offset: 10, // http://localhost:3000/item?page=2&offset=10
}
nestjs-sequelize-pagination is MIT licensed.