How to import .sql files in TypeScript with inferred types? Reylo, from the creator of Zod, does it without codegen ✅ yoDEV Tested

Reylo lets you import a .sql file directly into TypeScript and get inferred row and parameter types from your actual database, with no codegen step or generated files. It was created by Colin McDonnell, the author of Zod, and we tested version 0.6.0 against SQLite and Postgres: it delivers on its promise, with two limitations worth knowing before adopting it.

readme-demo

:white_check_mark: yoDEV Tested · October 30, 2026
We tested Reylo 0.6.0 with TypeScript 7.1.0-beta and Node 24 against SQLite (driver node:sqlite) and PostgreSQL 16 (postgres.js) on Linux. The results we report as ours come from that test.

First the requirements, so nobody wastes an afternoon: at the time of publishing this note (October 30, 2026), Reylo needs TypeScript 7.1, which Microsoft released as beta on October 20, plus Node 24 or higher and a development database running. If your project is still on TypeScript 5.x or 6.x, you’ll need to migrate to TypeScript 7 first, which we’ve already covered in TypeScript 7.0 is now in GA: install it today and compile up to 12x faster. In the meantime, Reylo is for testing in a separate project and adopting once 7.1 becomes stable.

What is Reylo and how does it type SQL without codegen?

Reylo is a content mapper for TypeScript: a type of plugin that arrived with TypeScript 7.1 and allows tsc to understand files that aren’t TypeScript. The TypeScript team itself designed this mechanism for .vue files, Svelte, Astro, and MDX; Reylo uses it for .sql.

In practice, when tsc finds import getUser from "./get_user.sql", it passes the file to Reylo, which analyzes the query against your development database and returns its types. There’s no watcher, no *.queries.ts file next to each file, and no “regenerate” step you can forget. Reylo keeps a cache of its analysis in the .reylo/ folder, which it itself excludes from git.

You write normal SQL, with named parameters:

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

And call it with the client of your own driver:

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 });

user arrives typed. .run() is not the only method: imported queries also expose .runSync(), .try(), .with(), .sql, .raw, and .source.

How do I install Reylo in a TypeScript project?

These are the steps from the official documentation (reylo.dev), the same ones we used in the test.

1. Install the package, your database engine, and the TypeScript beta:

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

At the time of publishing, typescript@beta installs 7.1.0-beta; when 7.1 becomes stable, that line will change. In production the engine needs to be a runtime dependency, so install it without -D.

2. Register Reylo as a content mapper in tsconfig.json:

{
  "compilerOptions": { /* ... */ },
  "contentMappers": [
    { "package": "reylo", "extensions": [".sql", ".reylo.yaml", ".reylo.yml"] }
  ]
}

3. Add the type-checking script. The --runExternalCode option is what allows tsc to run content mappers:

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

That option means exactly what it says: type checking runs third-party code. In the editor, content mappers only run in workspaces you’ve marked as trusted.

4. Point to your development database in .env. The URL scheme chooses the dialect:

DATABASE_URL=postgres://localhost:5432/app
DATABASE_URL=mysql://root@localhost:3306/app
DATABASE_URL=file:./app.db

The documentation requires a direct connection, not a pooler like PgBouncer.

5. Load it at runtime. On Node:

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

There are equivalents for Bun (preload = ["reylo/register"] in bunfig.toml), Vite (reylo/vite), Next.js (withReylo from reylo/next), esbuild, webpack/Rspack, Jest, and Cloudflare. With Vite, import .sql files only from server code, or they’ll end up in your browser bundle.

6. In VS Code or Cursor, install the TypeScript 7 extension (TypeScriptTeam.native-preview) and point it to your project’s TypeScript in .vscode/settings.json:

{
  "js/ts.experimental.useTsgo": true,
  "js/ts.tsdk.path": "./node_modules/typescript"
}

What types does Reylo infer? What we saw in our test

We ran Reylo 0.6.0 with TypeScript 7.1.0-beta on Node 24, against a SQLite file (driver node:sqlite) and Postgres 16 (postgres.js). These are the types Reylo reported (in tsc, and in its playground for the column search with unique):

Query Engine Inferred type
Search by primary key SQLite / Postgres Row | null
Search by unique column Postgres Row | null
Join SQLite Row[]
select count(*) as total SQLite { total: number }, one row, not an array
Nullable column (bio text) both string | null
Column from a left join Postgres string | null
Enum type column Postgres "admin" | "dev" | "viewer"
check (plan in ('free','pro')) Postgres "free" | "pro"
check (role in (...)) SQLite string, no union
json_build_object('name', …, 'posts', …) Postgres { name: string; posts: number }

