Route Handlers
Overview
createRelayerRoute() returns a route factory with 7 handler methods. Each returns a Next.js-compatible route handler function.
const userRoutes = createRelayerRoute(r, 'users', config);list()
Returns records with filtering, sorting, and pagination.
export const GET = userRoutes.list();HTTP: GET /api/users?where={"email":{"contains":"@gmail.com"}}&orderBy={"field":"name","order":"asc"}&limit=10&offset=0
Query params:
| Param | Type | Description |
|---|---|---|
where | JSON string | Filter conditions |
select | JSON string | Field selection |
orderBy | JSON string | Sort configuration |
sort | string | Shorthand: -createdAt,+name |
limit | number | Max records (clamped to maxLimit) |
offset | number | Skip records |
Response:
{ "data": [{ "id": 1, "name": "John" }], "meta": { "total": 42, "limit": 20, "offset": 0 }}findById()
Returns a single record by ID.
export const GET = userRoutes.findById();HTTP: GET /api/users/1
Response:
{ "data": { "id": 1, "name": "John", "email": "john@test.com" } }Returns 404 if not found.
create()
Creates a new record. Request body IS the data (no { data: ... } wrapper).
export const POST = userRoutes.create();HTTP: POST /api/users with body { "name": "John", "email": "john@test.com" }
Response (201):
{ "data": { "id": 3, "name": "John", "email": "john@test.com" } }update()
Updates a record by ID.
export const PATCH = userRoutes.update();HTTP: PATCH /api/users/1 with body { "name": "Updated" }
Response:
{ "data": { "id": 1, "name": "Updated", "email": "john@test.com" } }remove()
Deletes a record by ID.
export const DELETE = userRoutes.remove();HTTP: DELETE /api/users/1
Response:
{ "data": { "id": 1, "name": "John", "email": "john@test.com" } }count()
Returns the count of matching records.
export const GET = userRoutes.count();HTTP: GET /api/users/count?where={"role":"admin"}
Response:
{ "data": { "count": 42 } }aggregate()
Runs aggregate functions with optional groupBy.
export const GET = userRoutes.aggregate();HTTP: GET /api/users/aggregate?groupBy=["status"]&_count=true&_sum={"total":true}
Response:
{ "data": [{ "status": "active", "_count": 10, "_sum_total": 5000 }] }Shorthand vs full control
Shorthand — one line, no hooks:
export const { GET, POST } = userRoutes.handlers();export const { GET, PATCH, DELETE } = userRoutes.detailHandlers();Full control — per-handler hooks:
export const GET = userRoutes.list({ beforeFind: ... });export const POST = userRoutes.create({ beforeCreate: ... });See Hooks for all available hooks.