UNPKG

10 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import events = require("events");
4import stream = require("stream");
5import pgTypes = require("pg-types");
6import { NoticeMessage } from "pg-protocol/dist/messages";
7
8import { ConnectionOptions } from "tls";
9
10export type QueryConfigValues<T> = T extends Array<infer U> ? T : never;
11
12export 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
32export type ConnectionConfig = ClientConfig;
33
34export 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
43export interface PoolConfig extends ClientConfig {
44 // properties from module 'node-pool'
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
56export interface QueryConfig<I = any[]> {
57 name?: string | undefined;
58 text: string;
59 values?: QueryConfigValues<I>;
60 types?: CustomTypesConfig | undefined;
61}
62
63export interface CustomTypesConfig {
64 getTypeParser: typeof pgTypes.getTypeParser;
65}
66
67export interface Submittable {
68 submit: (connection: Connection) => void;
69}
70
71export interface QueryArrayConfig<I = any[]> extends QueryConfig<I> {
72 rowMode: "array";
73}
74
75export 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
85export interface QueryResultBase {
86 command: string;
87 rowCount: number | null;
88 oid: number;
89 fields: FieldDef[];
90}
91
92export interface QueryResultRow {
93 [column: string]: any;
94}
95
96export interface QueryResult<R extends QueryResultRow = any> extends QueryResultBase {
97 rows: R[];
98}
99
100export interface QueryArrayResult<R extends any[] = any[]> extends QueryResultBase {
101 rows: R[];
102}
103
104export interface Notification {
105 processId: number;
106 channel: string;
107 payload?: string | undefined;
108}
109
110export interface ResultBuilder<R extends QueryResultRow = any> extends QueryResult<R> {
111 addRow(row: R): void;
112}
113
114export interface QueryParse {
115 name: string;
116 text: string;
117 types: string[];
118}
119
120type ValueMapper = (param: any, index: number) => any;
121
122export 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
130export interface ExecuteConfig {
131 portal?: string | undefined;
132 rows?: string | undefined;
133}
134
135export interface MessageConfig {
136 type: string;
137 name?: string | undefined;
138}
139
140export function escapeIdentifier(str: string): string;
141
142export function escapeLiteral(str: string): string;
143
144export 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
163export interface PoolOptions extends PoolConfig {
164 max: number;
165 maxUses: number;
166 allowExitOnIdle: boolean;
167 maxLifetimeSeconds: number;
168 idleTimeoutMillis: number | null;
169}
170
171/**
172 * {@link https://node-postgres.com/apis/pool}
173 */
174export class Pool extends events.EventEmitter {
175 /**
176 * Every field of the config object is entirely optional.
177 * The config passed to the pool is also passed to every client
178 * instance within the pool when the pool creates that client.
179 */
180 constructor(config?: PoolConfig);
181
182 readonly totalCount: number;
183 readonly idleCount: number;
184 readonly waitingCount: number;
185 readonly expiredCount: number;
186
187 readonly ending: boolean;
188 readonly ended: boolean;
189
190 options: PoolOptions;
191
192 connect(): Promise<PoolClient>;
193 connect(
194 callback: (err: Error | undefined, client: PoolClient | undefined, done: (release?: any) => void) => void,
195 ): void;
196
197 end(): Promise<void>;
198 end(callback: () => void): void;
199
200 query<T extends Submittable>(queryStream: T): T;
201 // tslint:disable:no-unnecessary-generics
202 query<R extends any[] = any[], I = any[]>(
203 queryConfig: QueryArrayConfig<I>,
204 values?: QueryConfigValues<I>,
205 ): Promise<QueryArrayResult<R>>;
206 query<R extends QueryResultRow = any, I = any[]>(
207 queryConfig: QueryConfig<I>,
208 ): Promise<QueryResult<R>>;
209 query<R extends QueryResultRow = any, I = any[]>(
210 queryTextOrConfig: string | QueryConfig<I>,
211 values?: QueryConfigValues<I>,
212 ): Promise<QueryResult<R>>;
213 query<R extends any[] = any[], I = any[]>(
214 queryConfig: QueryArrayConfig<I>,
215 callback: (err: Error, result: QueryArrayResult<R>) => void,
216 ): void;
217 query<R extends QueryResultRow = any, I = any[]>(
218 queryTextOrConfig: string | QueryConfig<I>,
219 callback: (err: Error, result: QueryResult<R>) => void,
220 ): void;
221 query<R extends QueryResultRow = any, I = any[]>(
222 queryText: string,
223 values: QueryConfigValues<I>,
224 callback: (err: Error, result: QueryResult<R>) => void,
225 ): void;
226 // tslint:enable:no-unnecessary-generics
227
228 on(event: "release" | "error", listener: (err: Error, client: PoolClient) => void): this;
229 on(event: "connect" | "acquire" | "remove", listener: (client: PoolClient) => void): this;
230}
231
232export class ClientBase extends events.EventEmitter {
233 constructor(config?: string | ClientConfig);
234
235 connect(): Promise<void>;
236 connect(callback: (err: Error) => void): void;
237
238 query<T extends Submittable>(queryStream: T): T;
239 // tslint:disable:no-unnecessary-generics
240 query<R extends any[] = any[], I = any[]>(
241 queryConfig: QueryArrayConfig<I>,
242 values?: QueryConfigValues<I>,
243 ): Promise<QueryArrayResult<R>>;
244 query<R extends QueryResultRow = any, I = any>(
245 queryConfig: QueryConfig<I>,
246 ): Promise<QueryResult<R>>;
247 query<R extends QueryResultRow = any, I = any[]>(
248 queryTextOrConfig: string | QueryConfig<I>,
249 values?: QueryConfigValues<I>,
250 ): Promise<QueryResult<R>>;
251 query<R extends any[] = any[], I = any[]>(
252 queryConfig: QueryArrayConfig<I>,
253 callback: (err: Error, result: QueryArrayResult<R>) => void,
254 ): void;
255 query<R extends QueryResultRow = any, I = any[]>(
256 queryTextOrConfig: string | QueryConfig<I>,
257 callback: (err: Error, result: QueryResult<R>) => void,
258 ): void;
259 query<R extends QueryResultRow = any, I = any[]>(
260 queryText: string,
261 values: QueryConfigValues<I>,
262 callback: (err: Error, result: QueryResult<R>) => void,
263 ): void;
264 // tslint:enable:no-unnecessary-generics
265
266 copyFrom(queryText: string): stream.Writable;
267 copyTo(queryText: string): stream.Readable;
268
269 pauseDrain(): void;
270 resumeDrain(): void;
271
272 escapeIdentifier: typeof escapeIdentifier;
273 escapeLiteral: typeof escapeLiteral;
274 setTypeParser: typeof pgTypes.setTypeParser;
275 getTypeParser: typeof pgTypes.getTypeParser;
276
277 on(event: "drain", listener: () => void): this;
278 on(event: "error", listener: (err: Error) => void): this;
279 on(event: "notice", listener: (notice: NoticeMessage) => void): this;
280 on(event: "notification", listener: (message: Notification) => void): this;
281 // tslint:disable-next-line unified-signatures
282 on(event: "end", listener: () => void): this;
283}
284
285export class Client extends ClientBase {
286 user?: string | undefined;
287 database?: string | undefined;
288 port: number;
289 host: string;
290 password?: string | undefined;
291 ssl: boolean;
292
293 constructor(config?: string | ClientConfig);
294
295 end(): Promise<void>;
296 end(callback: (err: Error) => void): void;
297}
298
299export interface PoolClient extends ClientBase {
300 release(err?: Error | boolean): void;
301}
302
303export class Query<R extends QueryResultRow = any, I extends any[] = any> extends events.EventEmitter
304 implements Submittable
305{
306 constructor(queryTextOrConfig?: string | QueryConfig<I>, values?: QueryConfigValues<I>);
307 submit: (connection: Connection) => void;
308 on(event: "row", listener: (row: R, result?: ResultBuilder<R>) => void): this;
309 on(event: "error", listener: (err: Error) => void): this;
310 on(event: "end", listener: (result: ResultBuilder<R>) => void): this;
311}
312
313export class Events extends events.EventEmitter {
314 on(event: "error", listener: (err: Error, client: Client) => void): this;
315}
316
317export const types: typeof pgTypes;
318
319export const defaults: Defaults & ClientConfig;
320
321import * as Pg from ".";
322
323export const native: typeof Pg | null;
324
325export { DatabaseError } from "pg-protocol";