inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.3k stars 717 forks source link

Using inversify with mongoose: documentation needed #598

Closed asykes74 closed 7 years ago

asykes74 commented 7 years ago

I have been combing the internet for several days looking for an example that outlines how to use inversify with mongoose. I found an example using the mongo driver but nothing describing how to use mongoose. My schema are defined in a similar fashion to this example: http://brianflove.com/2016/10/04/typescript-declaring-mongoose-schema-model/. My confusion seems to be centered around how the container.bind should be declared for this scenario, inside the inversify.config.ts file? I tried something like this,

container.bind<Model<IUserModel>>(TYPES.User).to(model.user).whenTargetNamed("userSchemaModel"); where model.user is declared as model.user = mongoose.connection.model<IUserModel>("User", UserSchema);

for some reason .to is not working for this scenario. Not I also tried toConstantValue, which did not result in any red errors in WebStorm but threw an exception when I tried container.get indicating "uncaughtException: No matching bindings found for serviceIdentifier: Model\n Model - named: userSchemaModel". How should I implement this binding?

lholznagel commented 7 years ago

Can you create a small example repo? Maybe then I can help you better.

remojansen commented 7 years ago

I have a mongo / mongoose demo at https://github.com/stelltec/public-tech-demos/tree/master/nodejs-madrid-meetup/demo3 I hope it hels.

remojansen commented 7 years ago

@asykes74 please let me know if this issue can be closed?

asykes74 commented 7 years ago

Yes. The link makes sense. Thank you for providing it. One question: my original code injects the actual Model into the repository object. To avoid drastically changing code, is there a way that I could modify the container.bind above to accommodate injecting just the Model object into the repository? Please explain how I can tailor the container.bind to fit the scenario below, if not possible, I will change to fit the example you provide in the link.

Here is a sample of my UserRepository definition:

export class UserRepository extends RepositoryBase<IUserModel, IUser> implements IUserRepository {
    userSchemaModel: Model<Document>;

    constructor (@inject("Model<IUserModel>") @named(“userSchemaModel") userSchemaModel: Model<Document>)

Here is the container.bind code currently: container.bind<Model<IUserModel>>(TYPES.User).to(model.user).whenTargetNamed("userSchemaModel");

Here is the sample of my base repository definition:

export class RepositoryBase<T extends Document, U > implements IRepository<T, U> {
        protected _model: Model<Document>;

        constructor (schemaModel: Model<Document>) {
            this._model = schemaModel;
        }

Here is the sample of the IUserModel: export interface IUserModel extends IUser, Document {