Closed asykes74 closed 7 years ago
Can you create a small example repo? Maybe then I can help you better.
I have a mongo / mongoose demo at https://github.com/stelltec/public-tech-demos/tree/master/nodejs-madrid-meetup/demo3 I hope it hels.
@asykes74 please let me know if this issue can be closed?
Yes. The link makes sense. Thank you for providing it. One question: my original code injects the actual Model
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 {
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 asmodel.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?