bun-patternslisted
Install: claude install-skill Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack
# Bun Patterns
## HTTP Server (Bun.serve)
```ts
const server = Bun.serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url)
if (url.pathname === '/api/health') {
return Response.json({ status: 'ok' })
}
if (url.pathname === '/api/users' && req.method === 'GET') {
const users = await db.user.findMany()
return Response.json(users)
}
if (url.pathname === '/api/users' && req.method === 'POST') {
const body = await req.json()
const user = await db.user.create({ data: body })
return Response.json(user, { status: 201 })
}
return new Response('Not Found', { status: 404 })
},
})
console.log(`Listening on ${server.url}`)
```
## File I/O
```ts
// Read
const text = await Bun.file('data.txt').text()
const json = await Bun.file('config.json').json()
const buffer = await Bun.file('image.png').arrayBuffer()
// Write
await Bun.write('output.txt', 'Hello World')
await Bun.write('data.json', JSON.stringify(obj, null, 2))
// Stream large file
const file = Bun.file('large.csv')
const stream = file.stream()
```
## SQLite (built-in)
```ts
import { Database } from 'bun:sqlite'
const db = new Database('myapp.db')
// Create table
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
)
`)
// Prepared statements (type-safe)
const insert = db.prepare('INSERT INTO users (name, email) VALUES ($name, $email)')