GithubHelp home page GithubHelp logo

Comments (4)

kamilmysliwiec avatar kamilmysliwiec commented on April 25, 2024

There is currently no way to add mongoose schema hooks that depend on any @Injectable() services using the nest MongooseModule.

Actually, why not? You should be able to create, let's say, SchemaHooks class that would inject schema + all required services and register hooks in onModuleInit() lifecycle method.

from mongoose.

 avatar commented on April 25, 2024

@kamilmysliwiec Could you possibly provide an example of that? As far as I can see you cannot register schema hooks after connection.model() is called, which the MongooseModule does in its providers factory.

from mongoose.

kamilmysliwiec avatar kamilmysliwiec commented on April 25, 2024

Oh well, it seems that this behavior has changed between 4.x.x -> 5.x.x releases. In this case, you can avoid using .forFeature() and create providers manually:

{
      provide: getModelToken('Cat'),
      useFactory: (connection) => {
           const schema = ...
           // add hooks here
          return connection.model('Cat', schema);
      },
      inject: [getConnectionToken('YOUR_CONNECTION_NAME')],
},

from mongoose.

jselesan avatar jselesan commented on April 25, 2024

@kamilmysliwiec sorry to ask on a closed issue. I'm trying to create my providers manually as you posted here, but I get a ** Nest can't resolve dependencies of the useFactory (?). Please verify whether [0] argument is available in the current context.**

This is my code on the test:

        let catModel;
        const module = await Test.createTestingModule({
            providers: [
                CatsService,
                {
                    provide: getModelToken('Cat'),
                    useFactory: (connection) => {
                        catModel = connection.model('Cat', CatSchema);
                        return catModel;
                    },
                    inject: [getConnectionToken('MyDatabase')]
                }
            ]
        }).compile();

and my service looks like this:


@Injectable()
export class CatsService {
  constructor(@InjectModel('Cat') private readonly catModel: Model<Cat>) {}

  async create(createCatDto: CreateCatDto): Promise<Cat> {
    const createdCat = new this.catModel(createCatDto);
    return await createdCat.save();
  }

  async findAll(): Promise<Cat[]> {
    return await this.catModel.find().exec();
  }
}

What am I doing wrong?

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.