-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
feat: allow for microservice options to come from the di container #12622
feat: allow for microservice options to come from the di container #12622
Conversation
Pull Request Test Coverage Report for Build fcc8bd07-d1f6-4348-a848-ddd8d3ad7704
💛 - Coveralls |
packages/microservices/interfaces/microservice-configuration.interface.ts
Outdated
Show resolved
Hide resolved
7218e44
to
7ed071d
Compare
Microservices are now able to be created by getting their options from within the DI container itself. This has been a long requested feature of developers and I finally had some time to work through how we could possibly let this happen.
7ed071d
to
2f22522
Compare
app = await NestFactory.createMicroservice< | ||
AsyncOptions<MicroserviceOptions> | ||
>(RpcModule, { | ||
logger: false, | ||
inject: [RpcOptionsProvider], | ||
useFactory: (optionsProvider: RpcOptionsProvider) => | ||
optionsProvider.getOptions(), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we couldn't have something to make this feels like a traditional dynamic module import...
something like:
const app = await NestFactory.createMicroservice(
RpcModule,
MicroserviceOptionsFactory.createAsync<MicroserviceOptions>({
inject: [RpcOptionsProvider],
useFactory: (optionsProvider: RpcOptionsProvider) => ({
logger: false,
...optionsProvider.getOptions(),
})
})
)
perharps that's too overkill (or impossible) to have? yeah...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, just to give a unified API, but the underlying code and how it gets read in the server options resolver would pretty much be the same. This would just be a simple wrapper around the object we already return, and I don't think there would be much benefit
Microservices are now able to be created by getting their options from within the DI container itself. This has been a long requested feature of developers and I finally had some time to work through how we could possibly let this happen.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
There is no way to apply the microservice config for a microservice server, by getting the configuration from within the DI container, like from the
ConfigService
Issue Number:
What is the new behavior?
Microservices are now able to be configured via a provider that was registered within the DI tree, without creating a new
NestFactory.createApplicationContext()
first. Devs are now able to pass auseFactory
andinject
as a part of the options object passed toNestFactory.createMicroservice()
which will allow for Nest to find the tokens in the inject and pass them to theuseFactory
to get the configuration for the microservice. E.g.:Does this PR introduce a breaking change?
Other information