Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

1.4 KB
Raw Blame
import { EventEmitter } from 'node:events'
import { PostgresDatabase } from '../database.js'

export class ScriptedPoolClient extends EventEmitter {
  readonly statements: string[] = []
  release: () => void = () => undefined

  // Resolves each statement only once `gate` does, so a test can hold one mid-flight.
  constructor(private readonly gate: () => Promise<void> = async () => undefined) {
    super()
  }

  query = async (sql: string | { text: string }) => {
    this.statements.push(typeof sql === 'string' ? sql : sql.text)
    await this.gate()
    return { rows: [], rowCount: 0 }
  }
}

// Hands out the listed clients in order, reusing an object the way pg-pool reuses a connection.
export function scriptedPostgresDatabase(
  checkouts: ScriptedPoolClient[],
  beforeConnect: () => void | Promise<void> = () => undefined
): PostgresDatabase {
  const pool = {
    totalCount: checkouts.length,
    idleCount: 0,
    waitingCount: 0,
    connect: async () => {
      await beforeConnect()
      const client = checkouts.shift()
      if (!client) throw new Error('scripted_pool_exhausted')
      // pg-pool assigns a fresh release on every checkout.
      client.release = () => undefined
      return client
    }
  }
  // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: PostgresDatabase and its pool pressure read only connect, these counts and the optional options.
  return new PostgresDatabase(pool as never)
}