Sujstack
Bun · Next 16 · React 19 · TypeScript 7 · Drizzle · tRPC · OpenTelemetry
What is this
A full-stack template built on Ports and Adapters. Storage, logging and metrics sit behind hand-written interfaces, so each provider is swappable without touching a call site.
Nothing is connected out of the box: the app boots, typechecks, tests and runs with no database and no credentials. The cards below exercise the real code paths, running against the in-memory adapters.
The ports and adapters live in kit/, a separately versioned package pinned to a release. The app owns only the composition roots, the metrics catalog, the routers and this UI.
To start clean, delete apps/next/src/app/_demo/, then demo.ts and demo.test.ts in apps/next/src/server/trpc/routers/, and the router's line in _app.ts.
Data layer
Procedures see only the port interfaces — never which adapter is behind them. Adapters are chosen in code, in each composition root.
Checking…
Postgres transactions
Inserts a row inside a real transaction, then commits or rolls it back and checks what survived. Runs on any Postgres; on Neon, the row below names the branch that answered.
Background jobs
This Vercel function writes a two-second job into a pg-boss table on Neon, inside the same transaction as any other write, so the job exists only if that commits. A long-running worker (apps/worker, a Bun + Express process on Render, provisioned by infra/) polls that table, runs the job and stores its output; this card polls the job's row until it settles. A failing job is retried twice, then marked failed.
The worker runs against production only, so on a preview deployment jobs stay queued. It is off until infra/ sets worker_enabled, and until then deployed jobs stay queued too.
Key/value
A raw round trip with a 60 second TTL, and a write through a namespace that validates every value against a schema. The procedure's input is looser than the schema, so the namespace is what rejects a bad value.
Vector search
Seeds eight sample sentences and returns the three nearest. Embeddings are a toy hashed bag of words, so matches are by shared words; swap in a real model for meaning.
Faults, logs and metrics
Every procedure call is logged as
trpc.<path> and counted in trpc.requests. Severity follows the fault. A server fault's message never crosses the wire — the real one is only in the server log. Watch the terminal running bun run dev.log: info · metric: outcome="ok"
log: warn · metric: outcome="client_error"
log: error + cause · metric: outcome="server_error"
Webhooks
POST /api/webhooks/demo is built by the kit's createWebhookHandler: it verifies the raw body with the app's own verifier before anything is parsed, then runs the app's handler. The kit logs each delivery as webhook.demo and hands the result to the app's onDelivery, which counts it in webhooks.received. Here a tRPC procedure plays the external service, signing and POSTing each delivery.log: info · metric: outcome="accepted"
log: warn · metric: outcome="rejected"
log: error + cause · metric: outcome="failed"
Alert rules as code
Counters and rules are declared in
apps/next/src/server/metrics/catalog.ts. Rules reference typed counters and compile to the Prometheus rule file below, committed as apps/next/rules.yaml.tRPC procedure calls that reached a procedure, by outcometrpc_requests_total
Webhook deliveries, by source and outcomewebhooks_received_total
Background job attempts, by job and outcomejobs_processed_total
# Generated by `bun run metrics:rules`. Do not edit.
groups:
- name: "sujstack"
rules:
- alert: "TrpcServerErrorRatio"
expr: "sum(rate(trpc_requests_total{outcome=\"server_error\"}[5m])) / sum(rate(trpc_requests_total[5m])) > 0.05"
for: "10m"
labels:
severity: "page"
annotations:
summary: "More than 5% of tRPC calls are failing with a server error"
- alert: "JobsFailing"
expr: "sum(rate(jobs_processed_total{outcome=\"failed\"}[15m])) > 0"
for: "0m"
labels:
severity: "ticket"
annotations:
summary: "A background job has exhausted its retries"
Render errors
A component that throws while rendering.
error.tsx catches it, logs app.unhandled with the cause, and shows the generic message.