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

Function make

packages/sql/pg/src/PgConnection.ts:393–462  ·  view source on GitHub ↗
(options: Config)

Source from the content-addressed store, hash-verified

391 * @since 4.0.0
392 */
393export const make = (options: Config): Effect.Effect<PgConnection, SqlError, Scope.Scope> =>
394 Effect.flatMap(resolveConfig(options), (config) =>
395 Effect.withFiber((fiber) => {
396 const failure: ConnectFailure = {}
397 const attempt = Effect.acquireRelease(
398 Effect.gen(function*() {
399 const password = yield* PasswordInternal.resolve(config.password)
400 const session = yield* connect(config, password, failure)
401 return new PgConnectionImpl(options, config, session, options.types)
402 }),
403 (connection) => Effect.sync(() => connection.closeUnsafe()),
404 { interruptible: true }
405 ).pipe(
406 Effect.timeoutOrElse({
407 duration: config.connectTimeout,
408 orElse: () => {
409 failure.type = "timeout"
410 failure.sqlState = undefined
411 return Effect.fail(
412 new SqlError({
413 reason: new ConnectionError({
414 cause: new Error("Connection timed out"),
415 message: "PgConnection: Connection timed out",
416 operation: "connect"
417 })
418 })
419 )
420 }
421 }),
422 // Let nested connections inherit their caller's span.
423 Effect.provideService(rootConnectSpan, false)
424 )
425 if (!fiber.cache.tracerEnabled) return attempt
426
427 const clock = fiber.getRef(Clock.Clock)
428 const timingEnabled = fiber.getRef(TracerTimingEnabled)
429 return Effect.uninterruptibleMask((restore) =>
430 Effect.flatMap(
431 Effect.makeSpan("sql.connect", { kind: "client", root: fiber.getRef(rootConnectSpan) }),
432 (span) => {
433 span.attribute("db.system.name", "postgresql")
434 span.attribute("db.namespace", config.startupParameters.database ?? config.username)
435 if (config.path === undefined && config.stream === undefined) {
436 span.attribute("server.address", config.host)
437 span.attribute("server.port", config.port)
438 }
439 return Effect.onExit(restore(Effect.withParentSpan(attempt, span)), (exit) => {
440 // Keep raw error text out of the span's exit.
441 let spanExit: Exit.Exit<unknown, unknown> = Exit.void
442 if (Exit.isSuccess(exit)) {
443 exit.value[internalsKey].connectSpan = Tracer.externalSpan({
444 spanId: span.spanId,
445 traceId: span.traceId,
446 sampled: span.sampled
447 })
448 } else if (Cause.hasInterruptsOnly(exit.cause)) {
449 spanExit = exit
450 } else {

Callers

nothing calls this directly

Calls 13

resolveConfigFunction · 0.90
resolveMethod · 0.80
getRefMethod · 0.80
onExitMethod · 0.80
connectFunction · 0.70
pipeMethod · 0.65
closeUnsafeMethod · 0.65
failMethod · 0.65
attributeMethod · 0.65
endMethod · 0.65
restoreFunction · 0.50

Tested by

no test coverage detected