Skip to content

Instantly share code, notes, and snippets.

@ethanniser
Last active February 8, 2025 20:30
Show Gist options
  • Save ethanniser/998522056c046d5911c0e05f69eb94a5 to your computer and use it in GitHub Desktop.
Save ethanniser/998522056c046d5911c0e05f69eb94a5 to your computer and use it in GitHub Desktop.
Seperate Files in Elysia
export const ctx = new Elysia({
name: "@app/ctx",
})
.use(plugin())
.decorate("db", db)
.decorate("config", config)
import { ctx } from "./base"
export const todos = new Elysia()
.use(ctx)
.get("/todos", async () => {
const data = await db.select().from(todos);
return data;
})
import { todos } from "./handlers"
const app = new Elysia()
.use(todos)
// ...
.listen(3000)
@mnpenner
Copy link

mnpenner commented Feb 8, 2025

Thanks for sharing! If I want more than one handlers.ts file though, they each need .use(ctx) to get all the type hinting, no? Won't that cause the ctx to be used multiple times?

Edit: Or maybe that isn't a problem? https://elysiajs.com/essential/best-practice.html#reuse-a-plugin

@ethanniser
Copy link
Author

i am almost positive they do get deduplicated so yes
but i would ask in the elysia discord bc its been so long

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment