1.0.24 • Published 4 months ago

nestjs-prisma-pagination v1.0.24

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

nestjs-prisma-pagination

Description

A tool for easily generate pagination query for prisma

Installation

npm i nestjs-prisma-pagination

or

yarn add nestjs-prisma-pagination

Usage

To generate a query object for any prisma findMany clause, you can just do this:

import { paginate } from 'nestjs-prisma-pagination';

// Generate the query with `paginate` function
const query = paginate(
  {
    page: 1,
    limit: 10,
    from: '2023-01-01T00:00:00.000Z',
    to: '2023-12-31T22:59:59.000Z',
    search: 'foo',
  },
  {
    dateAttr: 'at',
    enabled: false,
    includes: ['post', 'user.agent.auth'],
    search: ['fullname', 'reference'],
    orderBy: { fullname: 'asc' },
  },
);

// Use your query in any findMany clause
const result = await prisma.user.findMany(query);

In this example, the value return by query is

{
  "where": {
    "OR": [
      {
        "fullname": {
          "contains": "foo",
          "mode": "insensitive"
        }
      },
      {
        "reference": {
          "contains": "foo",
          "mode": "insensitive"
        }
      }
    ],
    "at": {
      "gte": "2022-12-31T23:00:00.000Z",
      "lte": "2023-12-31T22:59:59.000Z"
    }
  },
  "take": 10,
  "skip": 0,
  "orderBy": {
    "fullname": "asc"
  },
  "include": {
    "post": true,
    "user": {
      "include": {
        "agent": {
          "include": {
            "auth": true
          }
        }
      }
    }
  }
}

paginate(args, options)

Parameters

  • args: <PaginationArgs>
  • options: <PaginationOptions>

PaginationArgs

interface PaginationArgs {
  page?: number; // The number of the page you want to retrieve (start counting with 1)
  limit?: number; // The maximum number of rows you want to take
  search?: string; // The value you want to search in your table (string | number | enum)
  from?: string; // ISOString value that indicates the starting date for the query
  to?: string; // ISOString value that indicates the end date for the query
  take?: number; // The number of items you want to retrieve
  skip?: number; // The number of items to skip
}

PaginationOptions

interface PaginationOptions {
  includes?: string[];
  search?: string[];
  equals?: string[];
  enums?: string[];
  orderBy?: Record<string, 'asc' | 'desc'>;
  enabled?: boolean;
  dateAttr?: string;
  disableInsensitiveMode?: boolean; // When set to true, search queries are in sensitive mode
}
1.0.24

4 months ago

1.0.22

11 months ago

1.0.21

11 months ago

1.0.20

11 months ago

1.0.23

11 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.2

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.1

2 years ago

1.0.0

2 years ago