Skip to content

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:

sql
-- @one
SELECT isbn, title, price_cents FROM books WHERE isbn = :isbn

You import the .sql file directly and call it like any other TypeScript function:

ts
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 } | undefined

Both 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 ​

How hyper-sql works: Pull, Build, and RunThree-stage pipeline: hyper-sql pull inspects schema.sql and queries against a scratch database to write sql.lock; tsc runs the content mapper offline using .sql files and sql.lock to produce typed modules; your app executes cached prepared statements with runtime row validation.1Pullhyper-sql pullschema.sqlsrc/*.sqlScratch SQLite or PostgresPrepares queries without running themsql.lock (committed)Only step that touches a database2Buildtscsrc/*.sqlsql.lockTS 7.1 Content MapperChecks SQL + emits flat TS typesTyped .sql modulesOffline · fast tsc · CI-ready3RunNode / HyperTSmain.tsyour driverPrepared SQL + Array DecodeValidates every cell against sql.lockTyped, verified rowsZero query-builder overhead
Pull inspects your schema and queries once; builds stay completely offline; runtime executes cached SQL with per-cell checks.
  1. Run hyper-sql pull. hyper-sql loads your schema.sql into a scratch database—an in-memory database on SQLite, or a transaction that gets rolled back on Postgres.
  2. Prepare every statement. It asks the database to prepare each query without executing it, so no data is touched.
  3. Write sql.lock. The database's column types, parameter types, and nullability info are saved to sql.lock. Commit this lock file alongside your code.
  4. Type-check with tsc. TypeScript runs hyper-sql's content mapper on each .sql file, combining your SQL with sql.lock to produce a typed module. Any mistake in a query becomes a TypeScript error right at that line in the .sql file, complete with instructions on how to fix it.
  5. 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 nested json_agg / json_group_array queries 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 tsc and IDE autocomplete: Generated modules expose flat Params and Row interfaces 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⁵³, unexpected NULLs, 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-in node:sqlite) and the Node .sql module loader hook.
  • Database — SQLite works out of the box with no server. For Postgres, hyper-sql pull connects to the database specified by DATABASE_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.