Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nodeteam/nestjs-prisma-pagination

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nodeteam/nestjs-prisma-pagination

Paginators for Nest.JS app

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Installation

npm install --save @nodeteam/nestjs-prisma-pagination

Usage

// import paginators
import { paginator, searchPaginator } from '@nodeteam/nestjs-prisma-pagination';
// import types
import { PaginatorTypes } from '@nodeteam/nestjs-prisma-pagination';

#Paginator

Paginator options:

page - number of page

perPage - number of records per page

Options can be redefined

Return type:

{
    data: T[],
    meta: {
        total: number,
        lastPage: number,
        currentPage: number,
        perPage: number,
        prev: number | null,
        next: number | null,
    },
}

Example:

Create new paginator function with default options

const paginate: PaginatorTypes.PaginateFunction = paginator({
    page: 1,
    perPage: 10,
});

full example:

import PrismaService from '@providers/prisma/prisma.service';
import { Injectable } from '@nestjs/common';
import { PaginatorTypes, paginator } from '@nodeteam/nestjs-prisma-pagination';

const paginate: PaginatorTypes.PaginateFunction = paginator({ perPage: 10 });

@Injectable()
export default class UserService {
    constructor(private prisma: PrismaService) {}

    async findMany({ where, orderBy, pagination }: {
        where?: Prisma.UserWhereInput,
        orderBy?: Prisma.UserOrderByWithRelationInput
        page?: number,
        perPage?: number,
    }): Promise<PaginatorTypes.PaginatedResult<User>> {
        return paginate(
            this.prisma.user,
            {
                where,
                orderBy,
            },
            {
                page,
                perPage,
            },
        );
    }
}
Redefine options:
        paginate(
            this.prisma.user,
            {
                where,
                orderBy,
            },
            {
                page: 2,  // Rendefine page
                perPage: 5, // Rendefine perPage
            },
        );

Examples:

  • Paginate records page: 1 and where: Jake
https://exmaple.com/api/v1/user?page=1&where=Jake

#Full-text Search Paginator

Search Paginator options:

page - number of page

perPage - number of records per page

skip - number of records to skip

searchColumns - array of columns in db

searchValue - string to search

Options can be redefined

Search Paginator arguments:

model - PrismaClient['modelName']

modelName - Name of model

create new search paginator function with default options

const searchPaginate: PaginatorTypes.SearchPaginateFunction = searchPaginator({
  page: 1,
  perPage: 10,
});

full example:

import PrismaService from '@providers/prisma/prisma.service';
import { Injectable } from '@nestjs/common';
import { PaginatorTypes, searchPaginator } from '@nodeteam/nestjs-prisma-pagination';

const searchPaginate: PaginatorTypes.SearchPaginateFunction = searchPaginator({ perPage: 10 });

@Injectable()
export default class UserService {
    constructor(private prisma: PrismaService) {}

    async searchUser({
        page,
        perPage,
        skip,
        searchValue
    }: {
        page: number,
        perPage: number,
        skip: number,
        searchValue?: string,
    }): Promise<PaginatorTypes.PaginatedResult<User>> {
        const searchColumns = ['firstName', 'lastName', 'description'];

        return searchPaginate(
            this.prisma,
            'User',
            {
                page,
                perPage,
                skip,
                searchValue,
                searchColumns,
            },
        );
    }
}
Redefine options:
        searchPaginate(
            this.prisma, 
            'User',
            {
                page: 2,  // Rendefine page
                perPage: 5, // Rendefine perPage
                skip,
                searchValue,
                searchColumns,
            },
        );

Examples:

  • Paginate search records page: 1 and searchValue: Lions
https://example.com/api/v1/users/full-text/search?search=Lions&page=1

#Pagination Helpers

getPagination options:

rawPage - number of page (optional)

rawPerPage - number of records per page (optional)

Examples:

const result = getPagination(1, 10);

result => {
    perPage: 10,
    page: 1,
    skip: 0,
};

getPaginatedResult options:

data - array of entities

pagination - page, perPage, skip

count - total count

Examples:

const result = getPaginatedResult({
    data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    pagination: {
        page: 1,
        perPage: 10,
        skip: 0,
      },
    count: 100,
});

result => {
    data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    meta: {
      total: 100,
      lastPage: 10,
      currentPage: 1,
      perPage: 10,
      prev: 0,
      next: 1,
    },
  };

Check useful npm packages from NodeTeam: https://www.npmjs.com/org/nodeteam

Keywords

FAQs

Package last updated on 15 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc