1.4.1 • Published 5 months ago

nestjs-sequelize-pagination v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

NestJs Sequelize Pagination

Sequelize pagination module for NestJS.

Description

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.

Integration

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

Getting Started

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.

NameDescriptionTypeDefault
urlIf you want a global urlstringnull
isGlobalIf you want the module globallybooleanfalse
pageIt is used by default when page information is not sent.number1
offsetIt is used by default when offset information is not sent.number50
detailsUsed to detail meta information'necessary' | 'complete'necessary

Service

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 {
}

Decorator

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
}

License

nestjs-sequelize-pagination is MIT licensed.

1.4.1

5 months ago

1.4.0

11 months ago

1.2.11

11 months ago

1.1.11

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago