UNPKG

9.41 kBTypeScriptView Raw
1// Type definitions for pg 8.6
2// Project: http://github.com/brianc/node-postgres
3// Definitions by: Phips Peter <https://github.com/pspeter3>, Ravi van Rooijen <https://github.com/HoldYourWaffle>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// TypeScript Version: 2.3
6
7/// <reference types="node" />
8
9import events = require('events');
10import stream = require('stream');
11import pgTypes = require('pg-types');
12import { NoticeMessage } from 'pg-protocol/dist/messages';
13
14import { ConnectionOptions } from 'tls';
15
16export interface ClientConfig {
17 user?: string | undefined;
18 database?: string | undefined;
19 password?: string | (() => string | Promise<string>) | undefined;
20 port?: number | undefined;
21 host?: string | undefined;
22 connectionString?: string | undefined;
23 keepAlive?: boolean | undefined;
24 stream?: stream.Duplex | undefined;
25 statement_timeout?: false | number | undefined;
26 parseInputDatesAsUTC?: boolean | undefined;
27 ssl?: boolean | ConnectionOptions | undefined;
28 query_timeout?: number | undefined;
29 keepAliveInitialDelayMillis?: number | undefined;
30 idle_in_transaction_session_timeout?: number | undefined;
31 application_name?: string | undefined;
32 connectionTimeoutMillis?: number | undefined;
33 types?: CustomTypesConfig | undefined;
34}
35
36export type ConnectionConfig = ClientConfig;
37
38export interface Defaults extends ClientConfig {
39 poolSize?: number | undefined;
40 poolIdleTimeout?: number | undefined;
41 reapIntervalMillis?: number | undefined;
42 binary?: boolean | undefined;
43 parseInt8?: boolean | undefined;
44}
45
46export interface PoolConfig extends ClientConfig {
47 // properties from module 'node-pool'
48 max?: number | undefined;
49 min?: number | undefined;
50 idleTimeoutMillis?: number | undefined;
51 log?: ((...messages: any[]) => void) | undefined;
52 Promise?: PromiseConstructorLike | undefined;
53}
54
55export interface QueryConfig<I extends any[] = any[]> {
56 name?: string | undefined;
57 text: string;
58 values?: I | undefined;
59 types?: CustomTypesConfig | undefined;
60}
61
62export interface CustomTypesConfig {
63 getTypeParser: typeof pgTypes.getTypeParser;
64}
65
66export interface Submittable {
67 submit: (connection: Connection) => void;
68}
69
70export interface QueryArrayConfig<I extends any[] = any[]> extends QueryConfig<I> {
71 rowMode: 'array';
72}
73
74export interface FieldDef {
75 name: string;
76 tableID: number;
77 columnID: number;
78 dataTypeID: number;
79 dataTypeSize: number;
80 dataTypeModifier: number;
81 format: string;
82}
83
84export interface QueryResultBase {
85 command: string;
86 rowCount: number;
87 oid: number;
88 fields: FieldDef[];
89}
90
91export interface QueryResultRow {
92 [column: string]: any;
93}
94
95export interface QueryResult<R extends QueryResultRow = any> extends QueryResultBase {
96 rows: R[];
97}
98
99export interface QueryArrayResult<R extends any[] = any[]> extends QueryResultBase {
100 rows: R[];
101}
102
103export interface Notification {
104 processId: number;
105 channel: string;
106 payload?: string | undefined;
107}
108
109export interface ResultBuilder<R extends QueryResultRow = any> extends QueryResult<R> {
110 addRow(row: R): void;
111}
112
113export interface QueryParse {
114 name: string;
115 text: string;
116 types: string[];
117}
118
119export interface BindConfig {
120 portal?: string | undefined;
121 statement?: string | undefined;
122 binary?: string | undefined;
123 values?: Array<Buffer | null | undefined | string> | undefined;
124}
125
126export interface ExecuteConfig {
127 portal?: string | undefined;
128 rows?: string | undefined;
129}
130
131export interface MessageConfig {
132 type: string;
133 name?: string | undefined;
134}
135
136export class Connection extends events.EventEmitter {
137 readonly stream: stream.Duplex;
138
139 constructor(config?: ConnectionConfig);
140
141 bind(config: BindConfig | null, more: boolean): void;
142 execute(config: ExecuteConfig | null, more: boolean): void;
143 parse(query: QueryParse, more: boolean): void;
144
145 query(text: string): void;
146
147 describe(msg: MessageConfig, more: boolean): void;
148 close(msg: MessageConfig, more: boolean): void;
149
150 flush(): void;
151 sync(): void;
152 end(): void;
153}
154
155/**
156 * {@link https://node-postgres.com/api/pool}
157 */
158export class Pool extends events.EventEmitter {
159 /**
160 * Every field of the config object is entirely optional.
161 * The config passed to the pool is also passed to every client
162 * instance within the pool when the pool creates that client.
163 */
164 constructor(config?: PoolConfig);
165
166 readonly totalCount: number;
167 readonly idleCount: number;
168 readonly waitingCount: number;
169
170 connect(): Promise<PoolClient>;
171 connect(callback: (err: Error, client: PoolClient, done: (release?: any) => void) => void): void;
172
173 end(): Promise<void>;
174 end(callback: () => void): void;
175
176 query<T extends Submittable>(queryStream: T): T;
177 // tslint:disable:no-unnecessary-generics
178 query<R extends any[] = any[], I extends any[] = any[]>(
179 queryConfig: QueryArrayConfig<I>,
180 values?: I,
181 ): Promise<QueryArrayResult<R>>;
182 query<R extends QueryResultRow = any, I extends any[] = any[]>(
183 queryConfig: QueryConfig<I>,
184 ): Promise<QueryResult<R>>;
185 query<R extends QueryResultRow = any, I extends any[] = any[]>(
186 queryTextOrConfig: string | QueryConfig<I>,
187 values?: I,
188 ): Promise<QueryResult<R>>;
189 query<R extends any[] = any[], I extends any[] = any[]>(
190 queryConfig: QueryArrayConfig<I>,
191 callback: (err: Error, result: QueryArrayResult<R>) => void,
192 ): void;
193 query<R extends QueryResultRow = any, I extends any[] = any[]>(
194 queryTextOrConfig: string | QueryConfig<I>,
195 callback: (err: Error, result: QueryResult<R>) => void,
196 ): void;
197 query<R extends QueryResultRow = any, I extends any[] = any[]>(
198 queryText: string,
199 values: I,
200 callback: (err: Error, result: QueryResult<R>) => void,
201 ): void;
202 // tslint:enable:no-unnecessary-generics
203
204 on(event: 'error', listener: (err: Error, client: PoolClient) => void): this;
205 on(event: 'connect' | 'acquire' | 'remove', listener: (client: PoolClient) => void): this;
206}
207
208export class ClientBase extends events.EventEmitter {
209 constructor(config?: string | ClientConfig);
210
211 connect(): Promise<void>;
212 connect(callback: (err: Error) => void): void;
213
214 query<T extends Submittable>(queryStream: T): T;
215 // tslint:disable:no-unnecessary-generics
216 query<R extends any[] = any[], I extends any[] = any[]>(
217 queryConfig: QueryArrayConfig<I>,
218 values?: I,
219 ): Promise<QueryArrayResult<R>>;
220 query<R extends QueryResultRow = any, I extends any[] = any[]>(
221 queryConfig: QueryConfig<I>,
222 ): Promise<QueryResult<R>>;
223 query<R extends QueryResultRow = any, I extends any[] = any[]>(
224 queryTextOrConfig: string | QueryConfig<I>,
225 values?: I,
226 ): Promise<QueryResult<R>>;
227 query<R extends any[] = any[], I extends any[] = any[]>(
228 queryConfig: QueryArrayConfig<I>,
229 callback: (err: Error, result: QueryArrayResult<R>) => void,
230 ): void;
231 query<R extends QueryResultRow = any, I extends any[] = any[]>(
232 queryTextOrConfig: string | QueryConfig<I>,
233 callback: (err: Error, result: QueryResult<R>) => void,
234 ): void;
235 query<R extends QueryResultRow = any, I extends any[] = any[]>(
236 queryText: string,
237 values: any[],
238 callback: (err: Error, result: QueryResult<R>) => void,
239 ): void;
240 // tslint:enable:no-unnecessary-generics
241
242 copyFrom(queryText: string): stream.Writable;
243 copyTo(queryText: string): stream.Readable;
244
245 pauseDrain(): void;
246 resumeDrain(): void;
247
248 escapeIdentifier(str: string): string;
249 escapeLiteral(str: string): string;
250
251 on(event: 'drain', listener: () => void): this;
252 on(event: 'error', listener: (err: Error) => void): this;
253 on(event: 'notice', listener: (notice: NoticeMessage) => void): this;
254 on(event: 'notification', listener: (message: Notification) => void): this;
255 // tslint:disable-next-line unified-signatures
256 on(event: 'end', listener: () => void): this;
257}
258
259export class Client extends ClientBase {
260 user?: string | undefined;
261 database?: string | undefined;
262 port: number;
263 host: string;
264 password?: string | undefined;
265 ssl: boolean;
266
267 constructor(config?: string | ClientConfig);
268
269 end(): Promise<void>;
270 end(callback: (err: Error) => void): void;
271}
272
273export interface PoolClient extends ClientBase {
274 release(err?: Error | boolean): void;
275}
276
277export class Query<R extends QueryResultRow = any, I extends any[] = any> extends events.EventEmitter
278 implements Submittable {
279 constructor(queryTextOrConfig?: string | QueryConfig<I>, values?: I);
280 submit: (connection: Connection) => void;
281 on(event: 'row', listener: (row: R, result?: ResultBuilder<R>) => void): this;
282 on(event: 'error', listener: (err: Error) => void): this;
283 on(event: 'end', listener: (result: ResultBuilder<R>) => void): this;
284}
285
286export class Events extends events.EventEmitter {
287 on(event: 'error', listener: (err: Error, client: Client) => void): this;
288}
289
290export const types: typeof pgTypes;
291
292export const defaults: Defaults & ClientConfig;
293
294import * as Pg from '.';
295
296export const native: typeof Pg | null;
297
298export { DatabaseError } from 'pg-protocol';