NewIntroducing Reylo Migrate — SQL-first schema and migrations

Type-safe SQL tooling for the modern era

Typed queries, schema modeling and migrations, all in plain SQL.

$npm i reylo
Read the docs
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

Works with

PostgresMySQLSQLiteSupabaseNeonPlanetScale
Amazon RDSGoogle Cloud SQLAzure DatabaseCloudflare D1Turso
Inference engine

Import SQL into TypeScript, fully typed

Reylo infers type information from SQL queries, against a live database.

Query
select u.name, p.title
from users u
left join posts p on p.user_id = u.id
where u.id = :user_id::int
Params
{ user_id: number }
Returns
{ name: string; title: string | null }[]

A left join makes the joined table's columns nullable.

01

SQL-first

No lossy DSL and no CRUD-limited ORM. Reylo gives you (and your agents) back the freedom to write complex queries without sacrificing DRY.

02

No hand-written types

Parameters, columns, nullability and cardinality come from the database's own analysis of each query.

How Reylo types a .sql import. TypeScript 7.1 hands the .sql file to Reylo, its content mapper. Reylo has your database (Postgres, MySQL or SQLite) prepare the statement without running it, reads the catalog, and gives TypeScript the types of its parameters and result.TypeScript 7.1tsc and the editorReylocontent mapperyour databasePostgres, MySQL or SQLitea .sql fileits typesprepare, not runcatalog
03

Checked against your database

A misspelled column is a type error at its line and column, and so is a wrong argument to .run().

04

Agent-friendly

A query and its types are one file. An agent edits the SQL, and the types follow, with nothing else to update.

get_author.sql
select u.id, u.name, count(p.id)::int as post_countselect u.id, u.name, u.email, count(p.id)::int as post_countfrom users uleft join posts p on p.user_id = u.idwhere u.id = :id::intgroup by u.id
05

Postgres, MySQL and SQLite

Each in its own dialect, on Neon, Supabase, PlanetScale, Cloudflare D1, Turso and more.

Query
select u.name,
  jsonb_agg(jsonb_build_object('id', p.id, 'title', p.title)) as posts
from users u
join posts p on p.user_id = u.id
where p.published_at > :since
group by u.id
Params
{ since: string }
Returns
{ name: string; posts: { id: number; title: string }[] }[]
06

Your own driver

Runs on pg, postgres.js, Bun.sql, Neon, mysql2, mariadb, PlanetScale, better-sqlite3, node:sqlite, libSQL, Cloudflare D1 and more, or through Drizzle and Knex.

main.ts
import pg from "pg";
import getUser from "./get_user.sql";

const db = new pg.Pool({ connectionString: process.env.DATABASE_URL });

const user = await getUser.run(db, { id: 1 });
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 });
import { SQL } from "bun";
import getUser from "./get_user.sql";

const db = new SQL(process.env.DATABASE_URL);

const user = await getUser.run(db, { id: 1 });
import { neon } from "@neondatabase/serverless";
import getUser from "./get_user.sql";

// a query over HTTP
const db = neon(process.env.DATABASE_URL);
const user = await getUser.run(db, { id: 1 });
import mysql from "mysql2/promise";
import getUser from "./get_user.sql";

const db = mysql.createPool(process.env.DATABASE_URL);

const user = await getUser.run(db, { id: 1 });
import { connect } from "@planetscale/database";
import getUser from "./get_user.sql";

const db = connect({ url: process.env.DATABASE_URL });

const user = await getUser.run(db, { id: 1 });
import Database from "better-sqlite3";
import getUser from "./get_user.sql";

const db = new Database("app.db");

const user = await getUser.run(db, { id: 1 });
import { DatabaseSync } from "node:sqlite";
import getUser from "./get_user.sql";

const db = new DatabaseSync("app.db");

const user = await getUser.run(db, { id: 1 });
import { createClient } from "@libsql/client";
import getUser from "./get_user.sql";

const db = createClient({ url: "file:app.db" });

const user = await getUser.run(db, { id: 1 });
import type { D1Database } from "@cloudflare/workers-types";
import getUser from "./get_user.sql";

export default {
  async fetch(request: Request, env: { DB: D1Database }) {
    const user = await getUser.run(env.DB, { id: 1 });
    return Response.json(user);
  },
};
import { drizzle } from "drizzle-orm/node-postgres";
import getUser from "./get_user.sql";

const db = drizzle(process.env.DATABASE_URL!);

const user = await getUser.run(db, { id: 1 });
import knex from "knex";
import getUser from "./get_user.sql";

const db = knex({ client: "pg", connection: process.env.DATABASE_URL });

const user = await getUser.run(db, { id: 1 });
Migration engine

SQL-first schema and migrations

Model your schema in schema.sql. Generate migrations, or write your own. Works on Postgres, MySQL and SQLite.

