mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-05 20:02:05 +08:00
only the array "QueryArrayResult" variant contained the "fields" property Added a new "QueryResultBase" interface with the shared set of fields between the single/array results and refactored both QueryResult and QueryArrayResult to extend from it, each now only needing to specify their own type variation of the "row" property for the type of results they contain (single vs array) Modified the tests to reference the "fields" array on single select results similarly to how it was done for the array select results
177 lines
5.3 KiB
TypeScript
177 lines
5.3 KiB
TypeScript
// Type definitions for pg 7.4
|
|
// Project: https://github.com/brianc/node-postgres
|
|
// Definitions by: Phips Peter <https://github.com/pspeter3>
|
|
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
|
|
/// <reference types="node" />
|
|
|
|
import events = require("events");
|
|
import stream = require("stream");
|
|
import pgTypes = require("pg-types");
|
|
|
|
export interface ConnectionConfig {
|
|
user?: string;
|
|
database?: string;
|
|
password?: string;
|
|
port?: number;
|
|
host?: string;
|
|
connectionString?: string;
|
|
}
|
|
|
|
export interface Defaults extends ConnectionConfig {
|
|
poolSize?: number;
|
|
poolIdleTimeout?: number;
|
|
reapIntervalMillis?: number;
|
|
binary?: boolean;
|
|
parseInt8?: boolean;
|
|
}
|
|
|
|
import { TlsOptions } from "tls";
|
|
|
|
export interface ClientConfig extends ConnectionConfig {
|
|
ssl?: boolean | TlsOptions;
|
|
}
|
|
|
|
export interface PoolConfig extends ClientConfig {
|
|
// properties from module 'node-pool'
|
|
max?: number;
|
|
min?: number;
|
|
connectionTimeoutMillis?: number;
|
|
idleTimeoutMillis?: number;
|
|
|
|
application_name?: string;
|
|
Promise?: PromiseConstructorLike;
|
|
}
|
|
|
|
export interface QueryConfig {
|
|
name?: string;
|
|
text: string;
|
|
values?: any[];
|
|
}
|
|
|
|
export interface QueryArrayConfig extends QueryConfig {
|
|
rowMode: 'array';
|
|
}
|
|
|
|
export interface FieldDef {
|
|
name: string;
|
|
tableID: number;
|
|
columnID: number;
|
|
dataTypeID: number;
|
|
dataTypeSize: number;
|
|
dataTypeModifier: number;
|
|
format: string;
|
|
}
|
|
|
|
export interface QueryResultBase {
|
|
command: string;
|
|
rowCount: number;
|
|
oid: number;
|
|
fields: FieldDef[];
|
|
}
|
|
|
|
export interface QueryResult extends QueryResultBase {
|
|
rows: any[];
|
|
}
|
|
|
|
export interface QueryArrayResult extends QueryResultBase {
|
|
rows: any[][];
|
|
}
|
|
|
|
export interface Notification {
|
|
processId: number;
|
|
channel: string;
|
|
payload?: string;
|
|
}
|
|
|
|
export interface ResultBuilder extends QueryResult {
|
|
addRow(row: any): void;
|
|
}
|
|
|
|
export class Pool extends events.EventEmitter {
|
|
// `new Pool('pg://user@localhost/mydb')` is not allowed.
|
|
// But it passes type check because of issue:
|
|
// https://github.com/Microsoft/TypeScript/issues/7485
|
|
constructor(config?: PoolConfig);
|
|
|
|
readonly totalCount: number;
|
|
readonly idleCount: number;
|
|
readonly waitingCount: number;
|
|
|
|
connect(): Promise<PoolClient>;
|
|
connect(callback: (err: Error, client: PoolClient, done: () => void) => void): void;
|
|
|
|
end(): Promise<void>;
|
|
end(callback: () => void): void;
|
|
|
|
query(queryStream: QueryConfig & stream.Readable): stream.Readable;
|
|
query(queryConfig: QueryArrayConfig): Promise<QueryArrayResult>;
|
|
query(queryConfig: QueryConfig): Promise<QueryResult>;
|
|
query(queryText: string, values?: any[]): Promise<QueryResult>;
|
|
query(queryConfig: QueryArrayConfig, callback: (err: Error, result: QueryArrayResult) => void): Query;
|
|
query(queryTextOrConfig: string | QueryConfig, callback: (err: Error, result: QueryResult) => void): Query;
|
|
query(queryText: string, values: any[], callback: (err: Error, result: QueryResult) => void): Query;
|
|
|
|
on(event: "error", listener: (err: Error, client: PoolClient) => void): this;
|
|
on(event: "connect" | "acquire" | "remove", listener: (client: PoolClient) => void): this;
|
|
}
|
|
|
|
export class ClientBase extends events.EventEmitter {
|
|
constructor(config: string | ClientConfig);
|
|
|
|
connect(): Promise<void>;
|
|
connect(callback: (err: Error) => void): void;
|
|
|
|
query(queryStream: QueryConfig & stream.Readable): stream.Readable;
|
|
query(queryConfig: QueryArrayConfig): Promise<QueryArrayResult>;
|
|
query(queryConfig: QueryConfig): Promise<QueryResult>;
|
|
query(queryText: string, values?: any[]): Promise<QueryResult>;
|
|
query(queryConfig: QueryArrayConfig, callback: (err: Error, result: QueryArrayResult) => void): Query;
|
|
query(queryTextOrConfig: string | QueryConfig, callback: (err: Error, result: QueryResult) => void): Query;
|
|
query(queryText: string, values: any[], callback: (err: Error, result: QueryResult) => void): Query;
|
|
|
|
copyFrom(queryText: string): stream.Writable;
|
|
copyTo(queryText: string): stream.Readable;
|
|
|
|
pauseDrain(): void;
|
|
resumeDrain(): void;
|
|
|
|
escapeIdentifier(str: string): string;
|
|
escapeLiteral(str: string): string;
|
|
|
|
on(event: "drain", listener: () => void): this;
|
|
on(event: "error" | "notice", listener: (err: Error) => void): this;
|
|
on(event: "notification", listener: (message: Notification) => void): this;
|
|
// tslint:disable-next-line unified-signatures
|
|
on(event: "end", listener: () => void): this;
|
|
}
|
|
|
|
export class Client extends ClientBase {
|
|
constructor(config: string | ClientConfig);
|
|
|
|
end(): Promise<void>;
|
|
end(callback: (err: Error) => void): void;
|
|
}
|
|
|
|
export interface PoolClient extends ClientBase {
|
|
release(err?: Error): void;
|
|
}
|
|
|
|
export class Query extends events.EventEmitter {
|
|
on(event: "row", listener: (row: any, result?: ResultBuilder) => void): this;
|
|
on(event: "error", listener: (err: Error) => void): this;
|
|
on(event: "end", listener: (result: ResultBuilder) => void): this;
|
|
}
|
|
|
|
export class Events extends events.EventEmitter {
|
|
on(event: "error", listener: (err: Error, client: Client) => void): this;
|
|
}
|
|
|
|
export const types: typeof pgTypes;
|
|
|
|
export const defaults: Defaults & ClientConfig;
|
|
|
|
import * as Pg from '.';
|
|
|
|
export const native: typeof Pg | null;
|