Mad World #Save_GAZA: Free Palestine  

Search

Reylo: Import SQL Files Into TypeScript With Full Type Inference

Reylo: Import SQL Files Into TypeScript With Full Type Inference

Reylo imports .sql files as typed modules instead, preparing each statement against your development database and inferring row and parameter types through TypeScript 7.1 content mappers—no generated query files, no schema to keep in sync. Version 0.5.0 moved build-time code and database drivers out of your project and into the engine packages.

Reylo requires TypeScript 7.1, Node 24 or later, and a running development database. Type checking prepares statements without executing them, so it establishes the inferred types against your development schema, not deployment database behavior.

Check the database and runtime prerequisites

Set DATABASE_URL in your environment or .env with the scheme that matches your database: postgres://localhost:5432/app, mysql://…, or file:./app.db for SQLite. Place comments on their own line; one open issue reports that inline comments in .env files are appended to the URL value.

On Node, Reylo loads through a runtime import:

Terminal
node --env-file=.env --import reylo/register main.ts

Bun, Vite, Next.js, esbuild, webpack, Rspack, Cloudflare Workers, and Jest need a setup plugin. The documentation provides an example project for each runtime.

Install Reylo and configure type checking

Install Reylo, the engine package for your database, and TypeScript 7.1:

Terminal
npm install reylo
npm install -D @reylosql/postgres  # or @reylosql/mysql, @reylosql/sqlite
npm install -D typescript@beta

Add .sql files and the configuration file to tsconfig.json:

JSON
{
  "include": ["src/**/*.ts", "src/**/*.sql", ".reylo.y*ml"]
}

Run the type checker with --runExternalCode, which lets tsc execute content mappers:

JSON
{
  "scripts": {
    "typecheck": "tsc --noEmit --runExternalCode"
  }
}

Import and execute a SQL query

Write a query in a .sql file. Name parameters with a colon and give each one a cast where the query doesn't pin the type down:

Code
-- get_user.sql
select id, name, email from users where id = :id::int

Import it and call .run() with your database client and a parameters object:

Nginx
import postgres from "postgres";
import getUser from "./get_user.sql";

const db = postgres(process.env.DATABASE_URL);
const user = await getUser.run(db, { id: 1 });
// { id: number; name: string; email: string | null } | null

Pass .run() a pool, client, connection, or transaction from any supported driver. Postgres drivers include pg, postgres.js, Bun.sql, PGlite, Neon, pg-promise, slonik, and @vercel/postgres. MySQL drivers are mysql2, mariadb, PlanetScale, and Bun.sql. SQLite clients include better-sqlite3, node:sqlite, Bun.sql, libSQL, Turso, Cloudflare D1, Durable Objects, sqlite3, and sqlite. A Drizzle database over any of those drivers also works.

Version 0.5.0 changed parameter rules in two ways. A cast to nullable(type) makes a parameter that type or null; coalesce no longer marks one nullable. Reylo refuses missing or null values for every other parameter before the query runs.

Understand the inferred query types

Reylo prepares each statement against your development database without running it, then reads the type of every column and parameter. Four inference rules layer on top of the database's own type information.

Nullability narrows when the schema, a join type, or a where clause rules out null. A not null column stays non-null unless it sits on the nullable side of an outer join.

Cardinality inference recognizes when a query returns exactly one row or at most one row. A match on a primary or unique key, or a query with limit 1, resolves to Row | null. An aggregate without group by resolves to Row. An insert, update, or delete without returning resolves to void. Everything else resolves to Row[].

Union types appear for enums and columns whose check constraint enumerates allowed values. The result type becomes a union of those string or number literals.

JSON structures built with json_build_object, jsonb_build_object, or to_json preserve their shape in the inferred type. A JSON column with a JSON Schema check constraint keeps the structure the schema describes.

Know what the types establish

The inferred types come from preparing statements against your development database, not from running them. Type checking shows that a query compiles on that schema and maps its parameter and result types through the development database's own type system. It does not verify runtime behavior or validate that results match the inferred shape when the query runs on the target database.

Execution returns a promise. The driver rejects it with its own error when the statement fails, the connection drops, or the client is refused.

FAQ

Do I need the database driver in my project for type checking?

No. Version 0.5.0 moved the type-check drivers into the engine packages, which are development dependencies. Your runtime still needs the driver you pass to .run(), but pg, postgres, mysql2, and their equivalents are no longer peers of reylo for the type check.

Can I write queries by hand instead of in a file?

Yes. Import defineQuery from reylo and pass it the SQL text, parameter names, and dialect. A query written by hand has no JSON Schema, so the inputSchema and outputSchema properties throw. If the query has a structured parameter or a typed cast, pass checks (exported from reylo); an error will say so if it's missing.

Write your first query and check the inferred types

Create a .sql file in your project, import it from a .ts file, and run npm run typecheck. The type checker connects to your development database and shows the inferred row and parameter types in your editor. When a column name changes or a parameter cast is wrong, you'll see a type error before the query runs.

Verification status: Supported by official documentation only under the conditions below; not reproduced in a test environment by IGPROD. Checked 2026-10-26.

Applies to: TypeScript 7.1.

Conditions:

  • A running development database is required for type checking; Reylo prepares the query without running it and reads column and parameter types from that database.
  • The documented Node setup uses the runtime registration import; other listed runtimes require their setup plugin or example project.

Sources:

Fatima Ezzahra

Fatima Ezzahra

Fatima Ezzahra covers AI tools, open-source projects and technology news for IGPROD.

Leave a comment

Your email address will not be published. Required fields are marked *

Your experience on this site will be improved by allowing cookies Cookie Policy