buildOrderBy
function buildOrderBy(input): object;Defined in: filters/drizzle/sort.ts:29
Compile a Sort into Drizzle orderBy SQL, allow-listed to columns (a
per-resource map of sortable public field name → column). Unknown fields are
dropped; an empty/all-dropped result falls back to defaultSort. Pair with a
db.select(...).orderBy(...buildOrderBy(...).orderBy) — NOT the RQB query
(RQB aliases the root table and breaks column-referencing SQL).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
input |
{ columns: Record<string, AnyColumn>; defaultSort?: Sort; sort: Sort; } |
input.columns |
Record<string, AnyColumn> |
input.defaultSort? |
Sort |
input.sort |
Sort |
Returns
Section titled “Returns”object
| Name | Type | Defined in |
|---|---|---|
orderBy |
SQL<unknown>[] |
filters/drizzle/sort.ts:33 |
Example
Section titled “Example”import { integer, PgDialect, pgTable, text } from "drizzle-orm/pg-core";import { buildOrderBy } from "@xtandard/lib/filters/drizzle";
const tasks = pgTable("tasks", { name: text("name"), amount: integer("amount") });const { orderBy } = buildOrderBy({ sort: [{ field: "amount", dir: "desc" }, { field: "password", dir: "asc" }], // password: not sortable columns: { name: tasks.name, amount: tasks.amount }, defaultSort: [{ field: "name", dir: "asc" }],});orderBy.map((o) => new PgDialect().sqlToQuery(o).sql); // → ['"tasks"."amount" desc']// in an app: await db.select().from(tasks).orderBy(...orderBy)