MCPcopy Create free account
hub / github.com/Effect-TS/effect / make

Function make

packages/sql/mysql2/src/MysqlClient.ts:217–417  ·  view source on GitHub ↗
(
  options: MysqlClientConfig
)

Source from the content-addressed store, hash-verified

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 }
269
270 execute(
271 sql: string,
272 params: ReadonlyArray<unknown>,
273 transformRows: (<A extends object>(row: ReadonlyArray<A>) => ReadonlyArray<A>) | undefined
274 ) {

Callers 1

layerFunction · 0.70

Calls 14

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

Tested by

no test coverage detected