Transactions and runtime reference
Reference for the Prisma ORM client lifecycle, transactions, prepared statements, and execution options.
Prisma ORM 8 renames schema.prisma to contract.prisma. Run npx prisma contract emit, which writes two files next to it: contract.json, which your app imports, and contract.d.ts, which gives you the TypeScript types. A new project keeps all three files in src/prisma/. Every client you create needs contract.json. The query you came for, prisma.user.findMany({ where }), is now db.orm.public.User.where(...).all(). Coming from Prisma ORM 7 lists the packages to install, the commands to run, and the rest of the renamed calls.
db is the client that postgres(...) or mongo(...) returns. db.runtime() gives you the object that actually runs queries, and that object is called the runtime.
Create, connect, and close the client, run transactions, prepare statements, and set per-query execution options, on PostgreSQL and MongoDB. For a task-oriented walkthrough, see the Fundamentals guide to Transactions. For the ways to query inside a transaction, see the ORM client reference, the SQL query builder reference, and the raw queries reference.
Transactions are not available on MongoDB. To run one there, pass your own MongoClient with the mongoClient option and use the driver's session.withTransaction(...), which Transactions on MongoDB shows in code.
Client lifecycle on PostgreSQL
Section titled “Client lifecycle on PostgreSQL”Create a PostgreSQL client with postgres(...), connect it to get a runtime, run queries, and close it when you are done.
postgres(options)
Section titled “postgres(options)”Create a PostgreSQL client.
Remarks
Section titled “Remarks”- Import
contract.jsonand pass it ascontractJson, as every example below does. Passcontractonly if you already hold a contract object; most code passescontractJson. Supply exactly one. - The
<Contract>type argument is what typesdb.ormanddb.sql.contract.d.tsexports it under the nameContract. Pass it yourself whenever you passcontractJson, because TypeScript cannot read a type out of a JSON file. It is inferred only when you use thecontractoption. - Tell the client which database to use in one of three ways:
url,pg, orbinding. Abindingwraps any of the three choices in one object, with akindfield saying which it is:{ kind: 'url', url },{ kind: 'pgPool', pool }, or{ kind: 'pgClient', client }. Use it when your code picks the source at run time. Pass exactly one ofurl,pg, andbinding, or leave all three out and pass one toconnect(). - When you pass a
pgpool or client, you own it: calldb.close()first, then close it yourself withpool.end(). When you pass aurl, the client creates the pool and closes it. Set that pool's timeouts withpoolOptions, which has no effect on a pool you supply yourself. - Some types come from a PostgreSQL extension, such as a
pgvector.Vector(1536)type in yourcontract.prisma. Runnpm install @prisma/orm-extension-pgvectorfirst. Thenimport pgvector from '@prisma/orm-extension-pgvector/runtime'and passextensions: [pgvector]. - A middleware is an object with a name and one or more hooks, such as
{ name: 'no-big-deletes', beforeQuery: (query) => { if (query.sql.includes('DELETE')) throw new Error('blocked') } }. Throw from a hook to block the query. The hooks arebeforeCompile,beforeQuery,beforeExecute,interceptQuery,interceptExecute,onRow,afterQuery, andafterExecute. See How middleware works. - Migrations record in your database which contract it matches. With
verifyMarker: 'onFirstUse', the default, the first query checks that record and logs a warning on a mismatch, then runs anyway. If you see the warning, runnpx prisma db update.verifyMarker: falseskips the check. postgres(options)does not open a connection. The client opens one on first use, soconnect()is optional: call it to open the connection up front and fail early.
Options
Section titled “Options”Pass exactly one of url, pg, and binding.
| Name | Type | Required | Description |
|---|---|---|---|
contractJson / contract |
JSON contract or contract value | Yes | The contract. Supply exactly one. |
url |
string |
See note | A PostgreSQL connection string. |
pg |
Pool or Client from the pg package |
See note | An existing pg instance to use. You close it yourself. |
binding |
{ kind: 'url' | 'pgPool' | 'pgClient', ... } |
See note | The same three choices in one object. |
poolOptions |
{ connectionTimeoutMillis?: number; idleTimeoutMillis?: number } |
No | Timeouts for the pool the client creates from url (defaults 20000 / 30000). |
extensions |
Array of extension runtimes, the default export of each package's /runtime subpath |
No | The PostgreSQL extensions your contract's types need. Install each package, then import its /runtime subpath and put the default export in this array. |
middleware |
Array of middleware | No | Code that runs around every query. See How middleware works. |
verifyMarker |
'onFirstUse' or false |
No | Whether the first query checks that the database was set up for this contract. Defaults to 'onFirstUse'. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
PostgresClient |
postgres<Contract>({ contractJson, url }) |
A client that has not connected yet. Its members are listed below. |
.ormholds your models by model name. See the ORM client reference..sqlis the SQL query builder, keyed by PostgreSQL schema, then by table name:db.sql.public.tag. The table name is the model's@@mapvalue, or the model name exactly as written when the model sets no@@map. See the SQL query builder reference. Write raw SQL withdb.raw.sql`...`, as the raw queries reference shows..enumsholds the enum values from your contract, by PostgreSQL schema:db.enums.public.Role..nativeEnumsholds the values of enum types that exist in PostgreSQL itself, created withCREATE TYPE, also by schema. Look the type up by name in brackets:db.nativeEnums.auth['AalLevel'].valuesis['aal1', 'aal2', 'aal3']..contractis the contract as an object.connect(),runtime(),transaction(),prepare(), andclose()are documented on this page.
Examples
Section titled “Examples”Create a client from a connection string
Section titled “Create a client from a connection string”import postgres from '@prisma/orm-postgres/runtime';
import type { Contract } from './contract.d';
import contractJson from './contract.json' with { type: 'json' };
const db = postgres<Contract>({ contractJson, url: process.env.DATABASE_URL! });These examples assume the file is beside contract.json in src/prisma/, so from anywhere else, change the relative paths. Write ./contract.d exactly like that, with no extension. It resolves under preserve or esnext with bundler, the settings prisma orm init writes, and under nodenext as CommonJS; under nodenext, node18, or node20 as ES modules it does not, and the upgrade guide covers that case. The line import contractJson from './contract.json' with { type: 'json' } needs Node.js 22.18 or newer and TypeScript 5.9 or newer. In tsconfig.json it needs resolveJsonModule: true and one of these module settings, each with its moduleResolution: preserve with bundler, esnext with bundler, or nodenext with nodenext. node18 and node20 also work, with moduleResolution left unset. prisma orm init writes preserve and bundler into new projects. When module is nodenext and package.json has no "type": "module", TypeScript treats the file as CommonJS, and there the import is import contractJson from './contract.json'; with no with part. The Prisma ORM 7 to 8 upgrade guide walks through which setting fits which kind of project. TypeScript types every process.env value as possibly undefined, so the examples write ! after it, the same way prisma orm init does.
Bind to an existing pg pool
Section titled “Bind to an existing pg pool”import postgres from '@prisma/orm-postgres/runtime';
import { Pool } from 'pg';
import type { Contract } from './contract.d';
import contractJson from './contract.json' with { type: 'json' };
const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
const db = postgres<Contract>({ contractJson, pg: pool });connect()
Section titled “connect()”Open a connection and return a runtime.
Remarks
Section titled “Remarks”- Returns a
Promise<Runtime>. The runtime is the connected object that runs queries. It has these calls:runtime.query(built)runs a built query and returns its rows. A built query is the object you get from.build()at the end of a builder or raw chain. See Running a built query.runtime.execute(built)runs a built query that returns no rows, such as an insert, and returns{ affectedRows }. See Running a built query.runtime.connection()takes one connection out of the pool so that several queries run on the same connection. It returns a promise, and you put the connection back withconnection.release():const connection = await runtime.connection(); try { await connection.query(built) } finally { await connection.release() }. See Manual connection and transaction control.runtime.prepare(declaration, callback)prepares a statement you run many times with different values. The client has aprepare()too, and the two do the same thing. See Prepared statements (PostgreSQL).runtime.telemetry()reports how the most recent query went. Seeruntime.telemetry().
connect()takes the same database options aspostgres(...), so a client you created without one can be given its database here:await db.connect({ url: process.env.DATABASE_URL! }), orawait db.connect({ binding: { kind: 'url', url } }).- Every error has a
codeproperty, and the error reference lists them all. Catch the error and compareerror.codewith the value shown here, for exampleif (error.code === 'DRIVER.NOT_CONNECTED'). - Call
connect()before any query, or not at all. Running a query connects the client, so aconnect()after that rejects with an error whosecodeisDRIVER.ALREADY_CONNECTED. So does a secondconnect().
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<Runtime> |
const runtime = await db.connect() |
The connected runtime. |
Examples
Section titled “Examples”Connect and run a query
Section titled “Connect and run a query”const runtime = await db.connect();
const tags = await runtime.query(db.sql.public.tag.select('id', 'label').build());runtime()
Section titled “runtime()”Get the current runtime synchronously.
Remarks
Section titled “Remarks”- On PostgreSQL,
runtime()is synchronous: it returns theRuntimedirectly, not a promise. You can call it before connecting, because the client connects on first use.
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Runtime |
const runtime = db.runtime() |
The runtime, returned synchronously. |
close()
Section titled “close()”Close the client and release its pool.
Remarks
Section titled “Remarks”- Returns a
Promise<void>.close()is safe to call at any time. - After
close(),connect()rejects andruntime()throws an error whosecodeisDRIVER.NOT_CONNECTED(Postgres client is closed).
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<void> |
await db.close() |
Resolves when the client is closed. |
Examples
Section titled “Examples”Close when finished
Section titled “Close when finished”const db = postgres<Contract>({ contractJson, url: process.env.DATABASE_URL! });
const admins = await db.orm.public.User.where((u) => u.kind.eq('admin')).all();
await db.close();await using (automatic disposal)
Section titled “await using (automatic disposal)”The client implements Symbol.asyncDispose, so await using closes it automatically at the end of its block.
Remarks
Section titled “Remarks”- Disposal fires at the end of the block the
await usingdeclaration lives in, not on the next line. Scope the client to the block where you need it. After the block exits, the client is closed the same wayclose()closes it: a laterconnect()rejects with an error whosecodeisDRIVER.NOT_CONNECTED. await usingneeds@types/nodeinstalled, or"esnext.disposable"added to thelibarray in yourtsconfig.json.
Examples
Section titled “Examples”Close automatically with await using
Section titled “Close automatically with await using”{
await using db = postgres<Contract>({ contractJson, url: process.env.DATABASE_URL! });
const tags = await db.runtime().query(db.sql.public.tag.select('id', 'label').build());
} // db is closed hereClient lifecycle on MongoDB
Section titled “Client lifecycle on MongoDB”Create a MongoDB client with mongo(...). The entry point and several lifecycle details differ from PostgreSQL, most notably that runtime() is asynchronous.
mongo(options)
Section titled “mongo(options)”Create a MongoDB client.
Remarks
Section titled “Remarks”- Pass the contract with
contractJsonorcontract(supply exactly one), the same way as on PostgreSQL. - Tell the client which database to use in one of four ways:
url,uri,mongoClient, orbinding. Abindingwraps the choices in one object, with akindfield saying which it is:{ kind: 'url', url, dbName }or{ kind: 'mongoClient', client, dbName }. Use it when your code picks the source at run time, and use theurlkind for auri. Pass exactly one of the four, or leave all four out and pass one to MongoDBconnect().dbNameis separate and does not count as one of the four, so{ url, dbName }is allowed. - With
url, the database name comes from the path of the connection string, as inmongodb://host:27017/app. AdddbNameto override that name, or when the string has no name in its path.uritakes the same kind of string, and always needsdbName. - Client ownership: with
urloruri, Prisma ORM creates the underlyingMongoClientand closes it onclose(). WithmongoClient, you supplied the client, so Prisma ORM does not close it. Becauseclose()leaves your client open, you can use oneMongoClientboth through Prisma ORM and in code you write against themongodbpackage directly. You close that client yourself. mongo(options)does not open a connection. The runtime is built on first use, or explicitly via MongoDBconnect().
Options
Section titled “Options”Pass exactly one of url, uri, mongoClient, and binding. dbName is separate, and uri and mongoClient both require it.
| Name | Type | Required | Description |
|---|---|---|---|
contractJson / contract |
JSON contract or contract value | Yes | The contract. Supply exactly one. |
url |
string |
See note | A mongodb:// or mongodb+srv:// string, with the database name in its path. |
uri + dbName |
string + string |
See note | The same kind of string, plus the database name, which is required here. |
mongoClient + dbName |
MongoClient + string |
See note | An existing MongoClient you own, plus the database name. |
binding |
{ kind: 'url' | 'mongoClient', ... } |
See note | The same choices in one object. |
middleware |
Array of middleware | No | Code that runs around every query. See How middleware works. |
mode |
'strict' or 'permissive' |
No | Defaults to 'strict'. Prisma ORM ignores this value. It is passed through to your middleware so your own code can branch on it. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
MongoClient |
mongo<Contract>({ contractJson, url, dbName }) |
A client that has not connected yet. This is Prisma ORM's own type. The mongodb driver has a class of the same name, so when you import both, name this one db. |
.ormholds your models by collection name, with no.publicin the path:db.orm.users. The collection name is the model's@@mapvalue, or the model name exactly as written when the model sets no@@map. The example schema sets@@map("users")onUser, which is why the key here isusers. See the ORM client reference..queryis the pipeline builder, which builds MongoDB aggregation pipelines. See the pipeline builder reference..rawsends raw MongoDB commands. See the raw queries reference..enumsholds the enum values from your contract, and.contractis the contract as an object. MongoDBconnect(), MongoDBruntime(), and MongoDBclose()are documented below.
Examples
Section titled “Examples”Create a client from a connection string
Section titled “Create a client from a connection string”import mongo from '@prisma/orm-mongo/runtime';
import type { Contract } from './contract.d';
import contractJson from './contract.json' with { type: 'json' };
const db = mongo<Contract>({ contractJson, url: process.env.MONGODB_URL!, dbName: 'app' });Share an existing MongoClient
Section titled “Share an existing MongoClient”import mongo from '@prisma/orm-mongo/runtime';
import { MongoClient } from 'mongodb';
import type { Contract } from './contract.d';
import contractJson from './contract.json' with { type: 'json' };
// `MongoClient` here is the driver's class; the `db` below is Prisma ORM's own MongoClient type
const client = new MongoClient(process.env.MONGODB_URL!);
const db = mongo<Contract>({ contractJson, mongoClient: client, dbName: 'app' });connect()
Section titled “connect()”Open a connection and return a runtime.
Remarks
Section titled “Remarks”- Returns a
Promise<MongoRuntime>. Callconnect()before any query, or not at all. Running a query connects the client, so aconnect()after that rejects with an error whosecodeisDRIVER.ALREADY_CONNECTED. So does a secondconnect(). connect()takes the same database options asmongo(...), so a client you created without one can be given its database here:await db.connect({ url: process.env.MONGODB_URL!, dbName: 'app' }).
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<MongoRuntime> |
const runtime = await db.connect() |
The connected runtime (query, execute, and close only). |
runtime()
Section titled “runtime()”Get the runtime.
Remarks
Section titled “Remarks”- On MongoDB,
runtime()` returns a **Promise
**: you must awaitit. PostgreSQL'sruntime()is synchronous, so writeawait db.runtime()on MongoDB anddb.runtime()` on PostgreSQL.
MongoRuntimehasquery,execute, andcloseonly. It has noconnection(),prepare(), ortelemetry(). See Transactions (MongoDB) and Execution options and results.
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<MongoRuntime> |
const runtime = await db.runtime() |
The runtime, resolved asynchronously. |
runtime.query()
Section titled “runtime.query()”Run a built query through the runtime.
Remarks
Section titled “Remarks”(await db.runtime()).query(built)runs any built MongoDB query, including one built by the pipeline builder (db.query).- Returns an
AsyncIterableResult, which holds the rows the query returns:awaitit for an array, orfor awaitto read the rows one at a time as they arrive. - For an update or delete command, use
runtime.execute(built), which returns{ affectedRows }. Other commands throw an error whosecodeisRUNTIME.MONGO_STATISTICS_UNSUPPORTED. There is noexecute()on the client itself.
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
AsyncIterableResult<Row> |
await (await db.runtime()).query(built) |
The built query's rows. |
Examples
Section titled “Examples”Run a query built by the pipeline builder
Section titled “Run a query built by the pipeline builder”// 'posts' is the collection name, the same key you use on `db.orm`
const built = db.query.from('posts').build();
const posts = await (await db.runtime()).query(built);close()
Section titled “close()”Close the client.
Remarks
Section titled “Remarks”- Always call
db.close().runtime.close()leaves the client looking open, and later calls fail. - Returns a
Promise<void>. Afterclose(), any further use rejects with an error whosecodeisDRIVER.NOT_CONNECTED(Mongo client is closed). That coversdb.runtime()and ORM access such asdb.orm.users.first(), since both go through the same runtime. When you supplied amongoClient,close()does not close your client (seemongo(options)).
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<void> |
await db.close() |
Resolves when the client is closed. |
Examples
Section titled “Examples”Use after close throws
Section titled “Use after close throws”await db.close();
await db.runtime(); // rejects with DRIVER.NOT_CONNECTEDawait using (automatic disposal)
Section titled “await using (automatic disposal)”The MongoDB client implements Symbol.asyncDispose too, so await using closes it at the end of its block.
Remarks
Section titled “Remarks”- Disposal fires at the end of the block, exactly as on PostgreSQL. After the block exits, a later
connect()rejects with an error whosecodeisDRIVER.NOT_CONNECTED.
Examples
Section titled “Examples”Close automatically with await using
Section titled “Close automatically with await using”{
await using db = mongo<Contract>({ contractJson, url: process.env.MONGODB_URL!, dbName: 'app' });
await db.connect();
// ... run queries ...
} // db is closed hereTransactions (PostgreSQL)
Section titled “Transactions (PostgreSQL)”A transaction groups several writes into one unit: they all commit together, or they all roll back. Use db.transaction(...) in application code. If you are writing a function that takes a runtime as a parameter instead of the client, use withTransaction(...). If you need to run several statements on one connection, get a connection with runtime.connection() and call commit() or rollback() yourself.
db.transaction(callback)
Section titled “db.transaction(callback)”Run a callback inside a transaction. The transaction commits when the callback returns and rolls back when it throws.
Remarks
Section titled “Remarks”- Query through
tx, notdb.tx.ormholds your models. Build queries withtx.sql, then run them withtx.query(...)to get rows back, ortx.execute(...)when the write returns no rows.tx.execute(...)resolves to{ affectedRows }, the number of rows the statement changed. Every call ontxuses the same transaction connection, and queries ondbrun outside the transaction. db.transaction(...)takes the callback and nothing else. There are noisolationLevel,timeout, ormaxWaitoptions, and Prisma ORM offers no way to set the isolation level. Nothing retries a failed transaction for you. Write the retry loop yourself. A transaction has no time limit. PostgreSQL'sidle_in_transaction_session_timeoutends one that sits idle between statements, and on PostgreSQL 17 and latertransaction_timeoutcaps its total length. Set either in your connection string or server configuration. See the fundamentals transactions guide.- To put a time limit on the queries inside a transaction, pass a
signalto eachtx.query(...)andtx.execute(...)call, as the example below does. SeeRuntimeExecuteOptions. txhas notransaction()method, so transactions do not nest. If a helper you call starts its own transaction, change it to taketxas a parameter instead.TransactionContext, imported from@prisma/orm-postgres/family-runtime, hasqueryandexecuteonly, so a helper typed with it cannot usetx.ormortx.sql:async function addTag(tx: TransactionContext) { ... }. There is no exported type for the fulltx, so derive it:type Tx = Parameters<Parameters<typeof db.transaction>[0]>[0].tx.sqlis a full SQL builder, keyed the same way asdb.sql: usetx.sql.public.<table>, wherepublicis the PostgreSQL schema.- Reads inside the transaction see the transaction's own uncommitted writes.
tx.enumsandtx.nativeEnumsare the same enum accessors the client has, and.valuesis the list of an enum's members.- The callback's return value passes through as the result of
db.transaction(...). tx.query(...)returns anAsyncIterableResultthat works only while the transaction is open. Read it inside the callback. A result you return and then read after the transaction has ended rejects with an error whosecodeisRUNTIME.TRANSACTION_CLOSED. To act on a code, catch the error and compareerror.codewith the string. Every code is listed in the error reference.
Options
Section titled “Options”| Name | Type | Required | Description |
|---|---|---|---|
callback |
(tx) => Promise<R> |
Yes | The transactional work. tx exposes orm, sql, query, execute, enums, and nativeEnums. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<R> |
await db.transaction(async (tx) => ...) |
Whatever the callback returns, so R is your callback's own return type. |
Examples
Section titled “Examples”Commit several writes atomically
Section titled “Commit several writes atomically”await db.transaction(async (tx) => {
await tx.orm.public.Tag.create({ label: 'tx-commit-a' });
await tx.orm.public.Tag.create({ label: 'tx-commit-b' });
});
// both tags exist nowRoll back when the callback throws
Section titled “Roll back when the callback throws”try {
await db.transaction(async (tx) => {
await tx.orm.public.Tag.create({ label: 'tx-rollback' });
throw new Error('deliberate rollback');
});
} catch {
// the tag was rolled back and does not exist
}Read your own uncommitted writes
Section titled “Read your own uncommitted writes”const { createdId, found } = await db.transaction(async (tx) => {
const created = await tx.orm.public.Tag.create({ label: 'tx-ryow' });
const found = await tx.orm.public.Tag.where({ label: 'tx-ryow' }).first();
return { createdId: created.id, found };
});
// found.id === createdIdRun a SQL builder query with tx.sql and tx.execute
Section titled “Run a SQL builder query with tx.sql and tx.execute”await db.transaction(async (tx) => {
const signal = AbortSignal.timeout(5000); // give this statement five seconds
await tx.execute(
tx.sql.public.tag.insert([{ id: crypto.randomUUID(), label: 'tx-sql-insert' }]).build(),
{ signal },
);
});For Prisma ORM 7 users, an array of queries becomes a callback:
- const [user, post] = await prisma.$transaction([
- prisma.user.create({ data: { email, displayName } }),
- prisma.post.create({ data: { title, userId } }),
- ]);
+ const { user, post } = await db.transaction(async (tx) => {
+ const user = await tx.orm.public.User.create({ email, displayName, kind: 'user' });
+ const post = await tx.orm.public.Post.create({ title, userId: user.id });
+ return { user, post };
+ });The callback form does something the array form never could: one query's result (here user.id) can feed the next query in the same transaction.
withTransaction(runtime, callback)
Section titled “withTransaction(runtime, callback)”A transaction helper you import directly. Use it when you are writing a function that takes a runtime as a parameter instead of the client.
Remarks
Section titled “Remarks”- Imported from
@prisma/orm-postgres/family-runtime, along with theRuntimetype. - The callback receives a transaction handle whose only methods are
queryandexecute. Unlikedb.transaction(...)'stx, it has no.ormor.sql. Pass a SQL builder into your function as well, such as the client'sdb.sql, and build your queries with that. - Commits on return, rolls back on throw, the same as
db.transaction(...).
Options
Section titled “Options”| Name | Type | Required | Description |
|---|---|---|---|
runtime |
Runtime |
Yes | The runtime to open the transaction on. |
callback |
(tx) => Promise<R> |
Yes | The transactional work. tx exposes query and execute. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<R> |
await withTransaction(runtime, async (tx) => ...) |
Whatever the callback returns. |
Examples
Section titled “Examples”Commit two writes with withTransaction
Section titled “Commit two writes with withTransaction”import { withTransaction, type Runtime } from '@prisma/orm-postgres/family-runtime';
async function addTags(runtime: Runtime, sql: typeof db.sql) {
return withTransaction(runtime, async (tx) => {
await tx.execute(sql.public.tag.insert([{ id: crypto.randomUUID(), label: 'wt-commit-1' }]).build());
await tx.execute(sql.public.tag.insert([{ id: crypto.randomUUID(), label: 'wt-commit-2' }]).build());
});
}
await addTags(db.runtime(), db.sql);Manual connection and transaction control
Section titled “Manual connection and transaction control”The lowest level: acquire a connection, open a transaction on it, commit or roll back yourself, and return the connection to the pool. Use it when you need several statements on one connection, such as a read and then a write with no transaction around them.
Remarks
Section titled “Remarks”runtime.connection()returns a dedicated connection.connection.transaction()opens a transaction on it. Run built queries withtransaction.query(...)for rows ortransaction.execute(...)for writes that return no rows, then calltransaction.commit()ortransaction.rollback(). Alwaysrelease()the connection when done, to return it to the pool. Commit or roll back on every path first, then release: the examples nest atryfor the transaction inside thetrywhosefinallyreleases the connection, so a statement that throws neither leaves the transaction open nor leaks the connection.connection.destroy()throws that connection away instead of returning it to the pool. Usedestroy()only for a connection you no longer trust.
Options
Section titled “Options”| Method | Type | Description |
|---|---|---|
runtime.connection() |
none | Returns a Promise of a dedicated connection. |
connection.transaction() |
none | Returns a Promise of a transaction on that connection. |
transaction.commit() / transaction.rollback() |
none | Commit or discard the transaction. |
connection.release() |
none | Return the connection to the pool. |
connection.destroy() |
none | Throw this connection away instead of returning it to the pool. |
Examples
Section titled “Examples”Commit manually, then release
Section titled “Commit manually, then release”const runtime = db.runtime();
const connection = await runtime.connection();
try {
await connection.query(db.sql.public.tag.select('id').limit(1).build()); // a read on this connection
const transaction = await connection.transaction();
try {
await transaction.execute(
db.sql.public.tag.insert([{ id: crypto.randomUUID(), label: 'manual-commit' }]).build(),
);
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
} finally {
await connection.release();
}Roll back manually, then release
Section titled “Roll back manually, then release”const connection = await runtime.connection();
try {
const transaction = await connection.transaction();
try {
await transaction.execute(
db.sql.public.tag.insert([{ id: crypto.randomUUID(), label: 'manual-rollback' }]).build(),
);
} finally {
await transaction.rollback();
}
} finally {
await connection.release();
}Throw a connection away with destroy()
Section titled “Throw a connection away with destroy()”const connection = await runtime.connection();
await connection.destroy();
// the runtime stays healthy and opens a replacement connection on the next queryTransactions (MongoDB)
Section titled “Transactions (MongoDB)”Prepared statements (PostgreSQL)
Section titled “Prepared statements (PostgreSQL)”A prepared statement compiles a query once against a declaration of its parameters, then runs it repeatedly with different values. Prepared statements are not available on MongoDB.
runtime.prepare(declaration, callback)
Section titled “runtime.prepare(declaration, callback)”Prepare a statement from a runtime.
Remarks
Section titled “Remarks”- The declaration maps each parameter name to a type id, for example
{ label: 'pg/text@1' }. Usedb.sql.public.<table>.columns.<column>.codecIdto get any column's id. The table on the raw queries page lists the common ones. - The callback takes one argument, the declared
params, and returns the built query, which is what.build()gives you.SqlQueryPlanin the tables below is the type of a built query. Build it with a SQL builder you already hold, such asdb.sql. In thewhere((f, fns) => ...)calls below,fholds the table's columns andfnsholds the comparison functions. - A declared parameter that the callback never uses is rejected at prepare time with an error whose
codeisRUNTIME.PREPARE_UNUSED_PARAM, withdetails.unusedlisting the parameter names you declared but did not use. The rejection happens atprepare(), before any execution. - The resulting
PreparedStatementruns viaps.query(target, params); seePreparedStatement.query.
Options
Section titled “Options”| Name | Type | Required | Description |
|---|---|---|---|
declaration |
Object mapping param name to type id | Yes | The statement's parameters. |
callback |
(params) => SqlQueryPlan |
Yes | Builds the query from the declared params. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<PreparedStatement> |
await runtime.prepare({ label: 'pg/text@1' }, (params) => ...) |
A reusable prepared statement. |
Examples
Section titled “Examples”Prepare and run a statement
Section titled “Prepare and run a statement”const runtime = db.runtime();
const ps = await runtime.prepare({ label: 'pg/text@1' }, (params) =>
db.sql.public.tag.select('id', 'label').where((f, fns) => fns.eq(f.label, params.label)).limit(1).build(),
);
const typescript = await ps.query(runtime, { label: 'typescript' });
const missing = await ps.query(runtime, { label: 'does-not-exist' });
// typescript has one row; missing has noneAn unused declared parameter is rejected at prepare time
Section titled “An unused declared parameter is rejected at prepare time”await runtime.prepare({ label: 'pg/text@1', unused: 'pg/int4@1' }, (params) =>
db.sql.public.tag.select('id', 'label').where((f, fns) => fns.eq(f.label, params.label)).limit(1).build(),
);
// rejects with RUNTIME.PREPARE_UNUSED_PARAM, details: { unused: ['unused'] }db.prepare(declaration, callback)
Section titled “db.prepare(declaration, callback)”Prepare a statement on the client itself.
Remarks
Section titled “Remarks”- The callback takes one argument, the declared
params, as it does onruntime.prepare(...). Build the query with the client's owndb.sqlordb.orm. - The callback can return a built SQL query, or an ORM query that ends in
.prepared.all(),.prepared.first(), or.prepared.aggregate(...)..preparedalways goes right before that last call, so a grouped aggregate is writtendb.orm.public.Post.groupBy('userId').prepared.aggregate((agg) => ({ posts: agg.count() })). - The ORM query is built once.
db.prepare(...)resolves to a prepared query, here calledps, and you run it withps.query(db.runtime(), values), wherevaluesholds a value for each declared parameter. A connection or a transaction also works in place ofdb.runtime(). Each run uses the new values and returns what the same call without.preparedreturns:all()returns the rows, as anAsyncIterableResultthat youawaitfor an array.first()returns a promise of one row, or ofnullwhen no row matches.aggregate(...)returns a promise of the aggregate object.groupBy(...)followed byaggregate(...)returns a promise of an array with one object per group.
prepare()starts the connection itself. Calldb.runtime()for the runtime.db.connect()would throw an error whosecodeisDRIVER.ALREADY_CONNECTED, becauseprepare()already connected.
Options
Section titled “Options”| Name | Type | Required | Description |
|---|---|---|---|
declaration |
Object mapping param name to type id | Yes | The statement's parameters. |
callback |
(params) => SqlQueryPlan or a prepared ORM query |
Yes | Builds the query from the declared params, with db.sql or db.orm. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Promise<PreparedStatement> |
await db.prepare({ label: 'pg/text@1' }, (params) => ...) |
A reusable prepared statement. |
Examples
Section titled “Examples”Prepare a SQL builder query off the client
Section titled “Prepare a SQL builder query off the client”const ps = await db.prepare({ label: 'pg/text@1' }, (params) =>
db.sql.public.tag.select('id', 'label').where((f, fns) => fns.eq(f.label, params.label)).limit(1).build(),
);
const rows = await ps.query(db.runtime(), { label: 'typescript' });Prepare an ORM read and an aggregate
Section titled “Prepare an ORM read and an aggregate”const byLabel = await db.prepare({ label: 'pg/text@1' }, (params) =>
db.orm.public.Tag.select('id', 'label').prepared.first({ label: params.label }),
);
const tag = await byLabel.query(db.runtime(), { label: 'typescript' });
// { id: '...', label: 'typescript' }, or null
const countTags = await db.prepare({}, () =>
db.orm.public.Tag.prepared.aggregate((agg) => ({ total: agg.count() })),
);
const { total } = await countTags.query(db.runtime(), {});In an ORM query, a prepared parameter works in where(...) filters, in the callback you pass to include(...), and in .limit(...) and .offset(...), so the same statement can page through results. Declare a page size or offset as 'pg/int4@1', as in { take: 'pg/int4@1' }, and pass params.take to .limit(...). A text type id such as 'pg/text@1', or a nullable one such as { codecId: 'pg/int4@1', nullable: true }, is a type error there.
PreparedStatement.query(target, params)
Section titled “PreparedStatement.query(target, params)”Run a prepared statement against a target, with the parameter values.
Remarks
Section titled “Remarks”-
A query that returns rows prepares into a
PreparedStatement, which you run withquery(...). Some queries return a row count instead of rows. End the query with.affectedCount(). Preparing one gives you aPreparedExecution, which you run withexecute(target, params). It resolves to{ affectedRows }. See the raw queries reference:TypeScript const archive = await runtime.prepare({ label: 'pg/text@1' }, (params) => db.raw.sql`UPDATE "tag" SET label = 'archived' WHERE label = ${params.label}`.affectedCount().build(), ); const { affectedRows } = await archive.execute(runtime, { label: 'stale' }); -
You must pass
target. It is the runtime, connection, or transaction the statement runs on. One prepared statement runs against any of them. -
A prepared statement has no
close()method and nothing to dispose. Hold one for as long as you like and reuse it.
Options
Section titled “Options”| Name | Type | Required | Description |
|---|---|---|---|
target |
Runtime, connection, or transaction |
Yes | Where to run the statement. |
params |
Object of the declared parameter values | Yes | The values for this execution. |
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
AsyncIterableResult<Row> |
await ps.query(runtime, { label }) |
The statement's rows. await the result for an array. |
A prepared ORM query from db.prepare(...) is different: its query(...) returns what the same ORM call returns, so a prepared first() gives a promise of one row or null.
Examples
Section titled “Examples”Run one prepared statement against a transaction and the runtime
Section titled “Run one prepared statement against a transaction and the runtime”import { withTransaction } from '@prisma/orm-postgres/family-runtime';
const ps = await runtime.prepare({ label: 'pg/text@1' }, (params) =>
db.sql.public.tag.select('id', 'label').where((f, fns) => fns.eq(f.label, params.label)).limit(1).build(),
);
const insertedId = await withTransaction(runtime, async (tx) => {
await tx.execute(db.sql.public.tag.insert([{ id: crypto.randomUUID(), label: 'ps-both-targets' }]).build());
const inTx = await ps.query(tx, { label: 'ps-both-targets' }); // runs against the transaction
return inTx[0]!.id;
});
const committed = await ps.query(runtime, { label: 'ps-both-targets' }); // runs against the runtime
// committed[0].id === insertedIdExecution options and results
Section titled “Execution options and results”runtime.query(...), runtime.execute(...), tx.query(...), tx.execute(...), connection.query(...), and connection.execute(...) all take a RuntimeExecuteOptions object as their second argument. A prepared statement takes it as a third argument, after target and params.
RuntimeExecuteOptions
Section titled “RuntimeExecuteOptions”Per-query options for cancellation.
Remarks
Section titled “Remarks”signalis anAbortSignalfor per-query cancellation. A signal that is already aborted when you callquery(...)rejects before any row is fetched, with an error whosecodeisRUNTIME.ABORTED. The error'scauseis the signal's reason, exactly as you passed it tocontroller.abort(...).- An abort that lands after rows have started arriving ends the stream with the same
RUNTIME.ABORTEDerror.details.phasesays where the abort landed. - Inside
db.transaction(...)the aborted statement throws, so your callback throws and the transaction rolls back.
Options
Section titled “Options”| Name | Type | Required | Description |
|---|---|---|---|
signal |
AbortSignal |
No | Per-query cancellation signal. |
scope |
'runtime' | 'connection' | 'transaction' |
No | A label middleware reads. The runtime sets it; setting it yourself changes only the label, not where the query runs. |
Examples
Section titled “Examples”A pre-aborted signal short-circuits
Section titled “A pre-aborted signal short-circuits”const controller = new AbortController();
controller.abort(new Error('cancelled'));
await runtime.query(db.sql.public.tag.select('id').limit(1).build(), {
signal: controller.signal,
});
// rejects with RUNTIME.ABORTEDruntime.telemetry()
Section titled “runtime.telemetry()”Read telemetry about the most recent query.
Remarks
Section titled “Remarks”- PostgreSQL only.
telemetry()does not exist on the MongoDB runtime. Read it after a query to log how long that query took and whether it succeeded. telemetry()returnsnullon a freshly-connected runtime, before any query has run. After a query, it returns an object of the shape{ lane, target: 'postgres', fingerprint, outcome, durationMs? }. The object reflects only the most recent query, not a running history.lanesays which API built the query, such asorm-clientfordb.ormorrawfordb.raw.sql.- Every run of the same query text shares one
fingerprint, whatever values you pass, so you can group runs of one query together. outcomeis'success'or'runtime-error'.durationMsis how long the query took. It is optional in the type, and it is set for every query the runtime ran.
Return type
Section titled “Return type”| Return type | Example | Description |
|---|---|---|
Telemetry object or null |
runtime.telemetry() |
The most recent query's telemetry, or null before any query. |
Examples
Section titled “Examples”Read telemetry before and after a query
Section titled “Read telemetry before and after a query”const before = runtime.telemetry(); // null
await runtime.query(db.sql.public.tag.select('id').limit(1).build());
const after = runtime.telemetry();
// { lane, target: 'postgres', fingerprint, outcome: 'success', durationMs? }AsyncIterableResult
Section titled “AsyncIterableResult”all(), createAll(), and the runtime's query(...) return an AsyncIterableResult. all() and query(...) read rows, and createAll() writes rows and returns the ones it wrote. await the result to collect an array, or for await it to take rows one at a time.
Use await, and use for await only to handle rows as they arrive. On PostgreSQL it does not reduce memory, because every row is loaded first.
Pick await or for await for a given result and do not mix the two. Calling .toArray() does the same thing as await. Re-awaiting a result you already awaited is safe and returns the same array, but switching between await and for await, or looping a second time with for await, throws an error whose code is RUNTIME.ITERATOR_CONSUMED. For the full rules on reading a result, shared identically by PostgreSQL and MongoDB, see AsyncIterableResult in the ORM client reference.