-
-
Notifications
You must be signed in to change notification settings - Fork 290
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(di): allow controllers to be scoped to environment #2551
Conversation
Benchmarks
ExplanationThe benchmark shows a performance difference between the frameworks. We note that Ts.ED is often last. In fact, Ts.ED uses features useful to a production application which reduce its performance. For example, Ts.ED initializes a sandbox (async_hook) for each request in order to work in an isolated context if necessary. All this at a necessary cost that reflects the reality of a production application ;) |
d4c2ec3
to
0acd261
Compare
Benchmarks
ExplanationThe benchmark shows a performance difference between the frameworks. We note that Ts.ED is often last. In fact, Ts.ED uses features useful to a production application which reduce its performance. For example, Ts.ED initializes a sandbox (async_hook) for each request in order to work in an isolated context if necessary. All this at a necessary cost that reflects the reality of a production application ;) |
0acd261
to
1fb96fc
Compare
Benchmarks
ExplanationThe benchmark shows a performance difference between the frameworks. We note that Ts.ED is often last. In fact, Ts.ED uses features useful to a production application which reduce its performance. For example, Ts.ED initializes a sandbox (async_hook) for each request in order to work in an isolated context if necessary. All this at a necessary cost that reflects the reality of a production application ;) |
Hello @abenerd So to do that:
export function resolveControllers(settings: Partial<TsED.Configuration>): TokenRoute[] {
const providers = lookupProperties.flatMap((property) => getTokens(settings[property]));
/// get env from settings
const env = settings.env
/// filter this list
return [...resolveRecursively(providers), ...providers].filter((provider) => !!provider.route) as any[];
}
Implementation: https://github.com/tsedio/tsed/blob/production/packages/di/src/common/utils/createContainer.ts#L5 export function createContainer(rootModule?: Type<any>) {
// GlobalProviders.entries() => returns Provider
const container = new Container(GlobalProviders.entries());
if (rootModule) { // can be removed because there is no usage of rootModule signature in the code base (I check on tsed/cli also
container.delete(rootModule);
}
return container;
} Suggested: import {Env} from "@tsed/core"
export function createContainer(env?: Env) { //or settings: DIConfiguration ?
/// change the code implementation to
} To cover all this, we can do a test as follows: @Controller({path: "/", environments: [Env.DEV]})
class MyController {
}
describe('Test env', () => {
describe('Env = DEV', ()=> {
beforeEach(() => PlatformTest.create({env: Env.DEV})
afterEach(() => Platform.reset())
it('should return the MyController provider', () => {
expect(PlatformTest.injector.get(MyController)).toBeInstanceof(MyController)
})
})
describe('Env = PROD', ()=> {
beforeEach(() => PlatformTest.create({env: Env.PROD})
afterEach(() => Platform.reset())
it('should return the MyController provider', () => {
expect(PlatformTest.injector.get(MyController)).not.toBeDefined()
})
})
}) See you :) |
You're right that would be even better! |
Yes you can use a simple string type instead of enum ;) |
f37ccd1
to
5306644
Compare
Information
Allow controllers to be scoped to an environment.
Todos