Search code examples
node.jsmongodbinversion-of-controlinversifyjs

inversify: how to bind Model<Document> to container object


I am developing an application using typescript, mean stack, inversify-express-utils and inversifyjs as my IOC container. I am receiving an error for my entries in the inversify.config.ts file for the entry that add a Model to the container. For example,

inversify.config.ts:

container.bind<Model<Document>>(TYPES.Document).to(User).whenTargetNamed("userSchema");

This seems to result in the following error: "Argument of type Model is not assignable to parameter of type 'new (...args: any[]) => Model'. Type 'IUserModel' is not assignable type 'Model'. Property 'findByid' is missing in type 'IUserModel'."

igroupmodel.ts:

export interface IUserModel extends IUser, Document

types.ts:

Document: Symbol("Model")

group.js(last line):

export let User = mongoose.model<IUserModel>("User", UserSchema);

What is the correct way to inject a mongoose model into the container? I have been looking for an example demonstrating how to work with inversifyjs and mongoose and have not found a solution.

Update: When I substitute to(User) with toConstantValue(User) I am no longer getting the error. However, my log file seems to display the following error in my log files:

"Error: No matching bindings found for serviceIdentifier: Model"


Solution

  • You can find an example of mongoose + inversify here. The example uses the onion architecture but you should be able to use a different architecture if you don't like it...