Resources

Views

Give each audience an exact response shape, with an inferred row type.

A view lists the exact fields a response includes. Each view gets its own inferred row type, and fields outside it are never selected.

Pick an audience below: fields outside its view drop out of the response.

Response roworders.$infer.views.list · 7 fields
{id: "ord_8f2c",reference: "ORD-29812",status: "shipped",totalAmount: 4820,createdAt: "2026-11-17",customer: {id: "cus_31",name: "Halvorsen Freight",displayName: "HALVORSEN FREIGHT",virtualorgId: "acme",hidden},}
never selected, in any view: deletedAt (private)

Declare views

import { 
count
,
sql
,
sum
} from "drizzle-orm";
import {
createQueryEngine
,
virtual
} from "drizzle-resource";
import {
db
} from "./db";
import {
schema
} from "./schema";
import {
relations
} from "./relations";
const
engine
=
createQueryEngine
({
db
,
schema
,
relations
,
models
: {
orders
: {
private
: ["deletedAt"] },
customers
: {
hidden
: ["orgId"],
virtual
: (
customer
) => ({
displayName
:
virtual
.
text
(
sql
`upper(${
customer
.
name
})`),
}), }, }, }).
withContext
<{
orgId
: string }>();
const
orders
=
engine
.
defineResource
("orders", {
relations
: {
customer
: true },
views
: {
list
: {
select
: {
id
: true,
reference
: true,
totalAmount
: true,
customer
: {
select
: {
id
: true,
displayName
: true } },
}, },
internal
: {
select
: {
id
: true,
customer
: {
select
: {
id
: true,
name
: true,
orgId
: true } },
}, }, },
query
: {
scope
: (
f
,
context
) =>
f
.
is
("customer.orgId",
context
.
orgId
),
search
: {
allowed
: ["reference", "customer.displayName"] },
},
summary
: (
order
) => ({
rows
:
count
(),
totalAmount
:
sum
(
order
.
totalAmount
),
}), }); type
ListRow
= typeof
orders
.
$infer
.
views
.
list
;

select is exact:

  • true on a relation returns that table's default fields, without its own relations or virtuals.
  • A selected to-one relation keeps the nullability Drizzle declares. A to-many relation is an array.
  • Hidden fields and virtuals are only returned when a view selects them. Private fields can't be selected.
  • Internal ordering ids are removed when the view doesn't select them.

Use a view

Pass view to query, findById, queryRows or scan:

const page = await orders.query({ context, request, view: "list" });

The view is chosen by your server, per endpoint. It is not part of the client request, and choosing a view does not authorize the caller: keep authorization in the scope.

Pass either view or load (load profiles), not both. With neither, the resource's load defaults and the model rules apply.

Validate the response

The Zod and Valibot responseSchema helpers accept a view and follow the same private and hidden rules. See Validation.

Copyright © 2026