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

@nest-extended/mongoose

v1.0.0

Published

This package provides powerful Mongoose integrations for the **NestExtended** ecosystem, offering a robust service layer with built-in pagination, filtering, soft delete capabilities, exception filters, and query utilities.

Readme

@nest-extended/mongoose

This package provides powerful Mongoose integrations for the NestExtended ecosystem, offering a robust service layer with built-in pagination, filtering, soft delete capabilities, exception filters, and query utilities.

Key Features

NestService

A generic service class (NestService<M, D>) that provides:

  • CRUD Operations: _find, _get, _create, _patch, _remove
  • Advanced Querying: Support for $regex, $or, $in, $nin, $lt, $lte, $gt, $gte, $ne and standard MongoDB operators
  • Pagination: Built-in pagination logic using $skip and $limit with configurable defaults (limit: 20, skip: 0)
  • Soft Delete: Configurable soft delete support — marks documents as deleted instead of removing, with user tracking via CLS context
  • Bulk Operations: Optional multi-document create via insertMany (enable with multi: true)
  • Count: getCount(filter) for counting documents matching a filter
  • Conditional Pagination: _find accepts { pagination: false } to return raw arrays instead of paginated responses

Constructor Options (NestServiceOptions):

  • multi (default: false) — allow bulk create with arrays
  • softDelete (default: true) — enable soft delete behavior
  • pagination (default: true) — enable paginated responses

Query Utilities

  • nestify(query, filters, options): Applies $select, $populate, $sort, $limit, $skip to a Mongoose query
  • rawQuery(query): Converts query params to MongoDB query with auto-ObjectId conversion, $regex support, and recursive $or handling
  • assignFilters: Extracts known filter keys ($sort, $limit, $skip, $select, $populate) from query params
  • filterQuery: Full query parsing — separates filters from query and validates operators
  • cleanQuery: Validates query operators and throws BadRequestException for invalid $ params

Helper Utilities

  • EnsureObjectId(id): Validates and converts string to Types.ObjectId, throws Error on invalid ID
  • ensureObjectId: Alias export for the same utility

Exception Filters

  • GlobalExceptionFilter: Catch-all exception filter that handles:

    • HttpException — returns standard NestJS error response
    • MongooseError — wraps as BadRequestException
    • ZodError — wraps as BadRequestException
    • MongoServerError — parses specific error codes with human-readable messages
    • Unhandled errors — returns 500 with stack trace (stack hidden in production)
  • handleMongoError(exception): Translates MongoDB error codes to user-friendly messages:

    • 11000 — Duplicate key violation
    • 121 — Document validation failure
    • 66 — Immutable field modification
    • 50 — Operation timeout
    • 16755 — Invalid aggregation pipeline
    • 40324 — Invalid index options
    • 8000 — Transaction error
    • 31 — Memory limit exceeded

Types

  • NestifyFilters: $select, $populate, $sort, $limit, $skip
  • NestifyOptions: defaultLimit, defaultSkip, defaultPagination

Usage

NestService

Extend NestService to create a service with full CRUD capabilities.

import { NestService } from '@nest-extended/mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Cat, CatDocument } from './schemas/cat.schema';

@Injectable()
export class CatsService extends NestService<Cat, CatDocument> {
  constructor(@InjectModel(Cat.name) catModel: Model<CatDocument>) {
    super(catModel);
  }
}

With custom options:

super(catModel, { multi: true, softDelete: false, pagination: false });

Querying

You can use the _find method with query objects:

const results = await this.catsService._find({
  name: { $regex: 'kitty', $options: 'i' },
  age: { $gt: 5 },
  $sort: { createdAt: -1 },
  $limit: 10
});

Disable pagination for a single query:

const allCats = await this.catsService._find({}, { pagination: false });

GlobalExceptionFilter

Register globally in app.module.ts:

import { GlobalExceptionFilter } from '@nest-extended/mongoose';
import { APP_FILTER } from '@nestjs/core';

providers: [
  { provide: APP_FILTER, useClass: GlobalExceptionFilter },
]

EnsureObjectId

import { EnsureObjectId } from '@nest-extended/mongoose';

const objectId = EnsureObjectId('507f1f77bcf86cd799439011');

Exported API

| Export | Type | Description | |---|---|---| | NestService | Class | Generic CRUD service with pagination & soft delete | | nestify | Function | Apply filters/pagination to Mongoose query | | rawQuery | Function | Convert query params to MongoDB filter | | assignFilters | Function | Extract filter params from query | | filterQuery | Function | Full query parsing with operator validation | | cleanQuery | Function | Validate query operators | | EnsureObjectId | Function | Validate & convert to ObjectId | | GlobalExceptionFilter | Filter | Catch-all exception handler | | handleMongoError | Function | MongoDB error code translator | | NestifyFilters | Interface | Filter type definition | | NestifyOptions | Interface | Options type definition |