nest
nest copied to clipboard
feat(core): Add multi provider option
PR Checklist
Please check if your PR fulfills the following requirements:
- [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
PR Type
What kind of change does this PR introduce?
[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:
What is the current behavior?
No multi
provider option
Issue Number: #770 #1517
What is the new behavior?
Support the multi
option for providers
const MULTI_TOKEN = Symbol('MY_MULTI_TOKEN');
class Test {
value = 'B';
}
@Module({
providers: [
{
multi: true,
useValue: 'A',
provide: MULTI_TOKEN
},
]
})
class AModule { }
@Module({
providers: [
{
multi: true,
useClass: Test,
provide: MULTI_TOKEN
},
]
})
class BModule { }
@Module({
imports: [AModule, BModule],
providers: [
{
multi: true,
useFactory: () => 'Application',
provide: MULTI_TOKEN,
},
]
})
class ApplicationModule { }
async function bootstrap() {
const app = await NestFactory.createApplicationContext(ApplicationModule);
const result = app.get(MULTI_TOKEN);
console.log(result);
}
bootstrap();
Does this PR introduce a breaking change?
[ ] Yes
[x] No
Other information
Pull Request Test Coverage Report for Build 3416
- 40 of 53 (75.47%) changed or added relevant lines in 2 files are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage decreased (-0.3%) to 94.21%
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
---|---|---|---|
packages/core/injector/module.ts | 38 | 51 | 74.51% |
<!-- | Total: | 40 | 53 |
Totals | |
---|---|
Change from base Build 3413: | -0.3% |
Covered Lines: | 3531 |
Relevant Lines: | 3748 |
💛 - Coveralls
@kamilmysliwiec Add 6.8.0 or 7.0 ?
@kamilmysliwiec will this get implemented at some point, or should it just be scratched completely? :smile:
I'm more inclined towards this syntax https://github.com/nestjs/nest/issues/4786 (composite providers) instead (more explicit)
So you ask people to see #4786 in this PR, and closed #4786 to ask people to see this PR? If I understand correctly, you still prefer this implementation, right?
Will it be added? I really need something like this
News?
https://github.com/inversify/InversifyJS/blob/master/wiki/multi_injection.md
I keep encountering scenarios where multi providers would be very useful... 😢
https://github.com/nestjs/nest/issues/770#issuecomment-1412029060