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

Function make

packages/sql-pg/src/PgClient.ts:399–468  ·  view source on GitHub ↗
(
  options: PgClientConfig
)

Source from the content-addressed store, hash-verified

397 * @since 1.0.0
398 */
399export const make = (
400 options: PgClientConfig
401): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> =>
402 Effect.gen(function*() {
403 const pool = new Pg.Pool({
404 connectionString: options.url ? Redacted.value(options.url) : undefined,
405 user: options.username,
406 host: options.host,
407 database: options.database,
408 password: options.password ? Redacted.value(options.password) : undefined,
409 ssl: options.ssl,
410 port: options.port,
411 ...(options.stream ? { stream: options.stream } : {}),
412 connectionTimeoutMillis: options.connectTimeout
413 ? Duration.toMillis(options.connectTimeout)
414 : undefined,
415 idleTimeoutMillis: options.idleTimeout
416 ? Duration.toMillis(options.idleTimeout)
417 : undefined,
418 max: options.maxConnections,
419 min: options.minConnections,
420 maxLifetimeSeconds: options.connectionTTL
421 ? Duration.toSeconds(options.connectionTTL)
422 : undefined,
423 application_name: options.applicationName ?? "@effect/sql-pg",
424 types: options.types
425 })
426
427 pool.on("error", (_err) => {
428 })
429
430 yield* Effect.acquireRelease(Effect.succeed(pool), () =>
431 Effect.promise(() => pool.end()).pipe(
432 Effect.interruptible,
433 Effect.timeoutOption(1000)
434 ))
435
436 yield* Effect.tryPromise({
437 try: () => pool.query("SELECT 1"),
438 catch: (cause) => new SqlError({ cause, message: "PgClient: Failed to connect" })
439 }).pipe(
440 Effect.timeoutFail({
441 duration: options.connectTimeout ?? Duration.seconds(5),
442 onTimeout: () =>
443 new SqlError({
444 cause: new Error("Connection timed out"),
445 message: "PgClient: Connection timed out"
446 })
447 })
448 )
449
450 let config = options
451 if (pool.options.connectionString) {
452 try {
453 const parsed = PgConnString.parse(pool.options.connectionString)
454 config = {
455 ...config,
456 host: config.host ?? parsed.host ?? undefined,

Callers 1

layerFunction · 0.70

Calls 6

parseMethod · 0.80
makeClientFunction · 0.70
pipeMethod · 0.65
endMethod · 0.65
makeMethod · 0.65
valueMethod · 0.45

Tested by

no test coverage detected