Reylo: Direct .sql File Imports in TypeScript 7.1 With Full Type Checking Against a Live DatabaseNote · digest for November 25
Direct optical fiber link between a structured SQL query block and a TypeScript type interface
Note3 sections≈ 5 min

Reylo: Direct .sql File Imports in TypeScript 7.1 With Full Type Checking Against a Live Database

Imagine a typical backend query: fetch an author and their post count using LEFT JOIN, COUNT, and GROUP BY. In ORMs like Prisma, Drizzle, or Kysely, you have to translate SQL into a custom DSL and fight with CTEs or window functions. Pick a code generator (sqlc, pgtyped), and you end up managing a file watcher and .d.ts files that drift from your schema. Write raw strings in a driver, and typos only surface at runtime.

Reylo tackles this head-on: you import a plain .sql file directly into TypeScript as an ES module, with no code generation and no watchers. Released under the MIT license in October 2026, it gained 1,683 GitHub stars in its first month.

How contentMappers in TypeScript 7.1 Connect SQL to the Compiler

Previously, tsc couldn't parse non-TS file formats, forcing frameworks to build wrappers like vue-tsc. In TypeScript 7.1 Beta (the Go port typescript-go), PR #4712 introduced contentMappers, bringing Volar's ideas straight into the compiler. Now tsc can request virtual types from an external plugin during type checking.

Reylo hooks into that extension point. You put plain SQL with named parameters right next to your code:

-- get_author.sql
select u.id, u.name, count(p.id)::int as post_count
from users u
left join posts p on p.user_id = u.id
where u.id = :id::int
group by u.id

During type checking, Reylo doesn't execute the query. Instead, it sends PREPARE to your dev database via DATABASE_URL. The database returns exact column and parameter types, and your TypeScript code calls the query like a function:

// main.ts
import postgres from "postgres";
import getAuthor from "./get_author.sql";

const db = postgres(process.env.DATABASE_URL);

// Type: { id: number; name: string; post_count: number } | null
const author = await getAuthor.run(db, { id: 1 });

A typo in a column name or passing a string instead of a number for id immediately becomes a tsc error pointing to the line in .sql. You enable the plugin in tsconfig.json:

// tsconfig.json
{
  "compilerOptions": { /* your options */ },
  "contentMappers": [{ "package": "reylo" }]
}

Type checking requires the --runExternalCode flag, and at runtime Node.js 24+ uses the reylo/register loader:

npm install reylo && npm install -D typescript@next
tsc --noEmit --runExternalCode
node --env-file=.env --import reylo/register main.ts

Beyond Node, plugins exist for Bun, Vite, Next.js (withReylo), esbuild, Rollup, webpack, Cloudflare Workers, and Jest, so bundlers handle .sql imports seamlessly.

What the Engine Infers and Which Databases Work Today

On top of PREPARE, Reylo inspects your schema, JOINs, and WHERE clauses: a column stays nullable unless NOT NULL or a filter rules out NULL. ENUM columns and CHECK (col IN (...)) constraints narrow into union types like "click" | "key". Constructed JSON and jsonb columns with JSON Schema (pg_jsonschema in Postgres or json_schema_valid in MySQL) also preserve exact object shapes.

The engine infers result cardinality too:

SQL query shapeTypeScript return type
Lookup by PRIMARY KEY, UNIQUE, or LIMIT 1Row | null
Aggregate without GROUP BY or single-row INSERT ... RETURNINGRow
INSERT, UPDATE, DELETE without RETURNINGvoid
Any other SELECTRow[]

Besides .run(), each query exposes .runSync(), .try(), .with(), .sql, .raw, and .source, and emits calls on Node's reylo:query diagnostics channel for loggers.

Unlike Prisma, Reylo doesn't lock you into its own client: .run() accepts a pool, connection, or transaction from Drizzle, Knex, or popular drivers. It supports PostgreSQL 14–19 (pg, postgres.js, Neon, PGlite), MySQL 8.0.38+ (mysql2, PlanetScale), SQLite (better-sqlite3, node:sqlite, Turso, Cloudflare D1), and ClickHouse 25.11+ in preview (@clickhouse/client). For AI agents, the Reylo docs provide reylo.dev/llms.txt and an installable skill via npx skills add reylosql/reylo --skill reylo.

Real-World Limitations to Keep in Mind

You need TypeScript 7.1 (typescript@next, November 12, 2026 build or newer for tsconfig.json support) and Node.js 24+—older LTS releases won't run it.

A live database with an up-to-date schema is required every time tsc --noEmit --runExternalCode runs. That means your CI pipeline needs a Postgres/MySQL instance or local SQLite/PGlite with DATABASE_URL (or direct_url in .reylo.yaml).

Bundlers have a few quirks too: TypeScript 7 lacks the legacy JS API for ts-loader and @rollup/plugin-typescript, so webpack needs swc-loader or babel-loader, and Rollup needs @rollup/plugin-sucrase. In Vite, keep .sql imports in server-only code (such as .server/) so queries don't leak into client bundles, while withReylo in Next.js disables built-in type checking so tsc runs separately. The project is still young, and ClickHouse support remains in preview.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 5