Some proposed ways of implementing scoped services:
-
Create scopes at the time of usage:
import { Elysia } from "elysia";
import { container } from './dependencies';
const app = new Elysia()
.transform(({ request }) => {
const scope = container
.createScope({ request: () => request })
.register("authService", createAuthService);
return scope.resolveAll();
})
.get("/session", ({ authService }) => authService.getSession());
-
Define the entire dependency tree in one place, and just create the scope when needed:
export const container = createIocContainer()
.register("db", Database)
.defineScope<{ request: Request }>(
"request",
(scope) => scope
.register("authService", AuthService })
.register("userService", UserService })
);
const app = new Elysia()
.transform(({ request }) => container.createScope("request", { request }))
.get("/session", ({ authService }) => authService.getSession());
Both methods would support nested scopes and multiple other scopes.
Some proposed ways of implementing scoped services:
Create scopes at the time of usage:
Define the entire dependency tree in one place, and just create the scope when needed:
Both methods would support nested scopes and multiple other scopes.