MCPcopy Create free account
hub / github.com/effect-app/libs / makeConection

Function makeConection

repos/effect/packages/sql/pg/src/PgClient.ts:290–355  ·  view source on GitHub ↗
(client?: Pg.PoolClient)

Source from the content-addressed store, hash-verified

288 readonly transformQueryNames?: ((str: string) => string) | undefined
289 readonly transformJson?: boolean | undefined
290 readonly types?: Pg.CustomTypesConfig | undefined
291 }
292): Effect.fn.Return<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> {
293 const pool = yield* options.acquire
294
295 const makeConection = (client?: Pg.PoolClient) =>
296 new ConnectionImpl(
297 function runWithClient<A>(f: (client: Pg.ClientBase, resume: (_: Effect.Effect<A, SqlError>) => void) => void) {
298 if (client !== undefined) {
299 return Effect.callback<A, SqlError>((resume) => {
300 f(client!, resume)
301 return makeCancel(pool, client!)
302 })
303 }
304 return Effect.callback<A, SqlError>((resume) => {
305 let done = false
306 let cancel: Effect.Effect<void> | undefined = undefined
307 let client: Pg.PoolClient | undefined = undefined
308 function onError(cause: Error) {
309 cleanup(cause)
310 resume(Effect.fail(new SqlError({ reason: classifyError(cause, "Connection error", "acquireConnection") })))
311 }
312 function cleanup(cause?: Error) {
313 if (!done) client?.release(cause)
314 done = true
315 client?.off("error", onError)
316 }
317 pool.connect((cause, client_) => {
318 if (cause) {
319 return resume(
320 Effect.fail(
321 new SqlError({
322 reason: classifyError(cause, "Failed to acquire connection", "acquireConnection")
323 })
324 )
325 )
326 } else if (!client_) {
327 return resume(
328 Effect.fail(
329 new SqlError({
330 reason: new ConnectionError({
331 message: "Failed to acquire connection",
332 cause: new Error("No client returned"),
333 operation: "acquireConnection"
334 })
335 })
336 )
337 )
338 } else if (done) {
339 client_.release()
340 return
341 }
342 client = client_
343 client.once("error", onError)
344 cancel = makeCancel(pool, client)
345 f(client, (eff) => {
346 cleanup()
347 resume(eff)

Callers 1

PgClient.tsFile · 0.85

Calls 11

makeCancelFunction · 0.85
connectMethod · 0.80
onceMethod · 0.80
syncMethod · 0.80
classifyErrorFunction · 0.70
cleanupFunction · 0.70
releaseMethod · 0.65
fFunction · 0.50
resumeFunction · 0.50
failMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…