index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. const stream_1 = require("stream");
  6. const pg_cursor_1 = __importDefault(require("pg-cursor"));
  7. class QueryStream extends stream_1.Readable {
  8. constructor(text, values, config = {}) {
  9. const { batchSize, highWaterMark = 100 } = config;
  10. super({ objectMode: true, autoDestroy: true, highWaterMark: batchSize || highWaterMark });
  11. this.cursor = new pg_cursor_1.default(text, values, config);
  12. // delegate Submittable callbacks to cursor
  13. this.handleRowDescription = this.cursor.handleRowDescription.bind(this.cursor);
  14. this.handleDataRow = this.cursor.handleDataRow.bind(this.cursor);
  15. this.handlePortalSuspended = this.cursor.handlePortalSuspended.bind(this.cursor);
  16. this.handleCommandComplete = this.cursor.handleCommandComplete.bind(this.cursor);
  17. this.handleReadyForQuery = this.cursor.handleReadyForQuery.bind(this.cursor);
  18. this.handleError = this.cursor.handleError.bind(this.cursor);
  19. this.handleEmptyQuery = this.cursor.handleEmptyQuery.bind(this.cursor);
  20. // pg client sets types via _result property
  21. this._result = this.cursor._result;
  22. }
  23. submit(connection) {
  24. this.cursor.submit(connection);
  25. }
  26. _destroy(_err, cb) {
  27. this.cursor.close((err) => {
  28. cb(err || _err);
  29. });
  30. }
  31. // https://nodejs.org/api/stream.html#stream_readable_read_size_1
  32. _read(size) {
  33. this.cursor.read(size, (err, rows) => {
  34. if (err) {
  35. // https://nodejs.org/api/stream.html#stream_errors_while_reading
  36. this.destroy(err);
  37. }
  38. else {
  39. for (const row of rows)
  40. this.push(row);
  41. if (rows.length < size)
  42. this.push(null);
  43. }
  44. });
  45. }
  46. }
  47. module.exports = QueryStream;
  48. //# sourceMappingURL=index.js.map