From b44c17d6c976c31ac85203d2b19bb1aebd0e0fa6 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Tue, 15 Nov 2022 05:17:59 +0000 Subject: [PATCH] make Result iterable --- lib/query.js | 12 ++++++++++++ typescript/pg-promise.d.ts | 4 ++-- typescript/pg-subset.d.ts | 12 ++++++------ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/query.js b/lib/query.js index 947f595b5..ea0bd7c28 100644 --- a/lib/query.js +++ b/lib/query.js @@ -167,12 +167,14 @@ function $query(ctx, query, values, qrm, config) { lastResult = result[result.length - 1]; for (let i = 0; i < result.length; i++) { const r = result[i]; + makeIterable(r); error = Events.receive(opt, r.rows, r, getContext()); if (error) { break; } } } else { + makeIterable(result); result.duration = Date.now() - start; error = Events.receive(opt, result.rows, result, getContext()); } @@ -258,6 +260,16 @@ function $query(ctx, query, values, qrm, config) { }); } +// Extends Result to provide iterable for the rows; +// +// To be removed once the following PR is merged amd released: +// https://github.com/brianc/node-postgres/pull/2861 +function makeIterable(r) { + r[Symbol.iterator] = function () { + return this.rows.values(); + }; +} + module.exports = config => { return function (ctx, query, values, qrm) { return $query.call(this, ctx, query, values, qrm, config); diff --git a/typescript/pg-promise.d.ts b/typescript/pg-promise.d.ts index 38e745bc7..c3aa23e73 100644 --- a/typescript/pg-promise.d.ts +++ b/typescript/pg-promise.d.ts @@ -8,7 +8,7 @@ */ ///////////////////////////////////////// -// Requires pg-promise v10.5.0 or later. +// Requires pg-promise v10.14.0 or later. ///////////////////////////////////////// // We use ES6 as static promise here, because generic promises are still not supported. @@ -290,7 +290,7 @@ declare namespace pgPromise { readonly $pool: pg.IPool } - interface IResultExt extends pg.IResult { + interface IResultExt extends pg.IResult { // Property 'duration' exists only in the following context: // - for single-query events 'receive' // - for method Database.result diff --git a/typescript/pg-subset.d.ts b/typescript/pg-subset.d.ts index 4850af1de..8a4493a41 100644 --- a/typescript/pg-subset.d.ts +++ b/typescript/pg-subset.d.ts @@ -39,10 +39,10 @@ declare namespace pg { format: string } - interface IResult { + interface IResult extends Iterable { command: string rowCount: number - rows: any[] + rows: T[] fields: IColumn[] // properties below are not available within Native Bindings: @@ -290,13 +290,13 @@ declare namespace pg { interface IClient extends EventEmitter { - query(config: any, values: any[], callback: (err: Error, result: IResult) => void): undefined + query(config: any, values: any[], callback: (err: Error, result: IResult) => void): undefined - query(config: any, callback: (err: Error, result: IResult) => void): undefined + query(config: any, callback: (err: Error, result: IResult) => void): undefined - query(config: any, values: any[]): Promise + query(config: any, values: any[]): Promise> - query(config: any): Promise + query(config: any): Promise> connectionParameters: IConnectionParameters database: string