Code Monkey home page Code Monkey logo

nestjs-sequelize-pagination's Introduction

NestJs Sequelize Pagination

Sequelize pagination module for NestJS.

NPM Version Package License NPM Downloads Coverage Github Page Author

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.

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

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.

nestjs-sequelize-pagination's People

Contributors

mahsumurebe avatar

Stargazers

Mustafa Çor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.