Reylo

An engine that infers the types of your SQL queries

License: MITnpm versionGitHub stars

Reylo is a type inference engine for SQL. It lets you import SQL queries into your TypeScript files with full type information.

If you have a query like this:

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
View schema
create extension if not exists pg_jsonschema;

create type user_role as enum ('admin', 'member');

create table users (
  id serial primary key,                     -- at most one row per id
  name text not null,                        -- name: string
  email text unique,                         -- email: string | null
  role user_role not null default 'member',  -- role: "admin" | "member"
  tags text[] not null default '{}',         -- tags: (string | null)[]
  created_at timestamptz                     -- created_at: string
    not null default now()
);

create table posts (
  id serial primary key,
  user_id integer not null references users (id),
  title text not null,
  body text,
  published_at timestamptz
);

create table events (
  id serial primary key,
  kind text not null
    check (kind in ('click', 'key')),        -- kind: "click" | "key"
  payload jsonb not null                     -- payload: what the schema admits
    check (jsonb_matches_schema('{
      "type": "object",
      "properties": {
        "x": { "type": "integer" },
        "y": { "type": "integer" },
        "code": { "type": "string" }
      }
    }', payload))
);
create table users (
  id int auto_increment primary key,          -- at most one row per id
  name text not null,                         -- name: string
  email varchar(255) unique,                  -- email: string | null
  role enum('admin', 'member')                -- role: "admin" | "member"
    not null default 'member',
  tags json not null default (json_array()),  -- tags: Json
  created_at datetime                         -- created_at: string, UTC
    not null default (utc_timestamp())
);

create table posts (
  id int auto_increment primary key,
  user_id int not null,
  title text not null,
  body text,
  published_at datetime,
  foreign key (user_id) references users (id)
);

create table events (
  id int auto_increment primary key,
  kind text not null,
  payload json not null                       -- payload: what the schema admits
    check (json_schema_valid('{
      "type": "object",
      "properties": {
        "x": { "type": "integer" },
        "y": { "type": "integer" },
        "code": { "type": "string" }
      }
    }', payload))
);
create table users (
  id integer primary key,               -- at most one row per id
  name text not null,                   -- name: string
  email text unique,                    -- email: string | null
  role text not null default 'member'   -- role: string
    check (role in ('admin', 'member')),
  tags text not null default '[]',      -- tags: string, JSON text
  created_at text                       -- created_at: string, UTC
    not null default (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
) strict;

create table posts (
  id integer primary key,
  user_id integer not null references users (id),
  title text not null,
  body text,
  published_at text
) strict;

create table events (
  id integer primary key,
  kind text not null,
  payload text not null
) strict;

You can import it like this. Hover over author to see the inferred type!

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

const  = (..);
const  = await .(, { : 1 });

?.
  • id
  • name
  • post_count

Reylo relies on the Content Mapper API (introduced in TypeScript 7.1) to let you directly import .sql files into TypeScript.

Reylo infers type information about both the parameters and the return types. Throughout the docs, you will see "inference cards" like this where you can quickly see both a query and its inferred type information.

Query
select id, name, email from users where id = :id::int
Params
{ id: number }
Returns
{ id: number; name: string; email: string | null } | null

Reylo gets type information about a query by sending it to a live dev database and getting back the database's own type descriptors. It then maps these to TypeScript types. Reylo is the perfect option for the agent era:

  • 💪 The full power of SQL — any syntax, function or operator your database supports
  • 🌵 DRY — no redundant hand-written types
  • 🎯 Checked against your database — a misspelled column is a compile error
  • 🤖 Agent-friendly — DRY code is more maintainable for agents
  • 🧩 Postgres, MySQL and SQLite — Neon, PlanetScale, Cloudflare D1, Turso and more
  • 🔌 Your own driver — pg, postgres.js, mysql2, better-sqlite3 and more in TypeScript

Migrations

The reylo command writes a migration from each change you make to schema.sql, and runs the migrations on your database:

How the migration commands move between schema.sql, the migrations and the database. reylo migrate new writes a migration from schema.sql into the migrations folder, whose reylo.lock records their order. reylo migrate dev and reylo migrate deploy run the migrations on the database. Off to the sides, skipping the migrations: reylo migrate init writes schema.sql from an existing database, and reylo db push changes the database straight from schema.sql.schema.sqlmigrations/reylo.lockDatabasereylo migrate newreylo migrate {dev|deploy}reylo migrate initreylo db push

Migrations covers the rest, and every command is on the CLI page.

Released under the MIT License. Copyright © 2026 Colin McDonnell.