trisql (trάɪsɪkl)
2-way SQL templates as typed TypeScript queries. 日本語
Write a .sql file that is valid, runnable SQL with sample values. Import it. Its parameters and rows are typed against your schema, with no code generation.
The name and the design are inspired by bisql, a 2-way SQL library for Clojure.
-- sql/get-user.sql
SELECT id, email, name
FROM users
WHERE id = /*$id*/1import getUser from "./sql/get-user.sql";
const user = await getUser.maybeOne(db, { id: "1" });
// { id: string; email: string; name: string | null } | null- Templates stay executable SQL (the 2-way SQL style of bisql).
- Types come from a TypeScript 7.1 content mapper that describes each statement against an embedded PGlite loaded with your DDL. No database server has to be running.
- Bring your own driver: anything with
query(text, values)returning{ rows }, such aspg, PGlite or Neon. - PostgreSQL only for now.
Status: experimental. See docs/2way-sql.md for the template specification and docs/differences-from-bisql.md for how it differs from bisql.
Requirements: Node 24+, TypeScript 7.1 (beta or nightly), a .sql schema file (migrations or pg_dump --schema-only).
npm install trisql pg
npm install -D typescript@beta @electric-sql/pglite
npm install -D libpg-query # optional: exact nullability for columns behind outer joins// tsconfig.json
{
"compilerOptions": { /* ... */ },
"contentMappers": [{ "package": "trisql", "extensions": [".sql"] }]
}tsc --noEmit --runExternalCode # type check
node --import trisql/register main.ts # run (plain Node)
npx trisql generate # write CRUD templates into sql/<schema>/<table>/crud/With a bundler, add the plugin instead of the Node preload: trisql/esbuild (also for Bun.build) or trisql/vite.
trisql/experimental exposes the analysis and generator internals (loadConfig, openDescriber, analyze, introspect, generateCrud, writeCrud, ...) for tools such as scaffolders and code generators. It is outside semver and may change in any release; pin the version if you depend on it.
npm run check # lint, typecheck, tests