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

@cgvweb/mongoose-cursor-pagination

v1.2.1

Published

A simple Mongoose plugin that allows defining cursor pagination for your Schemas.

Downloads

6

Readme

Mongoose Cursor Pagination

A simple Mongoose plugin that allows defining cursor pagination for your Schemas.

This plugin is developed by Christian Gil mainly to be used on his own projects, since it provides common utilities used in many back-end projects.

Installation

pnpm install @cgvweb/mongoose-cursor-pagination

[!IMPORTANT] This package requires mongoose ^7 || ^8 and zod ^3 to be installed as peer dependencies.

Usage

To use the cursor pagination functionality, you must install the plugin on your Schema and optionally add the type to the model:

// user.schema.ts
import { paginatePlugin, type PaginateFn } from '@cgvweb/mongoose-cursor-pagination';
import { Schema, model, type Model } from 'mongoose';

export interface UserFields {
  id: string;
  email: string;
  name: string;
}

/** Adds the `paginate` method type to the User model */
export interface UserModel extends Model<UserFields> {
  paginate: PaginateFn<UserFields>;
}

const userSchema = new Schema<UserFields>({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
});

// Add the cursor pagination plugin
userSchema.plugin(paginatePlugin);

export const User = model<UserFields, UserModel>('user', userSchema);

Now you can use the paginate method on your schema to query the data using cursor pagination:

import { User } from './user.schema.ts';

async function searchUsers() {
  const result = await User.paginate({
    pagination: { limit: 10, order: 'asc' },
  });
  return result;
}

Pagination Options

| Option | Type | Required | Description | | ----------------------- | ------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- | | pagination | object | Yes | The options for the paginated query. | | pagination.limit | number | Yes | The maximum number of docs to fetch per page. | | pagination.order | "asc" | "desc" | Yes | The sorting order of the cursor. | | pagination.prevCursor | string | No | The cursor to fetch the previous page from. | | pagination.nextCursor | string | No | The cursor to fetch the next page from. If used with prevCursor it will be ignored. | | filters | FilterQuery<T> | No | Same filters you would pass to a regular Mongoose query.For example: { filters: { status: "active" } } | | queryOpts | QueryOptions<T> | No | The same query options you would pass to a regular Mongoose query.For example: { queryOpts: { populate: "author" } } | | projection | ProjectionType<T> | No | The same projection options you would pass to a regular Mongoose query.For example: { projection: { email: false } } |

Pagination Response

| Field | Type | Description | | ------------ | ------------------ | ------------------------------------------------------------------------------------------------------------- | | data | T[] | The subset of documents on the current page based on the limit, order and cursors provided. | | totalCount | number | The total number of documents that match the query provided, including the ones on the current page. | | nextCursor | string | null | The cursor you can use to fetch the next page of documents. If null, the current page is the last one. | | prevCursor | string | null | The cursor you can use to fetch the previous page of documents. If null, the current page is the first one. |

Additional Exports

To make sure you can use the pagination functions with data validation, this plugin also exports a few Zod schemas and types:

| Resource | Type | Description | | ------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | | Paginated<T> | type | The type of the response from the paginate method. The T generic is used to type the data array. | | PaginateFn<T> | type | The type used to add the paginate method to the Mongoose Schema. The T generic is used to add the document fields to the paginate options. | | PaginationSchema | ZodObject | A Zod schema you can use to validate the options passed to pagination. | | PaginationFields | type | The inferred type of PaginationSchema. | | SortOrderType | type | asc | desc |

License

MIT License © 2018-PRESENT CGV WEB