1.1.1 • Published 3 months ago

@cgvweb/mongoose-cursor-pagination v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

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

OptionTypeRequiredDescription
paginationobjectYesThe options for the paginated query.
pagination.limitnumberYesThe maximum number of docs to fetch per page.
pagination.order"asc" | "desc"YesThe sorting order of the cursor.
pagination.prevCursorstringNoThe cursor to fetch the previous page from.
pagination.nextCursorstringNoThe cursor to fetch the next page from. If used with prevCursor it will be ignored.
filtersFilterQuery<T>NoSame filters you would pass to a regular Mongoose query.For example: { filters: { status: "active" } }
queryOptsQueryOptions<T>NoThe same query options you would pass to a regular Mongoose query.For example: { queryOpts: { populate: "author" } }
projectionProjectionType<T>NoThe same projection options you would pass to a regular Mongoose query.For example: { projection: { email: false } }

Pagination Response

FieldTypeDescription
dataT[]The subset of documents on the current page based on the limit, order and cursors provided.
totalCountnumberThe total number of documents that match the query provided, including the ones on the current page.
nextCursorstring | nullThe cursor you can use to fetch the next page of documents. If null, the current page is the last one.
prevCursorstring | nullThe 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:

ResourceTypeDescription
Paginated<T>typeThe type of the response from the paginate method. The T generic is used to type the data array.
PaginateFn<T>typeThe 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.
PaginationSchemaZodObjectA Zod schema you can use to validate the options passed to pagination.
PaginationFieldstypeThe inferred type of PaginationSchema.
SortOrderTypetypeasc | desc

License

MIT License © 2018-PRESENT CGV WEB

1.1.1

3 months ago

1.1.0

3 months ago

1.0.6

7 months ago