Resources

Engine

Create the engine once, declare the per-request context, and set field rules shared by every resource.

The engine binds to your Drizzle setup. You create it once and define every resource from it.

engine.ts
import { 
createQueryEngine
} from "drizzle-resource";
import {
db
} from "./db";
import {
schema
} from "./schema";
import {
relations
} from "./relations";
export const
engine
=
createQueryEngine
({
db
,
schema
,
relations
});
OptionWhat it is
dbYour Drizzle database instance, with relational queries (db.query.<table>.findMany).
schemaYour tables, keyed by name. Field paths and value types are read from it.
relationsYour Drizzle relations. They make paths like customer.name and orderLines.product.category valid and decide the joins.
modelsOptional field rules per table: private, hidden and virtual fields. See Models.

Per-request context

Most endpoints need values from the current request: the tenant, the user, their permissions. Declare their type with .withContext<T>():

import { 
createQueryEngine
} from "drizzle-resource";
import {
db
} from "./db";
import {
relations
} from "./relations";
import {
schema
} from "./schema";
export const
engine
=
createQueryEngine
({
db
,
schema
,
relations
}).
withContext
<{
orgId
: string;
userId
: string;
}>();

The context is passed by your server on every call, next to the client's request:

await ordersResource.query({ context: { orgId, userId }, request });

query.scope and custom stages receive it. It is never read from the client's request body. Leave .withContext() out if your resources don't need it.

One engine, many resources

Export the engine and import it in each resource file:

import { 
createQueryEngine
} from "drizzle-resource";
import {
db
} from "./db";
import {
relations
} from "./relations";
import {
schema
} from "./schema";
// engine.ts export const
engine
=
createQueryEngine
({
db
,
schema
,
relations
})
.
withContext
<{
orgId
: string }>()
// orders.resource.ts import {
engine
} from './engine'
export const
ordersResource
=
engine
.
defineResource
('orders', { ... })
// products.resource.ts import {
engine
} from './engine'
export const
productsResource
=
engine
.
defineResource
('products', { ... })

Reusing a scope

Define the scope inline on each resource with query.scope. When several resources share the same rule, engine.defineScope(...) lets you write it once. See Scope.

Copyright © 2026