Drizzle example
Build a route handler that maps the sort, filter, search text, and row range of a server grid onto a Drizzle query.
A server grid sends its spec (sorts, filters, searchText) and a row range (start, end) to your route. The query layer checks the request and runs it with Drizzle. This route serves a users table with a serial id and the text columns name, email, and role.
pnpm dlx shadcn@latest add @gridcn/data-grid-server-query-drizzle
The route
The column map lists the columns a request can name. The route follows the server rows contract. A request that fails the check gets status 400:
import { columnsFromDrizzleTable, createDrizzleGridQuery } from "@/components/data-grid-server-query-drizzle/data-grid-server-query-drizzle";
import { db } from "./db";
import { users } from "./schema";
const columns = columnsFromDrizzleTable(users, {
pick: ["id", "name", "email", "role"],
overrides: { name: { searchable: true }, email: { searchable: true } },
});
export const usersQuery = createDrizzleGridQuery({ db, table: users, columns });import { handleGridRequest } from "@/components/data-grid-server-query/data-grid-server-query";
import { usersQuery } from "./users-query";
export const POST = (req: Request) =>
handleGridRequest(req, usersQuery.fetchRows);About the route:
- The layer escapes the LIKE wildcards of every text value and binds every value as a parameter.
- A filter on an empty value follows the grid:
containswith no text matches every row. notEquals,notContains, andisNoneOfmatch the rows with NULL.emptymatches NULL and the empty string.- A page holds at most 1,000 rows. The primary key ends every sort, so a page never repeats a row.
The client
The grid posts the spec and the range to the route. fetchRows returns the JSON as the answer:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import {
useDataGridServerRows,
} from "@/components/data-grid-server/data-grid-server";
type User = { id: number; name: string };
const columns = defineColumns<User>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
export function UsersGrid() {
const server = useDataGridServerRows<User>({
getRowId: (row) => String(row.id),
fetchRows: async ({ signal, ...request }) => {
const body = JSON.stringify(request);
const init = { method: "POST", body, signal };
const response = await fetch("/api/grid", init);
if (!response.ok) throw new Error("Request failed");
return response.json();
},
});
return <DataGrid {...server.gridProps} columns={columns} />;
}Save edits from onSave. See Handle refused saves.
Shareable state
To restore the sort, the filters, the search text, the page, and the page size from the URL, spread useDataGridUrlPagination() into the hook options. Mount DataGridUrlState in the provider and spread server.providerProps and server.rootProps. The first request carries the restored spec, so the route stays as it is:
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
} from "@/components/data-grid/data-grid";
import {
DataGridUrlState,
useDataGridUrlPagination,
} from "@/components/data-grid-url-state/data-grid-url-state";
const url = useDataGridUrlPagination();
const server = useDataGridServerRows<User>({
getRowId: (row) => String(row.id),
fetchRows, // the function of the client above
...url,
});
return (
<DataGridProvider columns={columns} {...server.providerProps}>
<DataGridUrlState />
<DataGridRoot {...server.rootProps} className="h-96">
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridProvider>
);URL state lists the keys and the adapter setup.
Related
- Server rows example: the grid side of this route
- Server rows contract: the request and the answer
- Prisma example
- Supabase example