| name | bun-elysia-expert |
| description | Build production-ready optimized APIs with Bun.js, Elysia.js, and Prisma ORM. Use this skill for API development, PostgreSQL database integration, optimized queries, deployment to Railway/VPS (with or without Docker), Nginx configuration, and SSL setup. Covers routing, validation, authentication, WebSocket, Swagger/Scalar documentation, caching with Redis, and end-to-end type safety with Eden. |
Bun + Elysia.js Full-Stack Development
Build high-performance, production-ready full-stack applications with Bun runtime, Elysia.js framework, and Prisma ORM.
Quick Start
New Elysia Project
bun create elysia my-app
cd my-app
bun run dev
Basic Server
import { Elysia, t } from "elysia";
const app = new Elysia()
.get("/", () => "Hello, World!")
.get("/users/:id", ({ params }) => ({ id: params.id }), {
params: t.Object({ id: t.Number() }),
})
.post("/users", ({ body }) => ({ created: body }), {
body: t.Object({
name: t.String(),
email: t.String({ format: "email" }),
}),
})
.listen(3000);
With Bun Native APIs
import { Elysia } from "elysia";
import { sql } from "bun";
import { redis } from "bun";
const app = new Elysia()
.decorate("sql", sql)
.decorate("redis", redis)
.get("/users", async ({ sql }) => {
return sql`SELECT * FROM users`;
})
.get("/cache/:key", async ({ redis, params }) => {
return redis.get(params.key);
})
.listen(3000);
Reference Navigation
What do you want to do?
Set up a project:
Use Bun native APIs:
Database with Prisma ORM:
Build with Elysia:
API Design Best Practices:
Deploy to Production:
Migrate from Fastify:
Common Patterns
Authentication Middleware
import { Elysia } from "elysia";
import { jwt } from "@elysiajs/jwt";
const app = new Elysia()
.use(jwt({ secret: process.env.JWT_SECRET! }))
.derive(async ({ headers, jwt }) => {
const token = headers.authorization?.replace("Bearer ", "");
const user = token ? await jwt.verify(token) : null;
return { user };
})
.get("/profile", ({ user, error }) => {
if (!user) return error(401);
return user;
});
Database with Validation
import { Elysia, t } from "elysia";
import { sql } from "bun";
const app = new Elysia().post(
"/users",
async ({ body }) => {
const [user] = await sql`
INSERT INTO users (name, email)
VALUES (${body.name}, ${body.email})
RETURNING *
`;
return user;
},
{
body: t.Object({
name: t.String({ minLength: 1 }),
email: t.String({ format: "email" }),
}),
},
);
Grouped Routes with Guard
app.group("/api", (app) =>
app.guard(
{
headers: t.Object({
authorization: t.String(),
}),
beforeHandle: ({ headers, error }) => {
if (!isValidToken(headers.authorization)) {
return error(401);
}
},
},
(app) => app.get("/users", getUsers).post("/users", createUser),
),
);
WebSocket Chat
app.ws("/chat", {
body: t.Object({
room: t.String(),
message: t.String(),
}),
open(ws) {
ws.subscribe("general");
},
message(ws, { room, message }) {
ws.publish(room, { from: ws.id, message });
},
});
Key Differences: Fastify vs Elysia
| Fastify | Elysia |
|---|
request.params | { params } |
request.query | { query } |
request.body | { body } |
reply.code(n).send(x) | set.status = n; return x |
reply.header(k, v) | set.headers[k] = v |
| JSON Schema | TypeBox (t.*) |
preHandler hook | beforeHandle |
fastify.decorate() | .decorate() |
fastify.register(plugin) | .use(plugin) |
Installation
bun add elysia @elysiajs/swagger @elysiajs/jwt @elysiajs/cors @elysiajs/static @elysiajs/eden
bun add prisma @prisma/client
bunx prisma init --datasource-provider postgresql
bun add -d @types/bun typescript
Prisma Integration
Basic Setup with Elysia
import { Elysia, t } from "elysia";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const app = new Elysia()
.decorate("prisma", prisma)
.get("/users", async ({ prisma }) => {
return prisma.user.findMany({
select: { id: true, email: true, name: true },
});
})
.post(
"/users",
async ({ prisma, body }) => {
const user = await prisma.user.create({
data: { email: body.email, name: body.name },
});
return { id: user.id };
},
{
body: t.Object({
email: t.String({ format: "email" }),
name: t.String(),
}),
},
)
.onStop(async () => {
await prisma.$disconnect();
})
.listen(3000);
Swagger/Scalar Documentation
Scalar is the default documentation UI for Elysia's swagger plugin.
import { Elysia, t } from "elysia";
import { swagger } from "@elysiajs/swagger";
const app = new Elysia()
.use(
swagger({
documentation: {
info: {
title: "My API",
version: "1.0.0",
},
tags: [{ name: "users", description: "User operations" }],
},
path: "/docs",
scalarConfig: {
theme: "purple",
},
}),
)
.get("/users", () => "List users", {
detail: {
summary: "Get all users",
tags: ["users"],
},
})
.listen(3000);