| 104 | } |
| 105 | |
| 106 | const makeClient = ( |
| 107 | pool: Pg.Pool, |
| 108 | config: PgClientConfig, |
| 109 | options: ClientOptions |
| 110 | ): Effect.Effect<PgClient, SqlError, Scope.Scope | Reactivity.Reactivity> => |
| 111 | Effect.gen(function*() { |
| 112 | const compiler = makeCompiler( |
| 113 | options.transformQueryNames, |
| 114 | options.transformJson |
| 115 | ) |
| 116 | const transformRows = options.transformResultNames ? |
| 117 | Statement.defaultTransforms( |
| 118 | options.transformResultNames, |
| 119 | options.transformJson |
| 120 | ).array : |
| 121 | undefined |
| 122 | |
| 123 | class ConnectionImpl implements Connection { |
| 124 | readonly pg: Pg.PoolClient | undefined |
| 125 | constructor(pg?: Pg.PoolClient) { |
| 126 | this.pg = pg |
| 127 | } |
| 128 | |
| 129 | private runWithClient<A>(f: (client: Pg.PoolClient, resume: (_: Effect.Effect<A, SqlError>) => void) => void) { |
| 130 | if (this.pg !== undefined) { |
| 131 | return Effect.async<A, SqlError>((resume) => { |
| 132 | f(this.pg!, resume) |
| 133 | return makeCancel(pool, this.pg!) |
| 134 | }) |
| 135 | } |
| 136 | return Effect.async<A, SqlError>((resume) => { |
| 137 | let done = false |
| 138 | let cancel: Effect.Effect<void> | undefined = undefined |
| 139 | let client: Pg.PoolClient | undefined = undefined |
| 140 | function onError(cause: Error) { |
| 141 | cleanup(cause) |
| 142 | resume(Effect.fail(new SqlError({ cause, message: "Connection error" }))) |
| 143 | } |
| 144 | function cleanup(cause?: Error) { |
| 145 | if (!done) client?.release(cause) |
| 146 | done = true |
| 147 | client?.off("error", onError) |
| 148 | } |
| 149 | pool.connect((cause, client_) => { |
| 150 | if (cause) { |
| 151 | return resume(Effect.fail(new SqlError({ cause, message: "Failed to acquire connection" }))) |
| 152 | } else if (!client_) { |
| 153 | return resume( |
| 154 | Effect.fail( |
| 155 | new SqlError({ message: "Failed to acquire connection", cause: new Error("No client returned") }) |
| 156 | ) |
| 157 | ) |
| 158 | } else if (done) { |
| 159 | client_.release() |
| 160 | return |
| 161 | } |
| 162 | client = client_ |
| 163 | client.once("error", onError) |