@nest-extended/mongoose

The Mongoose adapter: a generic CRUD service plus query helpers, ObjectId utilities and a MongoDB-aware exception filter.

$npm install @nest-extended/core @nest-extended/mongoose @nest-extended/decorators nestjs-cls

Key exports

ExportTypePurpose
NestService<M, D>classCRUD service — _find, _get, _create, _patch, _remove
nestify()functionApply $select / $populate / $sort / $limit / $skip to a query
rawQuery()functionConvert query params to a MongoDB filter (ObjectId + $regex aware)
EnsureObjectId()functionValidate / convert a string to an ObjectId
GlobalExceptionFilterfilterHandles HttpException, Mongoose, Zod and MongoServerError

Usage

cats.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { NestService } from '@nest-extended/mongoose';
import { Cat, CatDocument } from './schemas/cat.schema';
 
@Injectable()
export class CatsService extends NestService<Cat, CatDocument> {
  constructor(@InjectModel(Cat.name) model: Model<CatDocument>) {
    super(model);
  }
}
Querying
await catsService._find({
  name: { $regex: 'kitty', $options: 'i' },
  age: { $gt: 5 },
  $populate: 'owner',
  $sort: { createdAt: -1 },
  $limit: 10,
});

Next steps