postgrejs
Version:
Professional PostgreSQL client NodeJS
92 lines (91 loc) • 3.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cursor = void 0;
const tslib_1 = require("tslib");
const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
const power_tasks_1 = require("power-tasks");
const safe_event_emitter_js_1 = require("../safe-event-emitter.js");
const convert_row_to_object_js_1 = require("../util/convert-row-to-object.js");
const parse_row_js_1 = require("../util/parse-row.js");
class Cursor extends safe_event_emitter_js_1.SafeEventEmitter {
constructor(statement, portal, fields, parsers, queryOptions) {
super();
this._taskQueue = new power_tasks_1.TaskQueue({ concurrency: 1 });
this._rows = new doublylinked_1.default();
this._closed = false;
this._statement = statement;
this._portal = portal;
this._parsers = parsers;
this._queryOptions = queryOptions;
this.fields = fields;
}
get rowType() {
return this._queryOptions.objectRows ? 'object' : 'array';
}
get isClosed() {
return this._closed;
}
async next() {
if (!this._rows.length) {
if (this._closed)
return;
await this._fetchRows();
}
return this._rows.shift();
}
async fetch(nRows) {
const out = [];
if (this._closed)
return out;
for (let i = 0; i < nRows; i++) {
if (!this._rows.length)
await this._fetchRows();
if (this._rows.length)
out.push(this._rows.shift());
else
break;
}
return out;
}
async close() {
if (this._closed)
return;
await this._portal.close();
await this._statement.close();
this.emit('close');
this._closed = true;
}
async _fetchRows() {
if (this._closed)
return;
const portal = this._portal;
await this._taskQueue
.enqueue(async () => {
const queryOptions = this._queryOptions;
const r = await portal.execute(queryOptions.fetchCount || 100);
if (r && r.rows && r.rows.length) {
if (this._parsers) {
const objectRows = queryOptions.objectRows;
const fields = this.fields;
const rows = r.rows;
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
(0, parse_row_js_1.parseRow)(this._parsers, row, this._queryOptions);
if (objectRows)
rows[i] = (0, convert_row_to_object_js_1.convertRowToObject)(fields, row);
}
}
this._rows.push(...r.rows);
this.emit('fetch', r.rows);
}
else {
await this.close();
}
})
.toPromise();
}
[Symbol.asyncDispose]() {
return this.close();
}
}
exports.Cursor = Cursor;