MCPcopy Create free account
hub / github.com/effect-app/libs / make

Function make

repos/effect/packages/sql/mysql2/src/MysqlClient.ts:211–410  ·  view source on GitHub ↗
(
  options: MysqlClientConfig
)

Source from the content-addressed store, hash-verified

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 */
217export 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 }

Callers 1

layerFunction · 0.70

Calls 13

syncMethod · 0.80
pushMethod · 0.80
makeCompilerFunction · 0.70
classifyErrorFunction · 0.70
valueMethod · 0.65
pipeMethod · 0.65
endMethod · 0.65
releaseMethod · 0.65
mapMethod · 0.65
makeMethod · 0.65
resumeFunction · 0.50
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…