Next.js
Reylo in the Next.js App Router
Your server code imports .sql files, and the build checks their types.
| Config | withReylo from reylo/next, around your Next.js config |
| Bundlers | Turbopack, and webpack with --webpack |
| Queries run in | Server components, route handlers and server actions |
| Edge runtime | A driver that connects over HTTP or WebSockets, such as Neon's |
| Example | nextjs, a Next.js 16.4 app on Postgres with a page, a server action and a route handler |
Setup
Wrap your Next.js config with withReylo, which loads .sql files in server code:
import type { NextConfig } from "next";
import { withReylo } from "reylo/next";
const nextConfig: NextConfig = {
// ...
};
export default withReylo(nextConfig);Add Reylo to tsconfig.json as a content mapper, as in Setup:
{
"compilerOptions": { /* ... */ },
"contentMappers": [{ "package": "reylo" }]
}To type-check without building, run next typegen first, which writes the route types:
{
"scripts": {
"typecheck": "next typegen && tsc --noEmit --runExternalCode"
}
}Server code
Await a query in a server component. Route handlers and server actions run queries the same way:
import { } from "@/lib/db";
import from "./list_posts.sql";
export default async function () {
const posts = await .(); return (
<>
{.(() => (
< ={.}>
{.} by {.}
</>
))}
</>
);
}select p.id, p.title, u.name as author
from p
join u on u.id = p.user_id
order by p.idClient components
A client component can't import a query. Run it in a server component, and pass the rows as props:
import { } from "@/lib/db";
import from "./list_posts.sql";
import { } from "./post-list";
export default async function () {
return < ={await .()} />;
}"use client";
import type from "./list_posts.sql";
type = <<typeof .>>;
export function ({ }: { : }) {
return (
<>
{.(() => (
< ={.}>{.}</>
))}
</>
);
}A type-only import, as in post-list.tsx, is fine. Importing the query itself into a client component fails the build.
Building
The build runs Reylo's type check after the compile. A type error in a query or its caller stops it:
$ next build
✓ Compiled successfully in 1760ms
Running next.config.js provided runAfterProductionCompile ...
app/page.tsx(10,33): error TS2551: Property 'authr' does not exist on type '{ id: number; title: string; author: string; }'. Did you mean 'author'?
⨯ Failed to run runAfterProductionCompile: Failed to type check (tsc --runExternalCode).How the build checks types:
- Next.js's own check runs without
--runExternalCode, sowithReyloturns it off and runs Reylo's instead. The build still prints "Skipping validation of types" for Next.js's check. - Setting
typescript.ignoreBuildErrorsin your config turns Reylo's check off too. - The build needs your dev database, as the type check does. In CI, start it and apply your migrations first. A host that builds before it migrates, such as Vercel, can build against a database of its own.