Validate
Every key, operator and path is checked against the resource.
Define a resource on a Drizzle table. You get request validation, filters, search, sorting, pagination, facets and totals, with the SQL and the TypeScript types to match. Select a key to see what it adds.
1export const orders = engine.defineResource("orders", {2 views: { list: { select: { reference: true,3 totalAmount: true, customer: { select: { name: true } } } } },4 query: {5 scope: (f, ctx) => f.is("customer.orgId", ctx.orgId),6 search: { allowed: ["reference", "customer.name"] },7 sort: { defaults: [{ key: "createdAt", dir: "desc" }] },8 facets: { allowed: ["status"] },9 pagination: { modes: ["cursor"] },10 },11 summary: (o) => ({12 revenue: sum(o.totalAmount),13 }),14});
{ view: "list", search: { value: "halv" }, sorting: [{ key: "createdAt", dir: "desc" }], facets: [{ key: "status" }], pagination: { mode: "cursor", pageSize: 25 }, summary: true, } // orgId comes from the server, never the client
select "reference", "total_amount", c."name" from "orders" join "customers" c on ... where c."org_id" = $1 and ("reference" ilike $2 or c."name" ilike $2) order by "created_at" desc, "id" desc limit 26 -- one aggregate statement select "status", count(*), sum("total_amount") group by grouping sets (("status"), ())
type Row = { reference: string; totalAmount: number; customer: { name: string } } type Response = { rows: Row[]; pageInfo: { nextCursor: string | null }; facets: { status: { value: string; count: number }[] }; summary: { revenue: string | null }; } type SortKey = "createdAt" | "reference" | … type SearchField = "reference" | "customer.name"
| reference | customer | total |
|---|---|---|
| ORD-29999 | Halvorsen Freight | $4,128 |
| ORD-29981 | Halvorsen Freight | $790 |
| ORD-29944 | Halvorsen Freight | $2,310 |
scopeThe tenant rule wraps every request on the server. The client can't send it or remove it.
Define a resource once on a Drizzle table. Each of these works on the server from that definition, with no endpoint code of your own.
Nested AND / OR groups, with the operators that fit each field's type.
One search box across the fields and relations you allow.
Several sort keys at once. Turn sorting off for any field.
Offset or cursor pages, with an exact count or none.
Counts per value for filter sidebars, each ignoring its own filter.
Your own aggregates over every matching row, typed from the expression.
A condition from the server context on every statement. Clients can't lift it.
Hide a field, or stop it from being filtered, searched or sorted.
Several response shapes from one resource: a list, an export, a client view.
Filter, sort and search through relations with a dotted path.
Field paths, operators and rows are typed from your Drizzle schema.
A Zod schema for the request body, generated from the resource.
Every list request runs the same six stages on your server, in this order. Pick a request and follow it through.
search "halv" · status is shippedrows · count · facetsEvery key, operator and path is checked against the resource.
The tenant condition comes from your server, never from the client.
One count over every match, for the pager or your own totals.
Filters, search and sort pick only this page's ids.
The rows load by id with their relations, in your view's shape.
Bucket counts per value, each ignoring its own filter.
2,412 sample orders, in your browser. Search, filter, sort and page: the line of the resource that allows each action lights up. Open the full playground →
1export const orders = engine.defineResource("orders", {2 relations: { customer: true,3 orderLines: { with: { product: true } } },4 query: {5 scope: (f, ctx) => f.is("customer.orgId", ctx.orgId),6 search: { allowed: ["reference", "customer.name"] },7 sort: {8 defaults: [{ key: "createdAt", dir: "desc" }],9 disabled: ["orderLines.product.name"],10 },11 facets: {12 allowed: ["status", "orderLines.product.category"],13 },14 pagination: { modes: ["cursor", "offset"] },15 },16 summary: (o) => ({17 rows: count(), revenue: sum(o.totalAmount),18 }),19});
| ORD-29999 | Halvorsen Freight | Halden 14 | $4,128 | 2026-11-17 |
| ORD-29995 | Nordvik Energy | Cirrus Headphones | $577 | 2026-11-16 |
| ORD-29991 | Quarry Lane Studio | Halden 14 | $1,635 | 2026-11-16 |
| ORD-29987 | Brightwater Labs | Cumulus SSD 2TB | $2,697 | 2026-11-16 |
| ORD-29983 | Brightwater Labs | Stratus Router | $631 | 2026-11-16 |
| ORD-29979 | Pell Street Bank | Cumulus SSD 2TB | $2,501 | 2026-11-16 |
| ORD-29975 | Pell Street Bank | Fjord Pro 16 | $308 | 2026-11-16 |
| ORD-29971 | Lindqvist Dental | Stratus Router | $236 | 2026-11-16 |
Median timings from the repository's PostgreSQL benchmark: 200,000 rows, PostgreSQL 18, one client. Bright pixels are today's time; dim pixels show what version 2.4.1 took.
Source: benchmark results, run of November 18, 2026. Single-client engine timings, not HTTP or production latency.
Install the package, create the engine once, define a resource per table, and validate incoming requests with the schema it generates.
bun add drizzle-resource
import { createQueryEngine } from "drizzle-resource"; export const engine = createQueryEngine({ db, schema, relations }) .withContext<{ orgId: string }>();
export const orders = engine.defineResource("orders", { relations: { customer: true }, query: { scope: (f, ctx) => f.is("customer.orgId", ctx.orgId), search: { allowed: ["reference", "customer.name"] }, facets: { allowed: ["status"] }, }, });
const Request = requestSchema(orders); // zod, valibot or vine app.post("/orders/query", async (c) => { const request = Request.parse(await c.req.json()); return c.json(await orders.query({ request, context })); });