How hyper-sql works
hyper-sql brings static TypeScript types to plain SQL. You write your queries in .sql files, and TypeScript type-checks both the SQL itself and every call to it in your code.
The problem
To TypeScript, a .sql file is just text—it can't tell what columns a query returns or what types its parameters expect. Your database already knows, though. hyper-sql asks the database once, saves the answers in a lock file, and hands those types to TypeScript at build time.
A quick example
Say you have a query that fetches a single book by ISBN:
-- @one
SELECT isbn, title, price_cents FROM books WHERE isbn = :isbnYou import the .sql file directly and call it like any other TypeScript function:
import { bookByIsbn } from './src/book-by-isbn.sql'
const book = bookByIsbn(db, { isbn: '978-0-00-000001-7' })
// book: { isbn: string; title: string; price_cents: number } | undefinedBoth the input parameters and the returned row are fully typed—pass the wrong type or misspell a parameter name, and TypeScript flags it immediately. Follow the Quick start to build and run this project from scratch.
How it works
- Run
hyper-sql pull. hyper-sql loads yourschema.sqlinto a scratch database—an in-memory database on SQLite, or a transaction that gets rolled back on Postgres. - Prepare every statement. It asks the database to prepare each query without executing it, so no data is touched.
- Write
sql.lock. The database's column types, parameter types, and nullability info are saved tosql.lock. Commit this lock file alongside your code. - Type-check with
tsc. TypeScript runs hyper-sql's content mapper on each.sqlfile, combining your SQL withsql.lockto produce a typed module. Any mistake in a query becomes a TypeScript error right at that line in the.sqlfile, complete with instructions on how to fix it. - Execute at runtime. Each generated function sends its SQL to your database driver and validates every returned row against the types in
sql.lock.
Only steps 1–3 need a database; builds and CI never do. Whenever you edit a query or update your schema, re-run hyper-sql pull. If you forget, TypeScript reminds you with changed since the last sql pull: run hyper-sql pull.
Key benefits
- Real SQL, zero DSL tax: Write CTEs,
RETURNING,MERGE,ROLLUP, views, and nestedjson_agg/json_group_arrayqueries directly instead of fighting an ORM or query-builder API. - Database-verified types: Types come from SQLite and Postgres themselves, including accurate nullability for outer joins, empty aggregates, scalar subqueries, and views.
- Fast
tscand IDE autocomplete: Generated modules expose flatParamsandRowinterfaces instead of deeply nested conditional types, keeping type-checking snappy as your schema grows. - Fast runtime: No query-builder AST or SQL string assembly at runtime—static queries are pre-numbered strings, dynamic variants are cached once, SQLite caches prepared statements, and both dialects decode rows in array mode straight into fixed-shape objects.
- Type-safe dynamic SQL: Comment blocks (
/*if*/,/*on*///*off*/,/*sort*/) handle optional filters, flags, and sorting while keeping every variant verified by the database. - Compile-time types backed by runtime checks: Every cell is validated against
sql.lock(catching unsafe integers beyond ±2⁵³, unexpectedNULLs, invalid booleans, and JSON drift), and Postgres transactions guard against common mistakes. - Zero dependencies & driver-agnostic: Pass your own
node:sqlite,pg, or compatible client. Works on Node 24+ and compiles natively under HyperTS strict mode.
Requirements
- TypeScript 7.1 (beta) — needed for content mapper support. Run builds with
tsc --runExternalCode. - Node 24 or later — required for
hyper-sql pull(which uses built-innode:sqlite) and the Node.sqlmodule loader hook. - Database — SQLite works out of the box with no server. For Postgres,
hyper-sql pullconnects to the database specified byDATABASE_URL.
Next steps
- Quick start — go from an empty directory to a running, type-checked app in a few minutes.
- Query files and annotations — learn about annotations, parameters, nested JSON rows, and conditional SQL blocks.
- Examples — explore small, runnable projects covering every feature.
- Agent skill — connect the bundled skill to Claude Code, Antigravity, or other coding agents.