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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-utils-kit

v1.0.1

Published

Reusable Mongoose plugins like toJSON, paginate, and aggregation.

Readme

Here is a clean, professional, well-structured README.md generated from the content you shared — formatted exactly like top-tier open-source packages on npm/GitHub:


mongoose-lite-plugins

Reusable, Type-Safe Mongoose Plugins for Modern Node.js APIs


Key Features

  • 🚀 Type-Safe Pagination — Fully typed .paginate() with rich filtering, sorting, population & aggregation support.
  • 🔒 Zero-Config Security — Automatically strips private fields (even nested ones) when converting Mongoose documents to JSON.
  • 🧼 Clean Output — Auto-maps _idid, removes __v, timestamps (optional), and supports aliasing.
  • Modern TypeScript First — Designed for Node.js + TS backends.

📦 Installation

npm install mongoose-lite-plugins
# or
yarn add mongoose-lite-plugins

🌟 Overview

mongoose-lite-plugins is a curated collection of essential Mongoose plugins that solve two of the most common needs in real-world API development:

✔ Data Transformation (toJSON)

Automatically remove private fields, handle aliases, and output clean JSON structures.

✔ Rich Pagination

Add a powerful, type-safe static paginate() method to any Mongoose model.

This package focuses on simplicity, security, and usability — perfect for production-grade Node.js APIs.


💡 Quick Start Usage

1. Define your schema & mark private fields

import { Schema, model } from 'mongoose';
import { paginate, toJSONPlugin } from 'mongoose-lite-plugins';

const userSchema = new Schema(
  {
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true, private: true }, // private
    profile: {
      age: { type: Number, private: true }, // nested private
      city: String,
    },
    tags: [String],
  },
  { timestamps: true }
);

2. Apply the plugins

userSchema.plugin(paginate);
userSchema.plugin(toJSONPlugin);

3. Export model

export const UserModel = model('User', userSchema);

🔍 Example Usage

Pagination

const results = await UserModel.paginate({}, { 
  page: 2,
  limit: 10,
  sortBy: 'name:asc' 
});

toJSON Transformation

const doc = await UserModel.findOne();
const json = doc.toJSON();

// Output:
// {
//   id: "...",
//   name: "...",
//   email: "...",
//   profile: { city: "..." },
//   tags: ["..."]
// }
// private fields like `password` and `profile.age` are removed

📘 1. toJSONPlugin

This plugin enhances the default Mongoose .toJSON() behavior for clean and secure API responses.

✨ Features

| Feature | Description | | -------------- | ------------------------------------------------------------------------------------ | | Security | Automatically strips all fields marked with private: true (supports deep nesting). | | ID Mapping | Converts _id → id and removes _id and __v. | | Timestamps | Removes createdAt and updatedAt by default (optional). | | Aliasing | Rename fields using string or object aliasing. |


ToJSONOptions Interface

import { ToJSONOptions } from 'mongoose-lite-plugins';

const options: ToJSONOptions = {
  includeTimeStamps: true,
  alias: {
    'profile.city': 'location',
    'tags': 'keywords'
  }
};

// alias (string format):
// "profile.city:location;tags:keywords"

📗 2. paginate Plugin

Adds a static Model.paginate(filter, options) method with rich query capabilities.

✨ Features

| Feature | Description | | ----------------------- | -------------------------------------------------------------------- | | Filtering & Sorting | Supports objects and sortBy string syntax (name:asc,score:desc). | | Deep Population | Handles nested population with field selection. | | Aggregation Support | Paginate aggregation pipeline results. | | Get All Mode | page: -1 → return all items without pagination. | | Shuffle Mode | Randomizes result order when isShuffleRecord=true. |


PaginateOptions

| Property | Type | Description | | --------------- | --------------- | ---------------------------------------------- | | page | number | Page number (default: 1); -1 for all records | | limit | number | Items per page (default: 10) | | sortBy | string | e.g., "score:desc,name:asc" | | fields | string | e.g., "name,email" | | populate | string | "path:field1,field2;otherPath" | | aggregation | PipelineStage[] | MongoDB pipeline array | | isShuffleRecord | boolean | Randomize final results |


QueryResult Output

export interface QueryResult<T> {
  results: T[];
  page: number;
  limit: number;
  totalPages: number;
  totalResults: number;
}

🤝 Contributing

Contributions are welcome! If you encounter a bug, have a feature request, or want to submit a PR:

  • Open an Issue
  • Submit a Pull Request

Your contributions help shape this project for the community.


📜 License

This project is licensed under the MIT License.