npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-sequelize-pagination

v2.0.1

Published

Pagination helper method for Sequelize models.

Downloads

9

Readme

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,
      limit: 50,
      page: 1,
      withDetails: 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 | | limit | It is used by default when limit information is not sent. | number | 50 | | withDetails | Used to detail meta information | 'necessary' | 'complete' | necessary | | cls | Paginated Data class to be used in function output. The class must be extended to PaginatedDataAbstract. | PaginatedDataAbstract | PaginatedData |

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 limit 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
  limit: 10, // http://localhost:3000/item?page=2&limit=10
}

License

nestjs-sequelize-pagination is MIT licensed.