Skip to content

Quick start ​

This guide takes you from an empty directory to a working, type-checked app using SQLite. Make sure you're running Node 24 or later.

1. Set up the project ​

sh
mkdir books && cd books
npm init -y
npm install @exprtlabs/hyper-sql
npm install --save-dev typescript@~7.1.0-beta @types/node

Add "type": "module" to your package.json.

2. Configure tsconfig.json ​

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "strict": true,
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "types": ["node"]
  },
  "contentMappers": [
    {
      "package": "@exprtlabs/hyper-sql",
      "extensions": [".sql"],
      "options": { "dialect": "sqlite", "schema": "schema.sql" }
    }
  ],
  "include": ["main.ts", "src"]
}

The contentMappers entry tells TypeScript to hand .sql files to hyper-sql's mapper. Set dialect to "sqlite" or "postgres", point schema at your DDL file, and make sure include covers your .sql files.

3. Add a schema and two queries ​

Create schema.sql to define your tables:

sql
CREATE TABLE books (
  isbn TEXT PRIMARY KEY,
  title TEXT NOT NULL,
  price_cents INTEGER NOT NULL
) STRICT;

Next, create your first query file at src/book-by-isbn.sql:

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

The filename determines the exported function name, so book-by-isbn.sql exports bookByIsbn. The -- @one annotation tells hyper-sql to return a single row, or undefined if nothing matches.

Now add a second query at src/add-book.sql to insert a book:

sql
-- @exec
INSERT INTO books (isbn, title, price_cents) VALUES (:isbn, :title, :priceCents)

-- @exec runs the statement and returns { changes, lastInsertRowid } (the number of affected rows and the last inserted row ID).

4. Write the application ​

Create main.ts. It opens an in-memory SQLite database, loads schema.sql, inserts a book, and looks up two ISBNs:

ts
import { readFileSync } from 'node:fs'
import { DatabaseSync } from 'node:sqlite'
import { sqlite } from '@exprtlabs/hyper-sql'
import { addBook } from './src/add-book.sql'
import { bookByIsbn } from './src/book-by-isbn.sql'

const raw = new DatabaseSync(':memory:')
raw.exec(readFileSync(new URL('./schema.sql', import.meta.url), 'utf8'))
const db = sqlite(raw)
addBook(db, { isbn: '978-0-00-000001-7', title: 'The Salt Road', priceCents: 1450 })

console.log(bookByIsbn(db, { isbn: '978-0-00-000001-7' }))
console.log(bookByIsbn(db, { isbn: 'none' }))

5. Run hyper-sql pull ​

sh
npx hyper-sql pull
text
sql.lock written: 1 tables, 2 statements

hyper-sql pull asks SQLite for every column and parameter type and writes them to sql.lock (see The lock file). Commit sql.lock to version control, and re-run hyper-sql pull whenever you change a query or edit your schema.

6. Type-check the project ​

sh
npx tsc --runExternalCode

No output means everything type-checked cleanly.

7. Run it ​

sh
node --import @exprtlabs/hyper-sql/register main.ts
text
{
  isbn: '978-0-00-000001-7',
  title: 'The Salt Road',
  price_cents: 1450
}
undefined

Node 24 strips TypeScript types natively when executing .ts files, while --import @exprtlabs/hyper-sql/register hooks into Node's module loader to compile .sql imports on the fly.

8. Test a type error ​

To see the type checker in action, change 'none' on line 13 of main.ts to 5, then run npx tsc --runExternalCode again:

text
main.ts(13,30): error TS2322: Type 'number' is not assignable to type 'string'.

Because books.isbn is TEXT, isbn must be a string—and TypeScript catches the mistake before your code ever runs.

Next steps ​