import Builder from "./Builder"; import Binding from "./Library"; import { encodeRequest, decodeRows, decodeResponse } from "./Protocol"; import type { ExecResult } from "./Protocol"; type Options = { database: string; }; /** * Allows to use SQLite databases in a NodeJS process. */ export default class CBuilder< I extends abstract new (...args: any) => any, > extends Builder { constructor(opts: Options) { super({ database: opts.database, url: "" }); } /** * TODO: handle responses */ async *Rows>(): AsyncGenerator { this.formatRequest(); const req = Buffer.from(encodeRequest(this.request)); const iter = Binding.rowIterator(req); for (;;) { const response = iter.next(); const rows = decodeRows(response); if (rows.length === 0) { iter.cleanup(); break; } for (const row of rows) { if (this.headerRow === null) { this.headerRow = row.r; continue; } yield this.formatRow(row.r); } } } async Query>(): Promise { const rows = this.Rows(); const result: T[] = []; for await (const row of rows) { result.push(row); } return result; } async Exec(): Promise { this.formatRequest(); const req = Buffer.from(encodeRequest(this.request)); const iter = Binding.rowIterator(req); const response = iter.next(); return decodeResponse(response); } }