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

@senaiplay/nestjs-sequelize-paginate

v1.0.9

Published

πŸ³β€πŸŒˆ Pagination helper method for Sequelize models.

Downloads

202

Readme

NestJs Sequelize Paginate

πŸ³β€πŸŒˆ Pagination helper method for Sequelize models.

🌐 Description

Under the hood, nestjs-sequelize-paginate makes use of the nest framework, and you also need to install nestjs, and sequelize !

πŸ“¦ Integration

To start using it, we first install the required dependencies. In this chapter we will demonstrate the use of the paginate for nestjs.

You simply need to install the package !

// We install with npm, but you could use the package manager you prefer !
npm install -save @senaiplay/nestjs-sequelize-paginate

▢️ Getting started

Once the installation process is complete, we can import the PaginateModule into the root AppModule

import { Module } from '@nestjs/common'
import { PaginateModule } from 'nestjs-sequelize-paginate'

@Module({
  imports: [
    PaginateModule.forRoot({
      url: 'http://localhost:3000',
    }),
  ],
})
export class AppModule {}

The forRoot() method supports all the configuration properties exposed by the paginate 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 | true | | showUrl | If you want the url to be shown in the results | boolean | false | | structure | Una forma de estructura de respuesta | 'simple' | 'segmented' | simple | | details | Una forma de respuesta | 'necessary' | 'complete' | complete | | defaultPage | Numeros de pagina por defecto globalmente | number | 1 | | defaultOffset | Numeros de cantidad por pagina globalmente | number | 5 | | showOffset | Si quere offset se muestre en las url globalmente | boolean | false |

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 User Model.

import { Injectable } from '@nestjs/common'
import { PaginateService, PaginateOptions } from 'nestjs-sequelize-paginate'
import { ModelUser } from 'src/models/user.model'

@Injectable()
export class UserService {
  constructor(private paginateService: PaginateService) {}
  async findAll(options: PaginateOptions): Promise<any> {
    const paginate = this.paginateService.findAllPaginate({
      ...options,
      model: ModelUser,
      path: '/user',
    })
    return paginate
  }
}

Next, let's look at the UserModule:

import { Controller, Get, Res, HttpStatus } from '@nestjs/common'
import { UserService } from './user.service'
import { Response } from 'express'
import { PaginateQueryInterface, PaginateQuery } from 'nestjs-sequelize-paginate'

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get()
  async getUsers(@Res() res: Response, @PaginateQuery('all') paginateQuery: PaginateQueryInterface): Promise<any> {
    const data = await this.userService.findAll(paginateQuery)
    res.status(HttpStatus.OK).send(data)
  }
}

Decorator

As you saw, we're using a decorator, '@PaginateQuery'. The decorator receives only one option as a parameter, which is all, this allows to add the offset through the url !

This decorator returns the following to you !

{
   path: '/user',
   page: 2, // http://localhost:3000/user?page=2
   offset: 10, // http://localhost:3000/user?page=2&offset=10
   showOffset: true // if you add 'all' as a parameter
}

⭐ Support for

Sass-colors is an open source project licensed by MIT. You can grow thanks to the sponsors and the support of the amazing sponsors. If you want to join them, contact me here.

🎩 Stay in touch

πŸ“œ License

Sass-colors is MIT licensed.