Use this file to discover all available pages before exploring further.
The ORM client is the high-level query surface in Prisma Next. It gives you typed model collections with a fluent API for filtering, ordering, pagination, relations, and mutations — all verified against your emitted contract at compile time. You compose queries by chaining methods on a collection; the client builds and executes the required SQL plans for you.
For a long-lived Node process, the db.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):
// db.tsimport postgres from '@prisma-next/postgres/runtime';import type { Contract } from './.prisma/contract.d';import contractJson from './.prisma/contract.json' with { type: 'json' };export const db = postgres<Contract>({ contractJson });// Access collections by model name (e.g. db.orm.User, db.orm.Post)const users = await db.orm.User.take(10).all();
For serverless runtimes, use createOrmClient(runtime) inside the request handler instead. See Deploying to serverless runtimes for the full pattern.
Collections are classes that extend the base Collection type and are registered on the orm() factory. Each collection maps to a model in your contract and can expose custom query methods:
Register collections on the orm() factory. Pass the runtime from db.runtime() (synchronous):
// orm-client/client.tsimport type { Runtime } from '@prisma-next/sql-runtime';import { orm } from '@prisma-next/sql-orm-client';import type { ExecutionContext } from '@prisma-next/sql-relational-core/query-lane-context';import type { Contract } from '../.prisma/contract.d';import { db } from '../db';import { UserCollection, PostCollection } from './collections';const context = db.context as ExecutionContext<Contract>;export function createOrmClient(runtime: Runtime) { return orm({ runtime, context, collections: { User: UserCollection, Post: PostCollection, }, });}
// Materializes all matching rows into an arrayconst posts = await db.orm.Post.where({ userId: currentUserId }).all();
.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.
const user = await db.orm.User.select('id', 'email', 'kind').create({ id: crypto.randomUUID(), email: 'bob@example.com', displayName: 'Bob', kind: 'user', createdAt: new Date(),});
update() and delete() require a .where() clause. Calling them without filtering is a compile-time error — Prisma Next enforces this to prevent accidental bulk 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:
import { withTransaction } from '@prisma-next/sql-runtime';const result = await withTransaction(db.runtime(), async (tx) => { const orm = createOrmClient(tx); const order = await orm.Order.create({ userId, total }); await orm.OrderItem.create({ orderId: order.id, productId, quantity }); return order;});
All statements inside the callback run on the same connection. If the callback throws, the transaction is rolled back automatically.