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