rescript-typesqlis a long-term fork oftypesqlthat is being steered toward a ReScript-first workflow.
- Generates typed query helpers from raw SQL.
- Supports direct ReScript generation from SQL strings.
- Supports embedded-query workflows for ReScript projects.
- Keeps SQL as the source of truth instead of pushing you into an ORM DSL.
- Infers parameter types, result types, nullability, nested result structure, and dynamic query metadata.
- PostgreSQL via
pg - MySQL via
mysql2 - SQLite via
better-sqlite3 - SQLite-compatible libSQL via
libsql - SQLite-compatible D1 via
d1 - SQLite via
bun:sqlite
Install it as a project dependency and run the CLI through npx:
npm install --save-dev rescript-typesqlThe package installs the typesql binary:
npx typesql --helpIf you are using the embedded ReScript flow, you will usually also want:
npm install --save-dev rescript rescript-embed-langCreate a typesql.json file:
{
"databaseUri": "./mydb.db",
"sqlDir": "./sql",
"client": "better-sqlite3",
"includeCrudTables": [],
"rescript": {
"srcDir": "./src",
"outDir": "./src/__generated__"
}
}Configuration notes:
clientcan bepg,mysql2,better-sqlite3,libsql,bun:sqlite, ord1.authTokenis used only forlibsql.schemasis optional for Postgres and defaults to["public"].databaseUriandauthTokensupport${ENV_VAR}substitution.- Use
--env-file .envto load env vars before config resolution.
Add some SQL:
SELECT
id,
name
FROM users
WHERE id = :idThen either generate TypeScript files beside your SQL:
npx typesql compile --config ./typesql.jsonOr generate ReScript for a single query directly:
npx typesql rescript generate --config ./typesql.json --name selectUser --sql "select id, name from users where id = :id"The ReScript command family is the part of the project that this fork is optimizing for.
Core commands:
typesql rescript generategenerates ReScript for a single SQL string.typesql rescript checkreturns the expected variable shape for a query.typesql rescript inspectresolves executable SQL and bind values.typesql rescript explainrunsEXPLAINorEXPLAIN ANALYZEfor a resolved query.typesql rescript execexecutes a query with explicit variables.typesql rescript syncextracts%generated.typesqlembeds and writes__typesql.resfiles.typesql rescript watchkeeps generated embed files in sync while you edit.typesql rescript daemonstarts a long-lived process for editor or tooling integrations.
Generate from stdin:
echo "select id, name from users where id = :id" | npx typesql rescript generate --config ./typesql.json --name selectUserInspect resolved SQL:
npx typesql rescript inspect --config ./typesql.json --name selectUser --sql "select id, name from users where id = :id" --vars '{"id":1}'Sync embedded queries:
npx typesql rescript sync --config ./typesql.jsonWatch embedded queries:
npx typesql rescript watch --config ./typesql.jsonThe legacy compile flow remains available and still generates .ts files from .sql files:
npx typesql compile --config ./typesql.json
npx typesql compile --watch --config ./typesql.jsonThis is still useful for compatibility and as an intermediate representation, but it is not the long-term product focus of this fork.
- Raw SQL stays the source of truth.
- Nullability is inferred from query structure when possible.
- Unique-key and
LIMIT 1cases can narrow result cardinality. - Dynamic query support includes typed
ORDER BYand list expansion. - SQLite ReScript generation caches static prepared statements per database instance.
- This fork is intended to diverge from upstream.
- ReScript support is the strategic focus.
- TypeScript support can change if that is what the ReScript path needs.
- Expect active iteration rather than strict compatibility guarantees.
Development notes and local CI setup live in DEVELOPMENT.md.