# withTransaction() function · @imqueue/pg-prisma

Source: https://imqueue.org/api/pg-prisma/latest/pg-prisma.withtransaction/
Published: 2026-08-28
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/pg-prisma 2.0.0 — generated reference, not hand-written

Run `fn` inside a transaction on one connection.

**Signature:**

```typescript
export declare function withTransaction<T>(pool: SqlPool, fn: (tx: SqlExecutor) => Promise<T>): Promise<T>;
```

## Parameters


| Parameter | Type | Description |
| --- | --- | --- |
| pool | [SqlPool](https://imqueue.org/api/pg-prisma/latest/pg-prisma.sqlpool/) | The pool to take a connection from. |
| fn | (tx: [SqlExecutor](https://imqueue.org/api/pg-prisma/latest/pg-prisma.sqlexecutor/)) => Promise<T> | The work to run inside the transaction. |


**Returns:**

Promise<T>

Whatever `fn` returns.

## Remarks

A transaction has to be one connection: issuing `BEGIN` on a pool and the statements after it on whatever connection the pool hands out next is the classic way to commit nothing and report success. The connection is released whatever happens, and a failure rolls back before rethrowing.

## Example


```typescript
await withTransaction(pool, async tx => {
    await tx.query('CREATE TABLE ...');
});
```

