nest-nepa
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Description

If you using NestJS with: Pagination, GraphQL code-first, Mongoose. This package is fit for you.

Install

with npm

npm i nest-nepa

with yarn

yarn add nest-nepa

Using

import { Paging, PagingInputInterface, PagingWithPage } from "nest-nepa";

/**
 * filter is a MongoDB Filter<T>
 */
interface FindManyProps {
  filter: any;
  paging: PagingInputInterface;
}

/**
 * In case I choose updatedAt_utc as a Cursor
 */
interface TodoDocument {
  updatedAt_utc: Date;
}

class TodoCRUD {
  private model: Model<TodoDocument>;
  async findMany(props: FindManyProps) {
    const { filter, sort, build } = new Paging<TodoDocument>({
      cursors: props?.paging?.cursors,
      filter: props.filter,
      order: Paging.DESC,
      key: "updatedAt_utc",
      KeyType: Date,
    });
    const limit = Number(props?.paging?.limit || 10);
    if (limit > 20) throw new Error("rate limit");

    const skip = Number(props?.paging?.offset);
    const many = await this.model
      .find(filter)
      .sort(sort)
      .limit(limit)
      .skip(skip);
    return build(many, this.model);
  }
}

Package Sidebar

Install

npm i nest-nepa

Weekly Downloads

26

Version

1.0.6

License

ISC

Unpacked Size

35.8 kB

Total Files

10

Last publish

Collaborators

  • truongduchuy910