(
options: PgClientConfig & {
/**
* Whether to acquire a separate client for each sql.stream / sql.listen
*/
readonly acquireForStream?: boolean | undefined
}
)
| 213 | * @since 4.0.0 |
| 214 | */ |
| 215 | export const makeClient = ( |
| 216 | options: PgClientConfig & { |
| 217 | /** |
| 218 | * Whether to acquire a separate client for each sql.stream / sql.listen |
| 219 | */ |
| 220 | readonly acquireForStream?: boolean | undefined |
| 221 | } |
| 222 | ): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> => { |
| 223 | function onError() {} |
| 224 | return fromClient({ |
| 225 | ...options, |
| 226 | acquire: Effect.acquireRelease( |
| 227 | Effect.tryPromise({ |
| 228 | try: async () => { |
| 229 | const client = new Pg.Client({ |
| 230 | connectionString: options.url ? Redacted.value(options.url) : undefined, |
| 231 | user: options.username, |
| 232 | host: options.host, |
| 233 | database: options.database, |
| 234 | password: options.password ? Redacted.value(options.password) : undefined, |
| 235 | ssl: options.ssl, |
| 236 | port: options.port, |
| 237 | ...(options.stream ? { stream: options.stream } : {}), |
| 238 | application_name: options.applicationName ?? "@effect/sql-pg", |
| 239 | types: options.types |
| 240 | }) |
| 241 | client.on("error", onError) |
| 242 | await client.connect() |
| 243 | return client |
| 244 | }, |
| 245 | catch: (cause) => new SqlError({ reason: classifyError(cause, "PgClient: Failed to connect", "connect") }) |
| 246 | }), |
| 247 | (client) => |
| 248 | Effect.promise(() => { |
| 249 | client.off("error", onError) |
| 250 | return client.end() |
| 251 | }).pipe( |
| 252 | Effect.timeoutOption(1000) |
| 253 | ), |
| 254 | { interruptible: true } |
| 255 | ).pipe( |
| 256 | Effect.timeoutOrElse({ |
| 257 | duration: options.connectTimeout ?? Duration.seconds(5), |
| 258 | orElse: () => |
| 259 | Effect.fail( |
| 260 | new SqlError({ |
| 261 | reason: new ConnectionError({ |
| 262 | cause: new Error("Connection timed out"), |
| 263 | message: "PgClient: Connection timed out", |
| 264 | operation: "connect" |
| 265 | }) |
| 266 | }) |
| 267 | ) |
| 268 | }) |
| 269 | ), |
| 270 | acquireForStream: options.acquireForStream ?? false |
no test coverage detected
searching dependent graphs…