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

@nodeteam/nestjs-prisma-pagination

v1.0.6

Published

Paginators for Nest.JS app

Downloads

4,802

Readme

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