@synap/nest-mongoose-module
v2.0.3
Published
An extension of Nest's MongooseModule with updated mongoose package and ability to add schema hooks with dependency injection.
Downloads
11
Readme
MongooseModule
This is an extension of Nest's MongooseModule. The only differences are
- The mongoose package is 5.3.1 as opposed to 5.0.1 which is the version included in @nestjs/mongoose
- The
createMongooseProviders
method has been updated to allow schema hooks and dependency injection
Usage
import { Module } from '@nestjs/common';
import { MongooseModule, InjectModel } from './';
import { Schema, Model, Document } from 'mongoose';
// CoreModule
@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost:27017/my-db', { useNewUrlParser: true }),
MyModule
]
})
export class CoreModule {}
// Document Interface
export interface MyDocument extends Document
{
someProp:string;
}
// MyService
export class MyService
{
constructor(@InjectModel('MyModel') private readonly myModel:Model<MyDocument>)
{
}
}
// MyModule
@Module({
imports: [
MongooseModule.forFeature([{
name: 'MyModel',
schema: new Schema({ someProp: { type: String } }),
hooks: [
{
type: 'post',
method: 'remove',
fn: async (doc:MyDocument, svc:MyService) => console.log('MyModel.postRemove'),
inject: ['MyService']
}
]
}])
]
})
export class MyModule {}