In Inversify.js there is multiInject
decorator that allow us to inject multiple objects as array. All objects' dependencies in this array resolved as well.
Is there any way to achieve this in Nest.js?
In Inversify.js there is multiInject
decorator that allow us to inject multiple objects as array. All objects' dependencies in this array resolved as well.
Is there any way to achieve this in Nest.js?
Share Improve this question edited Oct 19, 2018 at 17:14 Kim Kern 60.4k20 gold badges216 silver badges212 bronze badges asked Oct 19, 2018 at 15:31 Artsiom MiksiukArtsiom Miksiuk 4,30310 gold badges38 silver badges53 bronze badges2 Answers
Reset to default 21There is no direct equivalent to multiInject
. You can provide an array with a custom provider though:
Example
Try the example live in this sandbox.
Injectables
Let's assume you have multiple @Injectable
classes that implement the interface Animal
.
export interface Animal {
makeSound(): string;
}
@Injectable()
export class Cat implements Animal {
makeSound(): string {
return 'Meow!';
}
}
@Injectable()
export class Dog implements Animal {
makeSound(): string {
return 'Woof!';
}
}
Module
The classes Cat
and Dog
are both available in your module (provided there or imported from another module). Now you create a custom token for the array of Animal
:
providers: [
Cat,
Dog,
{
provide: 'MyAnimals',
useFactory: (cat, dog) => [cat, dog],
inject: [Cat, Dog],
},
],
Controller
You can then inject and use the Animal
array in a Controller like this:
constructor(@Inject('MyAnimals') private animals: Animal[]) {
}
@Get()
async get() {
return this.animals.map(a => a.makeSound()).join(' and ');
}
This also works if Dog
has additional dependencies like Toy
, as long as Toy
is available in the module (imported/provided):
@Injectable()
export class Dog implements Animal {
constructor(private toy: Toy) {
}
makeSound(): string {
this.toy.play();
return 'Woof!';
}
}
Just a minor tweak to the great solution by @kim-kern you can use that solution but avoid a small bit of overhead for adding new entries ...
replace
providers: [
Cat,
Dog,
{
provide: 'MyAnimals',
useFactory: (cat, dog) => [cat, dog],
inject: [Cat, Dog],
},
],
with
providers: [
Cat,
Dog,
{
provide: 'MyAnimals',
useFactory: (...animals: Animal[]) => animals,
inject: [Cat, Dog],
},
],
It's only minor but instead of having to add a new one in 3 places for every new addition it's down to 2. Adds up when you have a few and reduces chance of error.
Also the nest team are working on making this easier an you can track via this github issue: https://github.com/nestjs/nest/issues/770