Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@synap/nest-mongoose

labanalytix1ISC1.0.5TypeScript support: included

An extension of Nest's MongooseModule with updated mongoose package and ability to add schema hooks with dependency injection.

readme

MongooseModule

This is an extension of Nest's MongooseModule. The only differences are 1) The mongoose package is 5.3.1 as opposed to 5.0.1 which is the version included in @nestjs/mongoose 2) 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 {}