| 147 | * @since 4.0.0 |
| 148 | */ |
| 149 | export const make = (options: PgPoolConfig): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 150 | fromPool({ |
| 151 | ...options, |
| 152 | acquire: Effect.gen(function*() { |
| 153 | const pool = new Pg.Pool({ |
| 154 | connectionString: options.url ? Redacted.value(options.url) : undefined, |
| 155 | user: options.username, |
| 156 | host: options.host, |
| 157 | database: options.database, |
| 158 | password: options.password ? Redacted.value(options.password) : undefined, |
| 159 | ssl: options.ssl, |
| 160 | port: options.port, |
| 161 | ...(options.stream ? { stream: options.stream } : {}), |
| 162 | connectionTimeoutMillis: options.connectTimeout |
| 163 | ? Duration.toMillis(Duration.fromInputUnsafe(options.connectTimeout)) |
| 164 | : undefined, |
| 165 | idleTimeoutMillis: options.idleTimeout |
| 166 | ? Duration.toMillis(Duration.fromInputUnsafe(options.idleTimeout)) |
| 167 | : undefined, |
| 168 | max: options.maxConnections, |
| 169 | min: options.minConnections, |
| 170 | maxLifetimeSeconds: options.connectionTTL |
| 171 | ? Duration.toSeconds(Duration.fromInputUnsafe(options.connectionTTL)) |
| 172 | : undefined, |
| 173 | application_name: options.applicationName ?? "@effect/sql-pg", |
| 174 | types: options.types |
| 175 | }) |
| 176 | |
| 177 | pool.on("error", (_err) => {}) |
| 178 | |
| 179 | yield* Effect.acquireRelease( |
| 180 | Effect.tryPromise({ |
| 181 | try: () => pool.query("SELECT 1"), |
| 182 | catch: (cause) => new SqlError({ reason: classifyError(cause, "PgClient: Failed to connect", "connect") }) |
| 183 | }), |
| 184 | () => |
| 185 | Effect.promise(() => pool.end()).pipe( |
| 186 | Effect.timeoutOption(1000) |
| 187 | ), |
| 188 | { interruptible: true } |
| 189 | ).pipe( |
| 190 | Effect.timeoutOrElse({ |
| 191 | duration: options.connectTimeout ?? Duration.seconds(5), |
| 192 | orElse: () => |
| 193 | Effect.fail( |
| 194 | new SqlError({ |
| 195 | reason: new ConnectionError({ |
| 196 | cause: new Error("Connection timed out"), |
| 197 | message: "PgClient: Connection timed out", |
| 198 | operation: "connect" |
| 199 | }) |
| 200 | }) |
| 201 | ) |
| 202 | }) |
| 203 | ) |
| 204 | |
| 205 | return pool |
| 206 | }) |