MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeWithTransaction

Function makeWithTransaction

packages/sql/src/internal/client.ts:110–183  ·  view source on GitHub ↗
(options: {
  readonly transactionTag: Context.Tag<I, readonly [conn: S, counter: number]>
  readonly spanAttributes: ReadonlyArray<readonly [string, unknown]>
  readonly acquireConnection: Effect.Effect<readonly [Scope.CloseableScope | undefined, S], Error.SqlError>
  readonly begin: (conn: NoInfer<S>) => Effect.Effect<void, Error.SqlError>
  readonly savepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, Error.SqlError>
  readonly commit: (conn: NoInfer<S>) => Effect.Effect<void, Error.SqlError>
  readonly rollback: (conn: NoInfer<S>) => Effect.Effect<void, Error.SqlError>
  readonly rollbackSavepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, Error.SqlError>
})

Source from the content-addressed store, hash-verified

108
109/** @internal */
110export const makeWithTransaction = <I, S>(options: {
111 readonly transactionTag: Context.Tag<I, readonly [conn: S, counter: number]>
112 readonly spanAttributes: ReadonlyArray<readonly [string, unknown]>
113 readonly acquireConnection: Effect.Effect<readonly [Scope.CloseableScope | undefined, S], Error.SqlError>
114 readonly begin: (conn: NoInfer<S>) => Effect.Effect<void, Error.SqlError>
115 readonly savepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, Error.SqlError>
116 readonly commit: (conn: NoInfer<S>) => Effect.Effect<void, Error.SqlError>
117 readonly rollback: (conn: NoInfer<S>) => Effect.Effect<void, Error.SqlError>
118 readonly rollbackSavepoint: (conn: NoInfer<S>, id: number) => Effect.Effect<void, Error.SqlError>
119}) =>
120<R, E, A>(
121 effect: Effect.Effect<A, E, R>
122): Effect.Effect<A, E | Error.SqlError, R> =>
123 Effect.uninterruptibleMask((restore) =>
124 Effect.useSpan(
125 "sql.transaction",
126 { kind: "client", captureStackTrace: false },
127 (span) =>
128 Effect.withFiberRuntime<A, E | Error.SqlError, R>((fiber) => {
129 for (const [key, value] of options.spanAttributes) {
130 span.attribute(key, value)
131 }
132 const context = fiber.currentContext
133 const clock = Context.get(fiber.currentDefaultServices, Clock.Clock)
134 const connOption = Context.getOption(context, options.transactionTag)
135 const conn = connOption._tag === "Some"
136 ? Effect.succeed([undefined, connOption.value[0]] as const)
137 : options.acquireConnection
138 const id = connOption._tag === "Some" ? connOption.value[1] + 1 : 0
139 return Effect.flatMap(
140 conn,
141 (
142 [scope, conn]
143 ) =>
144 (id === 0 ? options.begin(conn) : options.savepoint(conn, id)).pipe(
145 // nothing to roll back if the transaction could not begin
146 Effect.onError((cause) =>
147 scope !== undefined ? Scope.close(scope, Exit.failCause(cause)) : Effect.void
148 ),
149 Effect.zipRight(
150 Effect.locally(
151 restore(effect),
152 FiberRef.currentContext,
153 Context.add(context, options.transactionTag, [conn, id]).pipe(
154 Context.add(Tracer.ParentSpan, span)
155 )
156 ).pipe(Effect.exit)
157 ),
158 Effect.flatMap((exit) => {
159 let effect: Effect.Effect<void>
160 if (Exit.isSuccess(exit)) {
161 if (id === 0) {
162 span.event("db.transaction.commit", clock.unsafeCurrentTimeNanos())
163 effect = Effect.orDie(options.commit(conn))
164 } else {
165 span.event("db.transaction.savepoint", clock.unsafeCurrentTimeNanos())
166 effect = Effect.void
167 }

Callers 1

makeFunction · 0.85

Calls 13

restoreFunction · 0.85
onErrorMethod · 0.80
failCauseMethod · 0.80
rollbackMethod · 0.80
attributeMethod · 0.65
getMethod · 0.65
getOptionMethod · 0.65
pipeMethod · 0.65
closeMethod · 0.65
addMethod · 0.65
eventMethod · 0.65

Tested by

no test coverage detected