Skip to content

Scopes #4

@aklinker1

Description

@aklinker1

Some proposed ways of implementing scoped services:

  1. 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());
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions