HeadlinesBriefing favicon HeadlinesBriefing.com

bun-sqlgen Brings Type-Safe SQL Queries to Bun Without ORM Overhead

Hacker News •
×

A new tool called bun-sqlgen generates TypeScript types for Bun.sql queries, offering type safety without the complexity of traditional ORMs. The package validates SQL queries against your actual database schema during development, catching errors before they reach production. It works with both PostgreSQL and SQLite, requiring no Docker containers for setup.

Developers tag queries with names like `sql.GetUser`...`` and receive fully typed results at the call site. The approach eliminates hand-written type definitions and generic boilerplate while maintaining null safety through automatic type inference. Wrong column names or malformed SQL cause build failures rather than runtime crashes.

The tool runs code generation against real database migrations, making your migration files the single source of truth for schema definitions. Install via `bun add @ilbertt/bun-sqlgen` and generate types with a CLI command that outputs to `src/queries.gen.d.ts`.

This solves a genuine pain point for Bun developers who want SQL type safety without abandoning raw queries. The runtime remains 100% Bun-native with zero abstraction overhead, appealing to developers who prefer explicit SQL control over ORM magic.