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 |
A page of rows takes about 0.3 ms at 200,000 rows and at 10 million. Counting grows with the table. The four tools below find each slow request and show what brings it back down.
Rows in the table 200,000 1 million 10 million Each request is measured at all three sizes. The bigger the table, the thicker its column.
pagination: { countCap: 10_000 }, defaults: { pagination: { count: "capped" } }, facets: { whenCapped: "skip" }, // pageInfo: { rowCount: 10_000, exact: false }
A capped count brings Page + count to 1.0 ms and Search "ord" + count to 5.4 ms at 10M. whenCapped: "skip" drops the facets once the count passes the cap. 4 facets did not get faster on this run.
Read about countCap →Measured on the repository's benchmark (orders and customers), 2026-11-27, engine 15da95f: PostgreSQL 18.7 on 4 CPUs, one client, median of 10 runs. Times are the engine's own, without HTTP. Benchmark details →
| Request | Out of the box | Tuned | Tuned vs out of the box | ||||
|---|---|---|---|---|---|---|---|
| 200k rows | 1M rows | 10M rows | 200k rows | 1M rows | 10M rows | ||
| Page of rows | 0.29 ms | 0.31 ms | 0.27 ms | not tuned | no change | ||
| Deep cursor page | 0.50 ms | 0.35 ms | 0.36 ms | not tuned | no change | ||
| Page + count | 11 ms | 16 ms | 100 ms | 0.89 ms | 1.0 ms | 1.0 ms | 95× faster |
| Sort by customer.name + count | 25 ms | 67 ms | 752 ms | 1.0 ms | 0.92 ms | 1.0 ms | 733× faster |
| 4 facets | 32 ms | 127 ms | 1,354 ms | 33 ms | 125 ms | 1,405 ms | no change |
| Search "ord" + count | 156 ms | 657 ms | 6,110 ms | 4.1 ms | 4.0 ms | 5.4 ms | 1,121× faster |
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 })); });