@vetrivelan-cp/mongoose
v1.0.5
Published
CPBS abstraction layer over Mongoose and @nestjs/mongoose. Shields applications from breaking Mongoose API changes.
Maintainers
Readme
@vetrivelan-cp/mongoose
CPBS abstraction layer over Mongoose and @nestjs/mongoose.
All applications should import from @vetrivelan-cp/mongoose — never directly from mongoose or @nestjs/mongoose. This shields applications from breaking Mongoose API changes by centralizing upgrades in this package.
Installation
npm i @vetrivelan-cp/mongoosePeer dependencies
This package expects the following peer dependencies to be installed by the consuming application:
mongoose(^6 || ^7 || ^8)@nestjs/mongoose(^9 || ^10)@nestjs/common(^9)@nestjs/core(^9)rxjs(required forhandleRetry)
Usage
1) Import decorators and helpers
import {
Schema,
Prop,
SchemaFactory,
InjectModel,
InjectConnection,
mongoose,
SchemaTypes,
Types,
} from '@vetrivelan-cp/mongoose';2) Module setup
import { Module } from '@nestjs/common';
import { MongooseModule } from '@vetrivelan-cp/mongoose';
@Module({
imports: [MongooseModule.forRoot('mongodb://localhost:27017/mydb')],
})
export class AppModule {}3) Define schemas
import { Schema, Prop, SchemaFactory } from '@vetrivelan-cp/mongoose';
@Schema({ timestamps: true })
export class User {
@Prop({ required: true })
name!: string;
}
export const UserSchema = SchemaFactory.createForClass(User);4) Inject models / connections
import { Injectable } from '@nestjs/common';
import { InjectModel, Model } from '@vetrivelan-cp/mongoose';
@Injectable()
export class UsersService {
constructor(@InjectModel('User') private readonly userModel: Model<any>) {}
}5) Repository base class
Note:
Modelis re-exported from Mongoose, so you can import it directly from@vetrivelan-cp/mongoose.
import { BaseRepository } from '@vetrivelan-cp/mongoose';
export class UsersRepository extends BaseRepository<any> {
// add custom methods here
}Tokens & retry utilities
This package also exports token helpers and a retry operator (useful for connection initialization):
import {
getModelToken,
getConnectionToken,
handleRetry,
raw,
} from '@vetrivelan-cp/mongoose';getModelToken(model: string, connectionName?: string): stringgetConnectionToken(name?: string): stringhandleRetry(retryAttempts?: number, retryDelay?: number, verboseRetryLog?: boolean)raw(definition: Record<string, any>): Record<string, any>
Exports
This package re-exports commonly used items from:
mongoose@nestjs/mongoose
including:
- Decorators:
Schema,Prop,SchemaFactory - Injection helpers:
InjectModel,InjectConnection - Nest module:
MongooseModule - Repository:
BaseRepository - Tokens/retry:
getModelToken,getConnectionToken,handleRetry,raw - Types/utilities:
ObjectId,toObjectId,CpbsDocument,FilterQuery, etc.
See src/index.ts for the full export surface.
Scripts
npm run build– build todist/npm run build:watch– build in watch modenpm test– run unit testsnpm run test:watch– run tests in watch modenpm run test:cov– run tests with coveragenpm run lint– run eslint autofix
Publishing
npm run prepublishOnly– runs tests, builds, and removesdist/tsconfig.build.tsbuildinfo(if present)
License
MIT
