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