Setting up the ORM client
For a long-lived Node process, thedb.orm property is available directly on the postgres() facade. ORM collection accessors use the model name from your contract (capitalized, as declared in your schema):
createOrmClient(runtime) inside the request handler instead. See Deploying to serverless runtimes for the full pattern.
Defining collection classes
Collections are classes that extend the baseCollection type and are registered on the orm() factory. Each collection maps to a model in your contract and can expose custom query methods:
orm() factory. Pass the runtime from db.runtime() (synchronous):
Filtering with .where()
You can pass a shorthand object filter or a typed expression callback. Both forms can be chained:
Sorting with .orderBy()
Pass a callback that selects a field and calls a direction method:
Pagination with .take() and .skip()
Including relations with .include()
Pass a relation name to eager-load it. You can pass a callback to further constrain the included collection — including nested includes:
.include() is a fully typed collection: you get the same .where(), .orderBy(), .take(), and .select() methods at every level.
Selecting fields with .select()
Use .select() to return only specific fields. TypeScript infers the return type from the selected columns:
Fetching results
.stream() returns an AsyncIterableResult. If execution fails, the iterable throws. Wrap streaming consumers in a try/catch or use a for await that propagates errors naturally.Creating, updating, and deleting records
Using withTransaction() for mutations
Wrap related mutations in a transaction to ensure atomicity. The withTransaction helper from @prisma-next/sql-runtime acquires a transaction on the runtime and runs your callback:
Using the ORM client in a serverless context
In per-request runtimes, construct the ORM client inside the request handler using the per-requestruntime returned by db.connect():
createOrmClient is a factory that passes the per-request runtime to orm({...}):