1.6 KiB
1.6 KiB
monk-code-screen
Billing/subscriptions/invoicing backend skeleton. Bun + Hono + bun:sqlite (zero network deps at runtime; TS runs natively, no build step).
⚡ START COMMAND (sticky)
bun run dev # http://localhost:3000, hot reload
Other commands:
bun run start # no watch
bun run db:reset # wipe data.db and reseed
bunx tsc --noEmit # typecheck
Layout
src/index.ts Hono app + routes
src/db.ts opens data.db, applies schema, seeds if empty (runs on import)
src/schema.sql customers / plans / subscriptions / invoices (idempotent)
DB is data.db at repo root (WAL, FK on). Boot always re-applies schema, so adding a table = edit schema.sql + restart.
Routes
curl localhost:3000/health
curl localhost:3000/customers
curl localhost:3000/customers/cus_ada/invoices
curl -X POST localhost:3000/subscriptions \
-H 'content-type: application/json' \
-d '{"customer_id":"cus_grace","plan_id":"plan_basic"}'
POST /subscriptions is the happy path: validates with zod, creates subscription + first invoice in one transaction, returns both (201).
Seed data
- customers:
cus_ada,cus_grace - plans:
plan_basic($10/mo),plan_pro($50/mo) cus_adahas an activesub_1on pro with openinv_1
Extension cheatsheet (interview)
- New route: add to
src/index.ts,db.query(...).all()/get(),db.run(sql, [params]) - Multi-write:
db.transaction(() => { ... })() - Validation:
z.object({...}).safeParse(await c.req.json()) - New table: append
CREATE TABLE IF NOT EXISTSto schema.sql, restart (ordb:resetif changing existing tables)