01

Use the full power of your database

No lossy or limited DSLs. Declare views, functions, triggers, policies, checks, computed columns, and more.

reylo/schema.sql
create view active_users as
  select id, email from users where deleted_at is null;

create materialized view daily_signups as
  select date_trunc('day', created_at) as day, count(*)
  from users group by 1;
create function slugify(t text) returns text
  language sql immutable as $$
    select lower(regexp_replace(t, '[^a-z0-9]+', '-', 'gi'))
  $$;
create function touch() returns trigger language plpgsql as $$
begin new.updated_at := now(); return new; end $$;

create trigger users_touch before update on users
  for each row execute function touch();
create index users_unverified on users (created_at)
  where verified_at is null;

create unique index users_email on users (lower(email));
alter table documents enable row level security;
create policy documents_own on documents
  using (owner_id = current_setting('app.user_id')::integer);
create table bookings (
  seats integer not null check (seats > 0),
  starts_at timestamptz not null,
  ends_at timestamptz not null,
  check (ends_at > starts_at)
);
create table line_items (
  price numeric not null,
  qty integer not null,
  total numeric generated always as (price * qty) stored
);
create table events (
  payload jsonb not null check (jsonb_matches_schema('{
    "type": "object",
    "properties": { "x": { "type": "integer" } },
    "required": ["x"]
  }', payload))
);
create extension if not exists btree_gist;
create type mood as enum ('sad', 'ok', 'happy');
create domain email as text check (value like '%@%');
create type floatrange as range (subtype = float8);
create table rooms (room int, during tstzrange,
  exclude using gist (room with =, during with &&));
grant select on active_users to reporting;
comment on table users is 'The people who sign in';
02

Fast iteration loops

Push schema.sql to your development database on every save. Then generate a migration once you're done iterating on schema.

03

The most advanced cross-dialect diff engine

Edit schema.sql, then generate a migration with one command. Editable as needed.

reylo/migrations/
-- 20261121090447_tighten_posts.sql
drop view post_titles;
alter table posts add column published boolean default false not null;
alter table posts add constraint title_not_null check (title is not null) not valid;
alter table posts add constraint posts_title_check check (length(title) <= 200) not valid;
create view post_titles as
  select p.id, p.title, p.published, u.name as author
  from posts p join users u on u.id = p.author_id;

-- 20261121090448_tighten_posts_2.sql
alter table posts validate constraint title_not_null;
alter table posts validate constraint posts_title_check;
alter table posts alter column title set not null;
alter table posts drop constraint title_not_null;
-- 20261121090447_tighten_posts.sql
alter table posts
  modify column title text not null,
  add column published tinyint(1) not null default '0',
  add constraint posts_chk_1 check ((length(title) <= 200));

-- 20261121090448_tighten_posts_2.sql
create or replace view post_titles as select p.id as id, p.title as title, p.published as published, u.name as author from (posts p join users u on((u.id = p.author_id)));
-- 20261121090447_tighten_posts.sql
drop view post_titles;
create table new_posts (
  id integer primary key,
  author_id integer not null,
  title text not null check (length(title) <= 200),
  published boolean not null default false,
  body text not null,
  foreign key (author_id) references users (id)
);
create table new_comments (
  id integer primary key,
  post_id integer not null,
  body text not null,
  foreign key (post_id) references new_posts (id)
);
insert into new_posts (id, author_id, title, body) select id, author_id, title, body from posts;
insert into new_comments (id, post_id, body) select id, post_id, body from comments;
drop table comments;
drop table posts;
alter table new_posts rename to posts;
alter table new_comments rename to comments;
create index posts_author on posts (author_id);
create view post_titles as
  select p.id, p.title, p.published, u.name as author
  from posts p join users u on u.id = p.author_id;
04

Explicit disambiguation

When schema changes are ambiguous, Reylo asks you for clarification. Use --drop or --rename to disambiguate.

05

Linear history via reylo.lock

Enforce linear migration history in Git with the reylo.lock lockfile.

06

Soundness guaranteed

Checksums in the lockfile prevent edited or reordered migrations. Untracked schema changes are detected and flagged.

07

Introspect anytime

Introspect the current database schema anytime in readable SQL.

08

Schemaless mode

For a DDL-first experience, just delete schema.sql. You (or your agents) can write raw DDL migrations.

09

Lock-free mode

For faster iteration, delete the lockfile. Unapplied migrations are run in lexicographic order. Reylo automatically prevents any ordering-dependent schema drift.

10

Switch easily

One command introspects your current schema, generates schema.sql, and creates your initial migrations/init.sql.

Compared with other migration tools

Hover a cross to see why.

Migrate
PrismaTypeScript
Drizzle KitTypeScript
FlywayJava
pg-deltaSupabase

Ship faster with Reylo

$npm i reylo
Read the docs