(options: {
readonly transactionService: Context.Key<I, readonly [conn: S, counter: number]>
readonly spanAttributes: ReadonlyArray<readonly [string, unknown]>
/**
* Acquires the connection for a top-level transaction. Runs under the
* transaction's uninterruptible mask; wrap the wait for a connection in
* `restore` so a queued transaction can be interrupted, and register
* releases on the provided scope. The scope closes with the transaction, or
* immediately when acquisition fails or is interrupted.
*/
readonly acquireConnection: (
restore: <XA, XE, XR>(effect: Effect.Effect<XA, XE, XR>) => Effect.Effect<XA, XE, XR>
) => Effect.Effect<S, SqlError, Scope.Scope>
/**
* Starts a top-level transaction. `statement` is the `BeginTransaction`
* provided to the transaction, if any; a driver that doesn't start
* transactions with a SQL statement ignores it.
*/
readonly begin: (conn: NoInfer<S>, statement: string | undefined) => Effect.Effect<void, SqlError>
readonly savepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, SqlError>
/**
* Releases a nested savepoint after success or a successful rollback.
* Omit to leave savepoints unreleased, including for dialects such as MSSQL
* that do not support releasing savepoints.
*/
readonly releaseSavepoint?: ((conn: NoInfer<S>, id: number) => Effect.Effect<void, SqlError>) | undefined
readonly commit: (conn: NoInfer<S>) => Effect.Effect<void, SqlError>
/** Driver-specific recovery before the connection is released after a failed COMMIT. */
readonly onCommitFailure?: ((conn: NoInfer<S>) => Effect.Effect<void, SqlError>) | undefined
readonly rollback: (conn: NoInfer<S>) => Effect.Effect<void, SqlError>
readonly rollbackSavepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, SqlError>
})
| 695 | * @since 4.0.0 |
| 696 | */ |
| 697 | export const makeWithTransaction = <I, S>(options: { |
| 698 | readonly transactionService: Context.Key<I, readonly [conn: S, counter: number]> |
| 699 | readonly spanAttributes: ReadonlyArray<readonly [string, unknown]> |
| 700 | /** |
| 701 | * Acquires the connection for a top-level transaction. Runs under the |
| 702 | * transaction's uninterruptible mask; wrap the wait for a connection in |
| 703 | * `restore` so a queued transaction can be interrupted, and register |
| 704 | * releases on the provided scope. The scope closes with the transaction, or |
| 705 | * immediately when acquisition fails or is interrupted. |
| 706 | */ |
| 707 | readonly acquireConnection: ( |
| 708 | restore: <XA, XE, XR>(effect: Effect.Effect<XA, XE, XR>) => Effect.Effect<XA, XE, XR> |
| 709 | ) => Effect.Effect<S, SqlError, Scope.Scope> |
| 710 | /** |
| 711 | * Starts a top-level transaction. `statement` is the `BeginTransaction` |
| 712 | * provided to the transaction, if any; a driver that doesn't start |
| 713 | * transactions with a SQL statement ignores it. |
| 714 | */ |
| 715 | readonly begin: (conn: NoInfer<S>, statement: string | undefined) => Effect.Effect<void, SqlError> |
| 716 | readonly savepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, SqlError> |
| 717 | /** |
| 718 | * Releases a nested savepoint after success or a successful rollback. |
| 719 | * Omit to leave savepoints unreleased, including for dialects such as MSSQL |
| 720 | * that do not support releasing savepoints. |
| 721 | */ |
| 722 | readonly releaseSavepoint?: ((conn: NoInfer<S>, id: number) => Effect.Effect<void, SqlError>) | undefined |
| 723 | readonly commit: (conn: NoInfer<S>) => Effect.Effect<void, SqlError> |
| 724 | /** Driver-specific recovery before the connection is released after a failed COMMIT. */ |
| 725 | readonly onCommitFailure?: ((conn: NoInfer<S>) => Effect.Effect<void, SqlError>) | undefined |
| 726 | readonly rollback: (conn: NoInfer<S>) => Effect.Effect<void, SqlError> |
| 727 | readonly rollbackSavepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, SqlError> |
| 728 | }) => { |
| 729 | const transactionSemaphore = Context.Service<Semaphore.Semaphore>( |
| 730 | `effect/sql/SqlClient/TransactionSemaphore/${transactionSemaphoreIdCounter++}` |
| 731 | ) |
| 732 | const afterCommit = makeAfterCommit(options.transactionService) |
| 733 | const target = Statement.spanTarget(options.spanAttributes) |
| 734 | const deferInvalidations = (reactivity: Reactivity): Reactivity => { |
| 735 | const invalidate = (keys: Parameters<Reactivity["invalidate"]>[0]) => |
| 736 | afterCommit(Effect.sync(() => reactivity.invalidateUnsafe(keys))) |
| 737 | return { ...reactivity, invalidate, mutation: (keys, effect) => Effect.tap(effect, invalidate(keys)) } |
| 738 | } |
| 739 | return <R, E, A>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E | SqlError, R> => |
| 740 | Effect.uninterruptibleMask((restore) => |
| 741 | Effect.useSpan( |
| 742 | "sql.transaction", |
| 743 | { kind: "internal" }, |
| 744 | (span) => |
| 745 | Effect.withFiber<A, E | SqlError, R>((fiber) => { |
| 746 | for (const [key, value] of options.spanAttributes) { |
| 747 | span.attribute(key, value) |
| 748 | } |
| 749 | const services = fiber.context |
| 750 | const reactivity = Context.getOrUndefined(services, Reactivity) |
| 751 | const propagate = fiber.cache.tracerEnabled && fiber.getRef(Statement.SpanPropagationEnabled) |
| 752 | const waitedClock = propagate ? waitClock(fiber) : undefined |
| 753 | // Runs a transaction control in a span of its own, so a failure ends |
| 754 | // that span with the control's own exit. |
no test coverage detected