( options: MysqlClientConfig )
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Creates a scoped MySQL client backed by a managed mysql2 pool, verifying connectivity and supporting streaming queries through mysql2 query streams. |
| 213 | * |
| 214 | * @category constructors |
| 215 | * @since 4.0.0 |
| 216 | */ |
| 217 | export const make = ( |
| 218 | options: MysqlClientConfig |
| 219 | ): Effect.Effect<MysqlClient, SqlError, Scope | Reactivity.Reactivity> => |
| 220 | Effect.gen(function*() { |
| 221 | const compiler = makeCompiler(options.transformQueryNames) |
| 222 | const transformRows = options.transformResultNames ? |
| 223 | Statement.defaultTransforms( |
| 224 | options.transformResultNames |
| 225 | ).array : |
| 226 | undefined |
| 227 | const defaultMethod: "execute" | "query" = options.disablePreparedStatements === true ? "query" : "execute" |
| 228 | |
| 229 | class ConnectionImpl implements Connection { |
| 230 | readonly conn: Mysql.PoolConnection | Mysql.Pool |
| 231 | constructor(conn: Mysql.PoolConnection | Mysql.Pool) { |
| 232 | this.conn = conn |
| 233 | } |
| 234 | |
| 235 | private runRaw( |
| 236 | sql: string, |
| 237 | values?: ReadonlyArray<any>, |
| 238 | rowsAsArray = false, |
| 239 | method: "execute" | "query" = defaultMethod |
| 240 | ) { |
| 241 | return Effect.callback<unknown, SqlError>((resume) => { |
| 242 | const operation = method === "query" ? "executeUnprepared" : "execute" |
| 243 | ;(this.conn as any)[method]({ |
| 244 | sql, |
| 245 | values, |
| 246 | rowsAsArray |
| 247 | }, (cause: unknown | null, results: unknown, _fields: any) => { |
| 248 | if (cause) { |
| 249 | resume( |
| 250 | Effect.fail(new SqlError({ reason: classifyError(cause, "Failed to execute statement", operation) })) |
| 251 | ) |
| 252 | } else { |
| 253 | resume(Effect.succeed(results)) |
| 254 | } |
| 255 | }) |
| 256 | }) |
| 257 | } |
| 258 | |
| 259 | private run( |
| 260 | sql: string, |
| 261 | values?: ReadonlyArray<any>, |
| 262 | rowsAsArray = false, |
| 263 | method: "execute" | "query" = defaultMethod |
| 264 | ) { |
| 265 | return this.runRaw(sql, values, rowsAsArray, method).pipe( |
| 266 | Effect.map((results) => Array.isArray(results) ? results : []) |
| 267 | ) |
| 268 | } |
no test coverage detected
searching dependent graphs…