From f0af8db482f3b7ca6b01d11f84fd7cda5be2e9a9 Mon Sep 17 00:00:00 2001 From: Ian Liu Rodrigues Date: Sun, 24 Jun 2018 20:55:15 -0300 Subject: [PATCH] [pg] Client.query now accepts Submittables This commit creates a new interface on @types/pg called Submittable following the description on node-postgres API doc, which says: > client.query with a Submittable > > If you pass an object to client.query and the object has a .submit > function on it, the client will pass it's PostgreSQL server connection > to the object and delegate query dispatching to the supplied object > [...] and uses the new interface as argument type to Client.query and Pool.query. This allows the usage of pg-copy-streams package. --- types/pg-query-stream/index.d.ts | 4 +++- types/pg/index.d.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/types/pg-query-stream/index.d.ts b/types/pg-query-stream/index.d.ts index 34331358e7..ce1efedf5a 100644 --- a/types/pg-query-stream/index.d.ts +++ b/types/pg-query-stream/index.d.ts @@ -6,6 +6,7 @@ /// import stream = require("stream"); +import pg = require("pg"); declare namespace QueryStream { interface Options { @@ -14,11 +15,12 @@ declare namespace QueryStream { } } -declare class QueryStream extends stream.Readable { +declare class QueryStream extends stream.Readable implements pg.Submittable { batchSize: number; text: string; values?: any[]; constructor(text: string, values?: any[], options?: QueryStream.Options); + submit(connection: pg.Connection): void; } export = QueryStream; diff --git a/types/pg/index.d.ts b/types/pg/index.d.ts index 279659a9aa..621a3aa1b8 100644 --- a/types/pg/index.d.ts +++ b/types/pg/index.d.ts @@ -51,6 +51,10 @@ export interface QueryConfig { values?: any[]; } +export interface Submittable { + submit: (connection: Connection) => void; +} + export interface QueryArrayConfig extends QueryConfig { rowMode: 'array'; } @@ -148,7 +152,7 @@ export class Pool extends events.EventEmitter { end(): Promise; end(callback: () => void): void; - query(queryStream: QueryConfig & stream.Readable): stream.Readable; + query(queryStream: T): T; query(queryConfig: QueryArrayConfig): Promise; query(queryConfig: QueryConfig): Promise; query(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise; @@ -166,7 +170,7 @@ export class ClientBase extends events.EventEmitter { connect(): Promise; connect(callback: (err: Error) => void): void; - query(queryStream: QueryConfig & stream.Readable): stream.Readable; + query(queryStream: T): T; query(queryConfig: QueryArrayConfig): Promise; query(queryConfig: QueryConfig): Promise; query(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise;