Calling queries
Every generated query function takes two arguments: a database handle (created with sqlite(...) or postgres(...)) and an object containing the query's parameters:
const db = sqlite(new DatabaseSync('app.db'))
const book = bookByIsbn(db, { isbn: '978-0-00-000001-7' })Even if a query takes no parameters, still pass an empty object: listBooks(db, {}).
SQLite
Pass any DatabaseSync-compatible instance—such as Node's built-in node:sqlite—to sqlite(raw). SQLite query functions execute synchronously:
import { DatabaseSync } from 'node:sqlite'
import { sqlite } from '@exprtlabs/hyper-sql'
import { addNote } from './src/add-note.sql'
const db = sqlite(new DatabaseSync('app.db'))
const [note] = addNote(db, { body: 'first' }) // INSERT ... RETURNING returns an array of rowsPostgres
Pass a client from your Postgres driver (such as pg.Client) to postgres(client). Postgres query functions are asynchronous and return promises:
import pg from 'pg'
import { postgres } from '@exprtlabs/hyper-sql'
import { flightByNumber } from './src/flights.sql'
const client = new pg.Client()
await client.connect()
const db = postgres(client)
const flight = await flightByNumber(db, { number: 'KX101' })Return types
| Statement annotation | Return type |
|---|---|
None, or -- @many | Row[] |
-- @one | Row | undefined (throws if the query returns more than one row) |
-- @exec, or any write statement without RETURNING | { changes, lastInsertRowid } on SQLite; { changes } on Postgres |
Transactions in Postgres
The Postgres handle exposes a db.transaction(...) helper. It runs BEGIN and passes a scoped transaction handle tx into your callback—use tx for every query inside the block:
const changes = await db.transaction(async (tx) => {
const booked = await addBooking(tx, { id: 1, flight: 'KX101', passenger: 'Ana' })
return booked.changes
})If your callback resolves, hyper-sql sends COMMIT and returns your callback's value. If it throws, hyper-sql sends ROLLBACK and rethrows the original error.
hyper-sql guards against four common transaction bugs by throwing SqlTransactionError with a message explaining how to fix them:
- Calling
dbdirectly instead oftxwhile a transaction is active. - Using
txafter its transaction has already finished. - Nesting a second
transaction()inside an active one. - Swallowing a failed statement's error inside the callback (Postgres aborts the transaction on error and rolls it back at
COMMITanyway—let the error propagate instead).
Transactions in SQLite
Because SQLite queries run synchronously, the SQLite handle doesn't need a transaction wrapper—send BEGIN, COMMIT, and ROLLBACK directly on the underlying driver you passed to sqlite(...):
const raw = new DatabaseSync('app.db')
const db = sqlite(raw)
raw.exec('BEGIN')
try {
addBooking(db, { id: 1, flight: 'KX101', passenger: 'Ana' })
addBooking(db, { id: 2, flight: 'KX101', passenger: 'Ben' })
raw.exec('COMMIT')
} catch (e) {
raw.exec('ROLLBACK')
throw e
}Query files only allow data statements (SELECT, INSERT, UPDATE, DELETE, MERGE), so transaction control always stays on your driver. See examples/host/transaction/ in the repository for a complete Postgres transaction example.