This repository was archived by the owner on Jan 9, 2023. It is now read-only.
forked from kagis/pgwire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.d.ts
335 lines (298 loc) · 11.5 KB
/
mod.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
export function pgconnect(...optionsChain: PgConnectOptions[]): Promise<PgConnection>;
export function pgconnection(...optionsChain: PgConnectOptions[]): PgConnection;
export function pgpool(...optionsChain: PgConnectOptions[]): PgClient;
export type PgConnectOptions = string | URL | PgConnectKnownOptions & Record<string, string | Uint8Array>;
export interface PgConnectKnownOptions {
readonly hostname?: string;
readonly port?: number;
readonly sslmode?: 'require' | 'prefer' | 'allow' | 'disable' | null;
// readonly sslrootcert?: string;
readonly password?: string | Uint8Array;
// these parameters are included in StatupMessage,
// any runtime config parameters are allowed
// https://www.postgresql.org/docs/14/runtime-config-client.html
readonly user?: string | Uint8Array;
readonly database?: string | Uint8Array;
readonly replication?: string | Uint8Array;
readonly application_name?: string | Uint8Array;
// underscore parameters are pgwire specific parameters
/** Connection attempts duration. If 0 (default) then only one connection attempt will be made. */
readonly _connectRetry?: number | string;
readonly _wakeInterval?: number | string;
readonly _poolIdleTimeout?: number | string;
readonly _poolSize?: number;
readonly _debug?: boolean;
}
export interface PgClient {
/** Simple query protocol. */
query(script: string, options?: PgSimpleQueryOptions): Promise<PgResult>;
/** Simple query protocol. */
stream(script: string, options?: PgSimpleQueryOptions): AsyncIterableIterator<PgChunk>;
/** Extended query protocol. */
query(...args: Statement[] | [...Statement[], PgExtendedQueryOptions]): Promise<PgResult>;
/** Extended query protocol. */
stream(...args: Statement[] | [...Statement[], PgExtendedQueryOptions]): AsyncIterableIterator<PgChunk>;
/** Terminates client gracefully if possible and waits until pending queries complete.
* New queries will be rejected. Has no effect if client already ended or destroyed. */
end(): Promise<void>;
/** Terminates client abruptly. Pending queries will be rejected.
* Has no effect when called on destroyed connection.
* @param {Error} reason Pending queries will be rejected with provided reason.
* New queries will also be rejected with provided reason unless `.end` was called
* before `.destroy`
* @returns reason back so you can destroy and throw in one line. */
destroy<R>(reason?: R): R;
}
export interface PgConnection extends PgClient {
logicalReplication(options: LogicalReplicationOptions): ReplicationStream;
/** Number of pending queries. */
readonly pending: number;
/** ID of postgres backend process. */
readonly pid: number | null;
readonly inTransaction: number | null;
readonly ssl: boolean | null;
/** Notification handler */
onnotification: (n: PgNotification) => void;
parameters: Record<string,string>;
}
export interface PgSimpleQueryOptions {
readonly stdin?: AsyncIterable<Uint8Array>;
readonly stdins?: Iterable<AsyncIterable<Uint8Array>>;
readonly signal?: AbortSignal | null;
}
export interface PgExtendedQueryOptions {
readonly signal?: AbortSignal | null;
}
export interface PgResult extends Iterable<any> {
/**
* @deprecated Use iterator instead.
*
* First row first column value. `undefined` if no rows returned. a */
readonly scalar: any;
readonly rows: any[][];
readonly columns: ColumnDescription[];
/** - Command tag (`'SELECT ...'`, `'UPDATE ...'`) if CommandComplete received.
* - `'PortalSuspended'` if {@link Statement.limit} has been reached.
* - `'EmptyQueryResponse'` if statement contains whitespaces or comments only.
* - `null` if there were more than one statement. */
readonly status: string | 'PortalSuspended' | 'EmptyQueryResponse' | null;
readonly results: PgSubResult[];
readonly notices: PgNotice[];
}
export interface PgSubResult {
/** First row first column value. `undefined` if no rows returned. */
readonly scalar: any;
readonly rows: any[][];
readonly columns: ColumnDescription[];
/** - Command tag (`'SELECT ...'`, `'UPDATE ...'`) if CommandComplete received.
* - `'PortalSuspended'` if {@link Statement.limit} has been reached.
* - `'EmptyQueryResponse'` if statement contains whitespaces or comments only. */
readonly status: string | 'PortalSuspended' | 'EmptyQueryResponse';
}
export type PgChunk = (
| PgChunkDataRow
| PgChunkCopyData
| PgChunkCommandComplete
| PgChunkRowDescription
);
export interface PgChunkDataRow extends Uint8Array {
readonly tag: 'DataRow';
readonly rows: any[][];
readonly copies: [];
readonly payload: null;
}
export interface PgChunkCopyData extends Uint8Array {
readonly tag: 'CopyData';
readonly rows: [];
readonly copies: Uint8Array[];
readonly payload: null;
}
export interface PgChunkCommandComplete extends Uint8Array {
readonly tag: 'CommandComplete';
readonly rows: [];
readonly copies: [];
/** Command, SELECT N, UPDATE 0 N, ... */
readonly payload: string;
}
export interface PgChunkRowDescription extends Uint8Array {
readonly tag: 'RowDescription';
readonly rows: [];
readonly copies: [];
readonly payload: ColumnDescription[];
}
export interface ColumnDescription {
readonly name: string;
readonly typeOid: number;
readonly typeMod: number;
}
export interface Statement {
readonly statement: string;
// readonly statementName?: string;
readonly params?: StatementParam[];
/** Max number of rows to fetch.
* {@link StatementResult.suspended} will be `true` if limit has been reached. */
readonly limit?: number;
readonly stdin?: AsyncIterable<Uint8Array>;
}
export interface StatementParam {
/** Valid type oid or builtin type name. */
readonly type?: number | 'uuid' | 'varchar' | 'bool' | 'bytea' | 'int2' | 'int4' | 'float4' | 'float8' | 'int8' | 'json' | 'jsonb' | 'pg_lsn';
readonly value: any;
}
export interface LogicalReplicationOptions {
readonly slot: string;
readonly startLsn?: string;
/** Decoder options */
readonly options?: Record<string, string>;
readonly ackIntervalMillis?: number;
}
export interface ReplicationStream extends AsyncIterable<ReplicationChunk> {
/** Confirms receipt of replication packet by lsn.
* Use {@link ReplicationMessage.lsn} to get packet lsn. */
ack(lsn: string): undefined;
/**
* Decodes {@link ReplicationMessage.data} and yields upgraded pgoutput packets.
* Use this method if replication is started with pgoutput slot. */
pgoutputDecode(): AsyncIterable<PgotputChunk>;
}
export interface ReplicationChunk {
readonly endLsn: string;
readonly time: bigint;
readonly messages: ReplicationMessage[];
}
export interface ReplicationMessage {
/** Log Serial Number of packet.
* Use it for {@link ReplicationStream.ack} to confirm receipt of packet. */
readonly lsn: string | null;
readonly endLsn: string | null;
/** microseconds since unix epoch */
readonly time: bigint;
/** binary payload */
readonly data: Uint8Array;
}
export interface PgotputChunk extends ReplicationChunk {
readonly messages: PgoutputMessage[];
}
/** https://www.postgresql.org/docs/14/protocol-logicalrep-message-formats.html */
export type PgoutputMessage = (
| PgoutputBegin
| PgoutputCommit
| PgoutputRelation
| PgoutputInsert
| PgoutputUpdate
| PgoutputDelete
| PgoutputTruncate
| PgoutputCustomMessage
);
export interface PgoutputBegin extends ReplicationMessage {
readonly tag: 'begin';
/** https://github.com/postgres/postgres/blob/27b77ecf9f4d5be211900eda54d8155ada50d696/src/include/replication/reorderbuffer.h#L275 */
readonly commitLsn: string;
readonly commitTime: bigint;
readonly xid: number;
}
export interface PgoutputCommit extends ReplicationMessage {
readonly tag: 'commit';
readonly commitLsn: string;
readonly commitTime: bigint;
}
export interface PgoutputRelation extends ReplicationMessage {
readonly tag: 'relation';
readonly relationid: number;
readonly schema: string;
readonly name: string;
/** https://www.postgresql.org/docs/14/sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY */
readonly replicaIdentity: 'default' | 'nothing'| 'full' | 'index';
readonly keyColumns: string[];
readonly columns: Array<{
/** `0b1` if attribute is part of replica identity */
readonly flags: number;
readonly name: string;
readonly typeOid: number;
readonly typeMod: number;
readonly typeSchema: string | null;
readonly typeName: string | null;
}>;
}
export interface PgoutputInsert extends ReplicationMessage {
readonly tag: 'insert';
readonly relation: PgoutputRelation;
readonly key: Record<string, any>;
readonly before: null;
readonly after: Record<string, any>;
}
export interface PgoutputUpdate extends ReplicationMessage {
readonly tag: 'update';
readonly relation: PgoutputRelation;
readonly key: Record<string, any>;
/**
* If {@link PgoutputRelation.replicaIdentity} is not `full`
* then gets row values before update, otherwise gets `null` */
readonly before: Record<string, any> | null;
/**
* Gets row values after update.
* If {@link PgoutputRelation.replicaIdentity} is not `full`
* then unchanged TOASTed values will be `undefined`.
* See https://www.postgresql.org/docs/14/storage-toast.html for TOASTing */
readonly after: Record<string, any>;
}
export interface PgoutputDelete extends ReplicationMessage {
readonly tag: 'delete';
readonly relation: PgoutputRelation;
readonly key: Record<string, any>;
/**
* If {@link PgoutputRelation.replicaIdentity} is not `full`
* then gets values of deleted row, otherwise gets `null`. */
readonly before: Record<string, any> | null;
readonly after: null;
}
export interface PgoutputTruncate extends ReplicationMessage {
readonly tag: 'truncate';
readonly cascade: boolean;
readonly restartIdentity: boolean;
/** Truncated relations. */
readonly relations: PgoutputRelation[];
}
export interface PgoutputCustomMessage extends ReplicationMessage {
readonly tag: 'message';
readonly transactional: boolean;
readonly messageLsn: string;
readonly prefix: string;
readonly content: Uint8Array;
}
/** https://www.postgresql.org/docs/14/protocol-error-fields.html */
export interface PgNotice {
/** WARNING, NOTICE, DEBUG, INFO, or LOG, or a localized translation of one of these. */
severity: string;
/** The SQLSTATE code for the error. Not localizable.
* https://www.postgresql.org/docs/14/errcodes-appendix.html */
code: string;
/** The primary human-readable error message. This should be accurate but terse (typically one line). */
message: string;
/** Optional secondary error message carrying more detail about the problem. Might run to multiple lines. */
detail: string | undefined;
/** An optional suggestion what to do about the problem.
* This is intended to differ from Detail in that it offers advice
* (potentially inappropriate) rather than hard facts.
* Might run to multiple lines. */
hint: string | undefined;
/** Error cursor position as an index into the original query string.
* The first character has index 1, and positions are measured in characters not bytes. */
position: number | undefined;
internalPosition: number | undefined;
internalQuery: string | undefined;
where: string | undefined;
file: string | undefined;
line: string | undefined;
routine: string | undefined;
schema: string | undefined;
table: string | undefined;
column: string | undefined;
datatype: string | undefined;
constraint: string | undefined;
}
export interface PgNotification {
readonly pid: number;
readonly channel: string;
readonly payload: string;
}