Transactions
Add the SQL integration to the server's middleware array to run every valid pipeline in one Effect SQL transaction:
import * as PipelineSql from "effect-rpc-pipeline/Sql"
const RpcRoutes = PipelineServer.layerHttp({
group: AppRpcs,
path: "/rpc",
middleware: [PipelineSql.transaction()]
}).pipe(
Layer.provide(AppLive),
Layer.provide(SqlLive)
)A successful graph commits once. Typed failures, defects, and interruption roll back through Effect’s sql.withTransaction. Nested Effect SQL transactions keep their normal savepoint behavior.
Graph validation happens before acquiring a connection or starting a transaction.
Choose which graphs need a transaction#
The default is to transact every graph on the route. Use when to select graphs explicitly:
middleware: [
PipelineSql.transaction({
when: ({ rpcTags }) => rpcTags.some((tag) =>
tag === "createTodo" || tag === "updateTodo" || tag === "deleteTodo"
)
})
]Choose mutation tags explicitly in application code. For dynamic capability calls or mixed workloads, use an unconditional transaction or put sql.withTransaction inside the handler that needs it.
Reverse capability calls are rejected while the transaction is active, so SQL locks cannot be held while waiting for a remote peer. Complete the transaction before starting an interactive callback workflow.
Run effects after a successful commit#
Register best-effort follow-up work from a transactional handler with PipelineSql.afterCommit:
createPost: (input) => Effect.gen(function*() {
const post = yield* Posts.create(input)
yield* PipelineSql.afterCommit(
PostEvents.publish({ _tag: "PostCreated", postId: post.id })
)
return post
})The effects run sequentially after the outer pipeline transaction commits. Rollback discards them. They use the outer service context after the transaction connection is released.
Read invocation-local values such as CurrentUser before registering the effect, and pass the needed data into it.
A failure is logged by default and cannot undo the committed mutation. Use onAfterCommitFailure to customize reporting.
Use this hook for best-effort notifications, cache invalidation, or telemetry that must wait for commit. For work that must survive a process crash or be retried reliably, use a transactional outbox.