| 397 | * @since 1.0.0 |
| 398 | */ |
| 399 | export 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( |
| 431 | Effect.tryPromise({ |
| 432 | try: () => pool.query("SELECT 1"), |
| 433 | catch: (cause) => new SqlError({ cause, message: "PgClient: Failed to connect" }) |
| 434 | }), |
| 435 | () => |
| 436 | Effect.promise(() => pool.end()).pipe( |
| 437 | Effect.interruptible, |
| 438 | Effect.timeoutOption(1000) |
| 439 | ) |
| 440 | ).pipe( |
| 441 | Effect.timeoutFail({ |
| 442 | duration: options.connectTimeout ?? Duration.seconds(5), |
| 443 | onTimeout: () => |
| 444 | new SqlError({ |
| 445 | cause: new Error("Connection timed out"), |
| 446 | message: "PgClient: Connection timed out" |
| 447 | }) |
| 448 | }) |
| 449 | ) |
| 450 | |
| 451 | let config = options |
| 452 | if (pool.options.connectionString) { |
| 453 | try { |
| 454 | const parsed = PgConnString.parse(pool.options.connectionString) |
| 455 | config = { |
| 456 | ...config, |