Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Why my custom decorator injects the ContainerInstance? #571

Open
Bennjamon opened this issue Dec 9, 2021 · 4 comments
Open

Question: Why my custom decorator injects the ContainerInstance? #571

Bennjamon opened this issue Dec 9, 2021 · 4 comments
Labels
type: question Questions about the usage of the library.

Comments

@Bennjamon
Copy link

I am creating my custom decorator InjectConnection:

export default function InjectConnection(dbName: string) {
  return (object: any, propertyName: string, index: number) => {
    Container.registerHandler({
      object,
      propertyName,
      index,
      value: () => dbName,
    });
  };
}

And I am tryíng to use it in a service:

@Service()
export default class UsersDBService {
  User: Model<UserDocument>;

  constructor(@InjectConnection('users') connection: any) {
    console.log(connection);

    // this.User = connection.model<UserDocument>('user');
  }
}

But, in the console.log the ContainerInstance is printed

@Bennjamon Bennjamon added the type: question Questions about the usage of the library. label Dec 9, 2021
@NoNameProvided
Copy link
Member

NoNameProvided commented Jan 13, 2022

The container injects itself as the last parameter for historical reasons. If you get the container for the connection parameter it seems the container could not resolve that parameter.

It's not optimal, but I think this is the reason for this behavior. (Can be wrong, I haven't touched this repo in a while)

@Bennjamon
Copy link
Author

Bennjamon commented Jan 13, 2022

@NoNameProvided, Ok, but is there any way to inject a custom value that is not in the container? In the example, in my code I want to inject the value of dbName

@NoNameProvided
Copy link
Member

NoNameProvided commented Jan 13, 2022

Ok, but is there any way to inject a custom value that is not in the container?

You need to register it first.

@Bennjamon
Copy link
Author

You need to register it first.

I have that code:

export default function InjectConnection(dbName: string) {
  return (object: any, propertyName: string, index: number) => {
    Container.registerHandler({
      object,
      propertyName,
      index,
      value: (containerInstance) => {
        const connection = createConnection(dbName);
        console.log("Connection %s created", dbName);

        return connection;
      },
    });
  };
}

But is not working and the value method is not called, What is wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

2 participants