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

Function queryStream

repos/effect/packages/sql/mysql2/src/MysqlClient.ts:475–520  ·  view source on GitHub ↗
(
  conn: Mysql.PoolConnection,
  sql: string,
  params?: ReadonlyArray<any>
)

Source from the content-addressed store, hash-verified

473 return ["", []]
474 },
475 onRecordUpdate() {
476 return ["", []]
477 }
478 })
479
480const escape = Statement.defaultEscape("`")
481
482function queryStream(
483 conn: Mysql.PoolConnection,
484 sql: string,
485 params?: ReadonlyArray<any>
486) {
487 return asyncPauseResume<any, SqlError>(Effect.fnUntraced(function*(emit) {
488 const query = (conn as any).query(sql, params).stream()
489 yield* Effect.addFinalizer(() => Effect.sync(() => query.destroy() as void))
490
491 let buffer: Array<any> = []
492 let taskPending = false
493 query.on(
494 "error",
495 (cause: unknown) =>
496 emit.fail(new SqlError({ reason: classifyError(cause, "Failed to stream statement", "stream") }))
497 )
498 query.on("data", (row: any) => {
499 buffer.push(row)
500 if (!taskPending) {
501 taskPending = true
502 queueMicrotask(() => {
503 const items = buffer
504 buffer = []
505 emit.array(items)
506 taskPending = false
507 })
508 }
509 })
510 query.on("end", () => {
511 if (buffer.length > 0) {
512 emit.array(buffer)
513 buffer = []
514 }
515 emit.end()
516 })
517
518 return {
519 onPause() {
520 query.pause()
521 },
522 onResume() {
523 query.resume()

Callers 1

executeStreamMethod · 0.85

Calls 9

asyncPauseResumeFunction · 0.90
addFinalizerMethod · 0.80
syncMethod · 0.80
pushMethod · 0.80
classifyErrorFunction · 0.70
streamMethod · 0.65
endMethod · 0.65
onMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…