mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
[@types/pg] Generify query methods (#37892)
* [@types/pg] Generify query methods `R` will be the return type of the query, `I` a tuple for parameters * [@types/pg] Add test for generified query method * Raise pg-copy-streams minimum typescript version for pg compatibility * Raise pg-ears minimum typescript version for pg compatibility * Raise pg-large-object minimum typescript version for pg compatibility * Raise pg-pool minimum typescript version for pg compatibility * Raise pg-query minimum typescript version for pg compatibility
This commit is contained in:
parent
65784f7fba
commit
47e9cf6d89
1
types/pg-copy-streams/index.d.ts
vendored
1
types/pg-copy-streams/index.d.ts
vendored
@ -2,6 +2,7 @@
|
||||
// Project: https://github.com/brianc/node-pg-copy-streams
|
||||
// Definitions by: Brian Crowell <https://github.com/fluggo>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
|
||||
1
types/pg-ears/index.d.ts
vendored
1
types/pg-ears/index.d.ts
vendored
@ -2,6 +2,7 @@
|
||||
// Project: https://github.com/doesdev/pg-ears
|
||||
// Definitions by: Bradley Ayers <https://github.com/bradleyayers>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
import { ClientConfig } from "pg";
|
||||
|
||||
|
||||
2
types/pg-large-object/index.d.ts
vendored
2
types/pg-large-object/index.d.ts
vendored
@ -2,7 +2,7 @@
|
||||
// Project: https://github.com/Joris-van-der-Wel/node-pg-large-object#readme
|
||||
// Definitions by: Mateusz Krupa <https://github.com/mateuszkrupa>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
|
||||
1
types/pg-pool/index.d.ts
vendored
1
types/pg-pool/index.d.ts
vendored
@ -2,6 +2,7 @@
|
||||
// Project: https://github.com/brianc/node-pg-pool
|
||||
// Definitions by: Leo Liang <https://github.com/aleung>, Nikita Tokarchuk <https://github.com/mainnika>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
import * as pg from 'pg';
|
||||
|
||||
|
||||
1
types/pg-query-stream/index.d.ts
vendored
1
types/pg-query-stream/index.d.ts
vendored
@ -2,6 +2,7 @@
|
||||
// Project: https://github.com/brianc/node-pg-query-stream
|
||||
// Definitions by: António Marques <https://github.com/asmarques>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
|
||||
61
types/pg/index.d.ts
vendored
61
types/pg/index.d.ts
vendored
@ -1,7 +1,8 @@
|
||||
// Type definitions for pg 7.11
|
||||
// Project: http://github.com/brianc/node-postgres
|
||||
// Definitions by: Phips Peter <https://github.com/pspeter3>
|
||||
// Definitions by: Phips Peter <https://github.com/pspeter3>, Ravi van Rooijen <https://github.com/HoldYourWaffle>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
@ -48,17 +49,17 @@ export interface PoolConfig extends ClientConfig {
|
||||
Promise?: PromiseConstructorLike;
|
||||
}
|
||||
|
||||
export interface QueryConfig {
|
||||
export interface QueryConfig<I extends any[] = any[]> {
|
||||
name?: string;
|
||||
text: string;
|
||||
values?: any[];
|
||||
values?: I;
|
||||
}
|
||||
|
||||
export interface Submittable {
|
||||
submit: (connection: Connection) => void;
|
||||
}
|
||||
|
||||
export interface QueryArrayConfig extends QueryConfig {
|
||||
export interface QueryArrayConfig<I extends any[] = any[]> extends QueryConfig<I> {
|
||||
rowMode: 'array';
|
||||
}
|
||||
|
||||
@ -79,12 +80,16 @@ export interface QueryResultBase {
|
||||
fields: FieldDef[];
|
||||
}
|
||||
|
||||
export interface QueryResult extends QueryResultBase {
|
||||
rows: any[];
|
||||
export interface QueryResultRow {
|
||||
[column: string]: any;
|
||||
}
|
||||
|
||||
export interface QueryArrayResult extends QueryResultBase {
|
||||
rows: any[][];
|
||||
export interface QueryResult<R extends QueryResultRow = any> extends QueryResultBase {
|
||||
rows: R[];
|
||||
}
|
||||
|
||||
export interface QueryArrayResult<R extends any[] = any[]> extends QueryResultBase {
|
||||
rows: R[];
|
||||
}
|
||||
|
||||
export interface Notification {
|
||||
@ -93,8 +98,8 @@ export interface Notification {
|
||||
payload?: string;
|
||||
}
|
||||
|
||||
export interface ResultBuilder extends QueryResult {
|
||||
addRow(row: any): void;
|
||||
export interface ResultBuilder<R extends QueryResultRow = any> extends QueryResult<R> {
|
||||
addRow(row: R): void;
|
||||
}
|
||||
|
||||
export interface QueryParse {
|
||||
@ -156,12 +161,14 @@ export class Pool extends events.EventEmitter {
|
||||
end(callback: () => void): void;
|
||||
|
||||
query<T extends Submittable>(queryStream: T): T;
|
||||
query(queryConfig: QueryArrayConfig, values?: any[]): Promise<QueryArrayResult>;
|
||||
query(queryConfig: QueryConfig): Promise<QueryResult>;
|
||||
query(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise<QueryResult>;
|
||||
query(queryConfig: QueryArrayConfig, callback: (err: Error, result: QueryArrayResult) => void): void;
|
||||
query(queryTextOrConfig: string | QueryConfig, callback: (err: Error, result: QueryResult) => void): void;
|
||||
query(queryText: string, values: any[], callback: (err: Error, result: QueryResult) => void): void;
|
||||
// tslint:disable:no-unnecessary-generics
|
||||
query<R extends any[] = any[], I extends any[] = any[]>(queryConfig: QueryArrayConfig<I>, values?: I): Promise<QueryArrayResult<R>>;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: I): Promise<QueryResult<R>>;
|
||||
query<R extends any[] = any[], I extends any[] = any[]>(queryConfig: QueryArrayConfig<I>, callback: (err: Error, result: QueryArrayResult<R>) => void): void;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryTextOrConfig: string | QueryConfig<I>, callback: (err: Error, result: QueryResult<R>) => void): void;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryText: string, values: I, callback: (err: Error, result: QueryResult<R>) => void): void;
|
||||
// tslint:enable:no-unnecessary-generics
|
||||
|
||||
on(event: "error", listener: (err: Error, client: PoolClient) => void): this;
|
||||
on(event: "connect" | "acquire" | "remove", listener: (client: PoolClient) => void): this;
|
||||
@ -174,12 +181,14 @@ export class ClientBase extends events.EventEmitter {
|
||||
connect(callback: (err: Error) => void): void;
|
||||
|
||||
query<T extends Submittable>(queryStream: T): T;
|
||||
query(queryConfig: QueryArrayConfig, values?: any[]): Promise<QueryArrayResult>;
|
||||
query(queryConfig: QueryConfig): Promise<QueryResult>;
|
||||
query(queryTextOrConfig: string | QueryConfig, values?: any[]): Promise<QueryResult>;
|
||||
query(queryConfig: QueryArrayConfig, callback: (err: Error, result: QueryArrayResult) => void): void;
|
||||
query(queryTextOrConfig: string | QueryConfig, callback: (err: Error, result: QueryResult) => void): void;
|
||||
query(queryText: string, values: any[], callback: (err: Error, result: QueryResult) => void): void;
|
||||
// tslint:disable:no-unnecessary-generics
|
||||
query<R extends any[] = any[], I extends any[] = any[]>(queryConfig: QueryArrayConfig<I>, values?: I): Promise<QueryArrayResult<R>>;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: I): Promise<QueryResult<R>>;
|
||||
query<R extends any[] = any[], I extends any[] = any[]>(queryConfig: QueryArrayConfig<I>, callback: (err: Error, result: QueryArrayResult<R>) => void): void;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryTextOrConfig: string | QueryConfig<I>, callback: (err: Error, result: QueryResult<R>) => void): void;
|
||||
query<R extends QueryResultRow = any, I extends any[] = any[]>(queryText: string, values: any[], callback: (err: Error, result: QueryResult<R>) => void): void;
|
||||
// tslint:enable:no-unnecessary-generics
|
||||
|
||||
copyFrom(queryText: string): stream.Writable;
|
||||
copyTo(queryText: string): stream.Readable;
|
||||
@ -208,12 +217,12 @@ export interface PoolClient extends ClientBase {
|
||||
release(err?: Error): void;
|
||||
}
|
||||
|
||||
export class Query extends events.EventEmitter implements Submittable {
|
||||
constructor(queryTextOrConfig?: string | QueryConfig, values?: any[]);
|
||||
export class Query<R extends QueryResultRow = any, I extends any[] = any> extends events.EventEmitter implements Submittable {
|
||||
constructor(queryTextOrConfig?: string | QueryConfig<I>, values?: I);
|
||||
submit: (connection: Connection) => void;
|
||||
on(event: "row", listener: (row: any, result?: ResultBuilder) => void): this;
|
||||
on(event: "row", listener: (row: R, result?: ResultBuilder<R>) => void): this;
|
||||
on(event: "error", listener: (err: Error) => void): this;
|
||||
on(event: "end", listener: (result: ResultBuilder) => void): this;
|
||||
on(event: "end", listener: (result: ResultBuilder<R>) => void): this;
|
||||
}
|
||||
|
||||
export class Events extends events.EventEmitter {
|
||||
|
||||
@ -46,6 +46,16 @@ client.query('SELECT $1::text as name', ['brianc'], (err, res) => {
|
||||
client.end();
|
||||
});
|
||||
|
||||
interface Person {
|
||||
name: string;
|
||||
}
|
||||
|
||||
client.query<Person, [string]>('SELECT $1::text as name', ['brianc'], (err, res) => {
|
||||
if (err) throw err;
|
||||
console.log(res.rows[0].name);
|
||||
client.end();
|
||||
});
|
||||
|
||||
const query = {
|
||||
name: 'get-name',
|
||||
text: 'SELECT $1::text',
|
||||
|
||||
Loading…
Reference in New Issue
Block a user