Parameters are also typed: passing { id: "one" } where the column is an integer is a TypeScript error, and omitting the parameter also is. Full project type checking took about two seconds, and at runtime both engines returned the expected rows.

What happens if the SQL has an error?

The error appears in tsc pointing to the .sql file, with line and column, like any other type error:

get_user.sql(1,18): error reylo6: no such column: emial
bad.sql(1,1): error reylo6: no such table: usuarios

That’s the real win over writing row types by hand: if someone renames a column or drops a table, the build fails at the query that uses it, not in production.

Reylo or pgtyped? Which is better for typing SQL queries in TypeScript?

pgtyped is the best-known tool in this space and solves the same problem with code generation. Both read types from a live database; the difference is how those types get into your code.

Reylo pgtyped
How types arrive tsc infers them when importing the .sql (content mapper) Generates a .queries.ts file for each SQL file
Generated files None (analysis cache in .reylo/, not versioned) Yes, and you import them from your code
Watch mode Not needed npx pgtyped -w -c config.json
Databases Postgres, MySQL, SQLite Postgres
TypeScript required 7.1 (beta at time of publishing) Works with your current TypeScript
Maturity 0.6.0, three weeks old Established project

If you’re on Postgres and still can’t move to TypeScript 7.1, pgtyped remains the stable option. If you want to eliminate the generation step, need MySQL or SQLite, or are starting a new project on TypeScript 7, Reylo is what to test.

What if you already use Drizzle, Prisma, or Kysely?

It’s a different philosophy. With an ORM or query builder you write queries, or the schema, in TypeScript, and the tool produces SQL. Reylo goes the other way: SQL is the source of truth, written as your database would execute it, and TypeScript only receives the types. If your team prefers to read and review SQL in pull requests, that’s the case for Reylo; if you’d rather not leave TypeScript, your ORM still makes sense.

Does Reylo work with MySQL and SQLite?

Yes. The engines are Postgres 14 through 18 (including Neon and PGlite), MySQL 8.0.22 or higher (including PlanetScale), and SQLite (including Cloudflare D1 and Durable Objects, Turso, and libSQL). Supported drivers include pg, postgres.js, Bun.sql, mysql2, mariadb, better-sqlite3, node:sqlite, and libSQL, among others.

SQLite infers less. In our test, a column with check (role in (...)) arrived as string, not as a union, and that matches the documentation: examples of unions from enums and check constraints are Postgres-only. SQLite also types NUMERIC, DATE, and DATETIME columns as number | string, and functions your application registers cause type checking to fail.

Can you use Reylo in CI without a database?

Not yet. Reylo reads types from a live database, so type checking or building needs one to reach. The approach it documents for CI is: bring up the database, apply migrations, then run tsc --noEmit --runExternalCode with DATABASE_URL defined.

We tested how far the cache goes. With Postgres down and the .reylo/ cache already full, type checking passed for already-analyzed queries. A new query failed with reylo5: Cannot connect to the database. Since the cache isn’t versioned, a clean CI runner always needs the database.

Two extras: a skill for agents and a playground

Reylo comes with a skill for code agents:

npx skills add colinhacks/reylo --skill using-reylo

The CLI also has a command that, as of October 30, 2026, doesn’t appear on the main documentation page: npx reylo play brings up a local playground with an HTTP endpoint. POST /api/analyze with {"sql": "..."} returns parameters, return type, cardinality, and query errors in JSON, without executing it. In our test, a search by email returned cardinality: "maybeOne", and a misspelled column returned the Postgres error with line and column. It’s a cheap way for an agent to review its own SQL before writing it to disk.

Should you adopt Reylo now?

Reylo is at 0.6.0 and published six minor versions between October 22 and 27, so expect the API to change. Our recommendation:

  • New project on TypeScript 7 with a development database on hand: test it now. It’s the most direct way we’ve seen to keep SQL as SQL and still have types.
  • Existing project on TypeScript 5.x or 6.x: wait for 7.1 stable. Migration to 7 is separate work; if you want to get a head start, in our test of tsrs, the TypeScript 7 port to Rust we also talked about 7.1.
  • CI without a database: not possible today; plan a database in your pipeline.

What about you? What’s the SQL error that’s cost you the most to discover in production?

How do you type your SQL queries in TypeScript today?
  • With an ORM or query builder (Drizzle, Prisma, Kysely)
  • With code generation (pgtyped, sqlc, or another)
  • With hand-written types
  • I prefer another option (tell us which)
0 votantes

Comment below or, if you got this article by email, reply directly to the email: your response gets published here.

Related: TypeScript 7.0 is now in GA: install it today and compile up to 12x faster