Skip to content

Commit

Permalink
make Result iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 15, 2022
1 parent 5197d13 commit b44c17d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
12 changes: 12 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions typescript/pg-promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -290,7 +290,7 @@ declare namespace pgPromise {
readonly $pool: pg.IPool
}

interface IResultExt extends pg.IResult {
interface IResultExt<T = unknown> extends pg.IResult<T> {
// Property 'duration' exists only in the following context:
// - for single-query events 'receive'
// - for method Database.result
Expand Down
12 changes: 6 additions & 6 deletions typescript/pg-subset.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ declare namespace pg {
format: string
}

interface IResult {
interface IResult<T = unknown> extends Iterable<T> {
command: string
rowCount: number
rows: any[]
rows: T[]
fields: IColumn[]

// properties below are not available within Native Bindings:
Expand Down Expand Up @@ -290,13 +290,13 @@ declare namespace pg {

interface IClient extends EventEmitter {

query(config: any, values: any[], callback: (err: Error, result: IResult) => void): undefined
query<T>(config: any, values: any[], callback: (err: Error, result: IResult<T>) => void): undefined

query(config: any, callback: (err: Error, result: IResult) => void): undefined
query<T>(config: any, callback: (err: Error, result: IResult<T>) => void): undefined

query(config: any, values: any[]): Promise<IResult>
query<T>(config: any, values: any[]): Promise<IResult<T>>

query(config: any): Promise<IResult>
query<T>(config: any): Promise<IResult<T>>

connectionParameters: IConnectionParameters
database: string
Expand Down

0 comments on commit b44c17d

Please sign in to comment.