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-wrapper-simple

v1.0.1

Published

Mongoose wrapper with default schema, auto timestamps, soft-delete pre-action hooks and easy pagination

Readme

Mongoose Wrapper Simple

Simple mongoose wrapper with default schema, soft-delete pre-action hooks and easy pagination. Sample provided in TypeScript.

Installation

npm i mongoose-wrapper-simple

mongoose is a peer dependency (supports Mongoose ^9.x). npm 7+ installs it automatically when not already present. mongoose-paginate-v2 is installed automatically as a runtime dependency. No other @types/* packages are required to use this library (the published declarations only reference mongoose types).

Local development (file: dependency)

Use this package from another project on the same machine without publishing to the registry:

// other-project/package.json
{
  "dependencies": {
    "mongoose-wrapper-simple": "file:../mongoose-wrapper-simple"
  }
}
cd other-project
npm install

The package ships only its compiled output (main / types / exports all point into dist/, and dist/ is git-ignored). Its prepare script builds dist/ automatically whenever the package is installed as a file: (or git) dependency, so npm install is all you need.

Troubleshooting — if you see Cannot find module 'mongoose-wrapper-simple' or its corresponding type declarations, the compiled dist/ output is missing (e.g. a stale checkout). Re-run npm install in the consuming project (this triggers prepare), or manually run npm run build inside mongoose-wrapper-simple.

Quick Start

import { Schema } from 'mongoose';
import {
  mongooseDefaultPlugins,
  mongooseReferencePlugins,
  mongooseDefaultSchemaOptions,
  ENUM_PAGING_MODE,
} from 'mongoose-wrapper-simple';

// apply ALL default behaviors: soft-delete pre-action + pagination + default schema
const userSchema = new Schema({ name: String }, mongooseDefaultSchemaOptions);
mongooseDefaultPlugins(userSchema);

// reference / sub-document schema: only add the default schema fields
const addressSchema = new Schema({ street: String }, mongooseDefaultSchemaOptions);
mongooseReferencePlugins(addressSchema);

Features

  • mongooseDefaultPlugins(schema) — apply all default behaviors in one call:
    • pre-action soft delete (mongoose-pre-action)
    • pagination (mongoose-pagination)
    • default schema (mongoose-default-schema)
  • mongooseReferencePlugins(schema) — add only the default schema fields (for reference / sub-document schemas)
  • mongooseDefaultSchemaOptions — SchemaOptions that map createdAt / updatedAt timestamps to createdUtcDate / updatedUtcDate
  • ENUM_PAGING_MODE — 'nopaging' | 'page' | 'next' | 'all'

Default schema fields

Added by both plugins (all fields are select: false):

| field | type | description | | ---------------- | ------- | ------------------------------------- | | createdUtcDate | Date | auto-set on create | | createdBy | String | | | updatedUtcDate | Date | auto-set on update (timestamps) | | updatedBy | String | | | deleted | Boolean | soft-delete flag | | deletedBy | String | | | deletedUtcDate | Date | stamped automatically on soft delete | | __v | Number | mongoose version key (hidden) |

Soft delete behavior (pre-action hooks)

  • count, countDocuments, estimatedDocumentCount, find, findOne, distinct → automatically filter deleted: { $ne: true }
  • findOneAndUpdate, updateOne, updateMany, findOneAndReplace, replaceOne → filter non-deleted records; when deleted: true is set, deletedUtcDate is stamped automatically
  • deleteOne, deleteMany, findOneAndDelete, findOneAndRemove → blocked — soft-delete the record instead of removing it
// soft delete (allowed)
await userModel.updateOne({ _id }, { deleted: true });

// hard delete (blocked - throws)
await userModel.deleteOne({ _id });

Pagination

Adds the mongoose-paginate-v2 plugin and a static findWithPaging(query, pagingOptions, queryOptions).

const result = await userModel.findWithPaging(
  { name: /^A/ },              // query
  {
    pagingMode: ENUM_PAGING_MODE.page,
    page: 1,
    limit: 10,
    sort: { createdUtcDate: -1 },
  },
  // queryOptions: e.g. { select: 'name email' }
);

// result = {
//   results: [...],
//   paging: {
//     pagingMode: 'page',
//     limit: 10,
//     currentCount: 10,
//     currentPage: 1,
//     totalPage: 3,
//     totalCount: 25,
//     hasPrevPage: false,
//     hasNextPage: true,
//     prevPage: null,
//     nextPage: 2,
//   }
// }

findWithPaging always filters out soft-deleted records (deleted: { $ne: true }).

Paging modes

| pagingMode | behavior | | ---------------- | ----------------------------------------------------------------- | | nopaging | first page only, no page/total metadata | | page | classic page/limit with total counts and prev/next navigation | | next | next-token (cursor) paging using a hex-encoded token | | all | all matching records (capped at 500) |

Every mode returns a { results, paging } shape, which pairs directly with express-wrapper-simple's res.sendSuccess (it detects the { results, paging } shape automatically).

References

  • ENUM_PAGING_MODE
    • nopaging — no paging, first page only
    • page — offset / page-based paging
    • next — next-token (cursor) paging
    • all — all matching records (max 500)

Development / Publishing

npm install        # install dev dependencies (runs prepare -> builds dist/)
npm run build      # clean, compile dist/, minify (gulp)
npm test           # type-check a consumer project against dist/ (no type errors)
npm pack           # create the publishable tarball (runs prepare first)
npm publish        # publish to npm registry (runs prepare first)
  • prepare runs on local npm install and on file: / git dependency installs, so dist/ is always built before the package is consumed or published.
  • Only the contents of dist/ (plus README.md, LICENSE, package.json) are shipped; the source, tests and build tooling are excluded via the files field.
  • mongoose is a peer dependency so consumers never end up with two Mongoose instances (which would otherwise cause schema / model identity mismatches).
  • The published .d.ts only references mongoose types, so consumers do not need @types/mongoose-paginate-v2, @types/node or any other @types/* package.