Import SQL into TypeScript, fully typed

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

$npm add 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

What Reylo infers

Parameters, columns, nullability, cardinality and nested JSON shapes, from your database's own analysis of each query.

Query
update users
set name = :name
where id = :id
Params
{ name: string; id: number }
Returns
void
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 }[]
Query
select u.name,
  coalesce(u.email, '') as email,
  exists (select 1 from posts p where p.user_id = u.id) as has_posts
from users u
where u.role = :role
Params
{ role: "admin" | "member" }
Returns
{ name: string; email: string; has_posts: boolean }[]
Query
select id, title
from posts
where user_id = coalesce(:user_id::nullable(int), user_id)
Params
{ user_id?: number | null }
Returns
{ id: number; title: string }[]
Query
select id, name
from users
where email = :email::text
Params
{ email: string }
Returns
{ id: number; name: string } | null
Query
select count(*)::int as posts
from posts
where user_id = :user_id::int
Params
{ user_id: number }
Returns
{ posts: number }
Query
select id, title,
  row_number() over (
    partition by user_id
    order by published_at
  ) as n,
  lag(title) over (order by id) as prev
from posts
Params
void
Returns
{ id: number; title: string; n: bigint; prev: string | null }[]
Query
select u.name, p.title, p.published_at
from users u
left join lateral (
  select title, published_at
  from posts
  where user_id = u.id
  order by published_at desc nulls last
  limit 1
) p on true
Params
void
Returns
{
  name: string;
  title: string | null;
  published_at: string | null;
}[]
Query
insert into users (name)
values (:name::text)
returning id
Params
{ name: string }
Returns
{ id: number }
Query
select id, kind
from events
order by id desc
limit :limit::int
Params
{ limit: number }
Returns
{ id: number; kind: "click" | "key" }[]
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 }[] }[]
Query
select id, payload
from events
where id = :id::int
Params
{ id: number }
Returns
{ id: number; payload: { x: number; y: number } } | null
Query
select id, payload
from events
where id = :id::int
Params
{ id: number }
Returns
{
  id: number;
  payload:
    | { kind: "click"; x: number; y: number }
    | { kind: "key"; code: string };
} | null

Why Reylo

The full power of SQL

Any syntax, function or operator your database supports, from window functions to JSON aggregation and returning clauses.

No hand-written types

Parameters, columns, nullability and cardinality come from the database's own analysis of each query. There is nothing to declare by hand.

Checked against your database

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

Agent-friendly

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

Postgres, MySQL and SQLite

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

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.

Ship faster with Reylo

$npm add reylo
Read the docs