Skip to main content

Hono

Build a REST API with Hono and jcell. Hono is ultra-fast and works on Node, Bun, Deno, and Cloudflare Workers.

Setup​

npm install @sajjadbzn/jcell hono
# or
bun add @sajjadbzn/jcell hono

Complete API​

app.ts
import { Hono } from 'hono'
import { createDB, schema, t, fileAdapter } from '@sajjadbzn/jcell'

// ── Schema ─────────────────────────────────────────────────────────────────

const taskSchema = schema({
id: t.id(),
title: t.string(),
completed: t.boolean().default(false),
priority: t.enum(['low', 'medium', 'high'] as const),
createdAt: t.date().default(() => new Date()),
})

// ── Database setup ─────────────────────────────────────────────────────────

const db = createDB({ adapter: fileAdapter({ path: './data' }) })
const tasks = db.collection('tasks', taskSchema)

// ── Hono app ───────────────────────────────────────────────────────────────

const app = new Hono()

// GET /tasks — list all tasks
app.get('/tasks', async (c) => {
const all = await tasks.find()
return c.json(all)
})

// POST /tasks — create a task
app.post('/tasks', async (c) => {
const body = await c.req.json()
try {
const task = await tasks.insert({
title: body.title,
priority: body.priority ?? 'medium',
})
return c.json(task, 201)
} catch (err) {
return c.json({ error: (err as Error).message }, 400)
}
})

// PATCH /tasks/:id — update a task
app.patch('/tasks/:id', async (c) => {
const id = c.req.param('id')
const body = await c.req.json()
const count = await tasks.update({ id }, body)
if (count === 0) return c.json({ error: 'Not found' }, 404)
const updated = await tasks.first({ id })
return c.json(updated)
})

// DELETE /tasks/:id — delete a task
app.delete('/tasks/:id', async (c) => {
const id = c.req.param('id')
const count = await tasks.delete({ id })
if (count === 0) return c.json({ error: 'Not found' }, 404)
return c.json({ deleted: true })
})

// ── Start server ───────────────────────────────────────────────────────────

const port = Number(process.env.PORT) || 3000
console.log(`Hono + jcell running at http://localhost:${port}`)
export default { port, fetch: app.fetch }

Run It​

bun run app.ts

Hono on Cloudflare Workers​

Hono + jcell + D1 fits together naturally on Cloudflare Workers:

import { Hono } from 'hono'
import { createDB, schema, t, d1Adapter } from '@sajjadbzn/jcell/d1'

const app = new Hono()

app.get('/tasks', async (c) => {
const db = createDB({
adapter: d1Adapter({ binding: c.env.DB })
})
const tasks = db.collection('tasks', taskSchema)
return c.json(await tasks.find())
})

export default app