GithubHelp home page GithubHelp logo

maximblack / nestjs-sequelize-paginate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yonycalsin/nestjs-sequelize-paginate

0.0 1.0 0.0 26 KB

๐ŸŽ‰ Pagination response object function + types for sequelize + nestjs

JavaScript 40.66% TypeScript 59.34%

nestjs-sequelize-paginate's Introduction

Nestjs Sequelize Paginate Logo

๐Ÿณโ€๐ŸŒˆ Pagination helper method for Sequelize models.

NPM Version Package License NPM Downloads Coverage

๐ŸŒ 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 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.

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.