Batch related operations
An HTTP or edge request needs several database reads or writes grouped into one unit of work.
Neon · Function
transaction() executes multiple Neon SQL queries together in one non-interactive transaction. Call it on the query function returned by neon() and provide an array of queries or a synchronous query-producing callback.
Call transaction() on the query function returned by neon(). Pass an array of tagged-template queries, or a non-async function that receives a query function and returns an array. Neon executes the queries together in one non-interactive transaction and returns their results in the corresponding order.
transaction(queriesOrFn, options)| Name | Type | Description |
|---|---|---|
queriesOrFn | Query[] | (sql: NeonQueryFunction) => Query[] | An array of queries, or a non-async function receiving a query function and returning an array of queries. |
options | TransactionOptions | Optional query and transaction configuration. |
A promise resolving to the query results in the same order as the supplied queries.
neon(), invoke its transaction() method.transaction(), rather than attaching them to individual queries.isolationLevel, use one of the four documented PostgreSQL isolation-level values.deferrable requires both a read-only transaction and Serializable isolation.import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
const [posts, tags] = await sql.transaction([
sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${10}`,
sql`SELECT * FROM tags`
]);
console.log({ posts, tags });Runs two parameterized queries together as one non-interactive transaction.
An HTTP or edge request needs several database reads or writes grouped into one unit of work.
A request must retrieve independent result sets while minimizing separate database round trips.
A write workflow needs all included statements to participate in the same non-interactive transaction.
transaction() function do?transaction() is a property of the query function returned by neon(). It accepts an array of queries or a non-async callback that returns an array, and executes those queries in one non-interactive transaction.
Follow-up: When should WebSockets be used instead? Use the WebSocket driver for interactive transactions or session-oriented work; HTTP transactions are non-interactive.
transaction()?The array form lets you issue multiple queries together, and the returned results correspond to the queries in array order. The queries should be supplied as tagged-template expressions from the Neon query function.
Follow-up: How can transaction behavior be configured? Pass an options object as the second argument, including isolation, read-only, or deferrable settings.
The second argument accepts the ordinary query options plus transaction-specific settings. Those settings include isolationLevel, readOnly, and deferrable.
Follow-up: What constraint applies to deferrable? It is effective only when readOnly is true and the isolation level is Serializable.
neon()Neon serverless driverisolationLevelreadOnlydeferrable