@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
| Export | Type | Purpose |
|---|---|---|
NestService<M, D> | class | CRUD service — _find, _get, _create, _patch, _remove |
nestify() | function | Apply $select / $populate / $sort / $limit / $skip to a query |
rawQuery() | function | Convert query params to a MongoDB filter (ObjectId + $regex aware) |
EnsureObjectId() | function | Validate / convert a string to an ObjectId |
GlobalExceptionFilter | filter | Handles HttpException, Mongoose, Zod and MongoServerError |
Usage
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);
}
}await catsService._find({
name: { $regex: 'kitty', $options: 'i' },
age: { $gt: 5 },
$populate: 'owner',
$sort: { createdAt: -1 },
$limit: 10,
});