postgrejs 3.12.0 → 3.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/README.md +1 -1
  2. package/connection/bind-param.d.ts +21 -0
  3. package/connection/bind-param.js +21 -0
  4. package/connection/connection.d.ts +136 -0
  5. package/connection/connection.js +132 -5
  6. package/connection/copy-stream.d.ts +2 -0
  7. package/connection/copy-stream.js +2 -0
  8. package/connection/cursor.d.ts +67 -5
  9. package/connection/cursor.js +88 -15
  10. package/connection/intl-connection.d.ts +14 -0
  11. package/connection/intl-connection.js +95 -15
  12. package/connection/large-object.d.ts +18 -0
  13. package/connection/large-object.js +17 -0
  14. package/connection/logical-replication.d.ts +15 -0
  15. package/connection/logical-replication.js +6 -0
  16. package/connection/pool.d.ts +80 -0
  17. package/connection/pool.js +82 -9
  18. package/connection/portal.d.ts +37 -0
  19. package/connection/portal.js +29 -0
  20. package/connection/prepared-statement.d.ts +68 -0
  21. package/connection/prepared-statement.js +71 -2
  22. package/constants.d.ts +18 -0
  23. package/constants.js +18 -0
  24. package/data-type-map.d.ts +38 -0
  25. package/data-type-map.js +38 -0
  26. package/data-types/bit-type.d.ts +4 -0
  27. package/data-types/bit-type.js +4 -0
  28. package/data-types/bool-type.d.ts +2 -0
  29. package/data-types/bool-type.js +2 -0
  30. package/data-types/box-type.d.ts +2 -0
  31. package/data-types/box-type.js +2 -0
  32. package/data-types/bytea-type.d.ts +2 -0
  33. package/data-types/bytea-type.js +2 -0
  34. package/data-types/char-type.d.ts +2 -0
  35. package/data-types/char-type.js +2 -0
  36. package/data-types/circle-type.d.ts +2 -0
  37. package/data-types/circle-type.js +2 -0
  38. package/data-types/date-type.d.ts +2 -0
  39. package/data-types/date-type.js +2 -0
  40. package/data-types/float4-type.d.ts +2 -0
  41. package/data-types/float4-type.js +2 -0
  42. package/data-types/float8-type.d.ts +2 -0
  43. package/data-types/float8-type.js +2 -0
  44. package/data-types/inet-type.d.ts +4 -0
  45. package/data-types/inet-type.js +4 -0
  46. package/data-types/int2-type.d.ts +2 -0
  47. package/data-types/int2-type.js +2 -0
  48. package/data-types/int2-vector-type.d.ts +2 -0
  49. package/data-types/int2-vector-type.js +2 -0
  50. package/data-types/int4-type.d.ts +2 -0
  51. package/data-types/int4-type.js +2 -0
  52. package/data-types/int8-type.d.ts +2 -0
  53. package/data-types/int8-type.js +2 -0
  54. package/data-types/interval-type.d.ts +9 -0
  55. package/data-types/interval-type.js +9 -0
  56. package/data-types/json-type.d.ts +2 -0
  57. package/data-types/json-type.js +2 -0
  58. package/data-types/jsonb-type.d.ts +2 -0
  59. package/data-types/jsonb-type.js +2 -0
  60. package/data-types/jsonpath-type.d.ts +2 -0
  61. package/data-types/jsonpath-type.js +2 -0
  62. package/data-types/line-type.d.ts +2 -0
  63. package/data-types/line-type.js +2 -0
  64. package/data-types/lseg-type.d.ts +2 -0
  65. package/data-types/lseg-type.js +2 -0
  66. package/data-types/macaddr-type.d.ts +4 -0
  67. package/data-types/macaddr-type.js +4 -0
  68. package/data-types/money-type.d.ts +1 -0
  69. package/data-types/money-type.js +17 -10
  70. package/data-types/numeric-type.d.ts +2 -0
  71. package/data-types/numeric-type.js +2 -0
  72. package/data-types/oid-type.d.ts +2 -0
  73. package/data-types/oid-type.js +2 -0
  74. package/data-types/oid-vector-type.d.ts +1 -0
  75. package/data-types/oid-vector-type.js +1 -0
  76. package/data-types/path-type.d.ts +4 -0
  77. package/data-types/path-type.js +4 -0
  78. package/data-types/pg-lsn-type.d.ts +2 -0
  79. package/data-types/pg-lsn-type.js +2 -0
  80. package/data-types/point-type.d.ts +2 -0
  81. package/data-types/point-type.js +2 -0
  82. package/data-types/range-type.d.ts +4 -0
  83. package/data-types/range-type.js +4 -0
  84. package/data-types/snapshot-type.d.ts +4 -0
  85. package/data-types/snapshot-type.js +4 -0
  86. package/data-types/tid-type.d.ts +2 -0
  87. package/data-types/tid-type.js +2 -0
  88. package/data-types/time-type.d.ts +2 -0
  89. package/data-types/time-type.js +2 -0
  90. package/data-types/timestamp-type.d.ts +2 -0
  91. package/data-types/timestamp-type.js +2 -0
  92. package/data-types/timestamptz-type.d.ts +2 -0
  93. package/data-types/timestamptz-type.js +2 -0
  94. package/data-types/timetz-type.d.ts +2 -0
  95. package/data-types/timetz-type.js +2 -0
  96. package/data-types/tsvector-type.d.ts +4 -0
  97. package/data-types/tsvector-type.js +4 -0
  98. package/data-types/uuid-type.d.ts +2 -0
  99. package/data-types/uuid-type.js +2 -0
  100. package/data-types/varchar-type.d.ts +2 -0
  101. package/data-types/varchar-type.js +2 -0
  102. package/data-types/xid-type.d.ts +6 -0
  103. package/data-types/xid-type.js +6 -0
  104. package/interfaces/command-result.d.ts +7 -0
  105. package/interfaces/copy-from-rows-options.d.ts +4 -0
  106. package/interfaces/data-mapping-options.d.ts +6 -0
  107. package/interfaces/data-type.d.ts +37 -0
  108. package/interfaces/database-connection-params.d.ts +31 -0
  109. package/interfaces/field-info.d.ts +8 -0
  110. package/interfaces/function-call-options.d.ts +1 -0
  111. package/interfaces/function-call-result.d.ts +6 -0
  112. package/interfaces/pipeline-request.d.ts +2 -0
  113. package/interfaces/query-options.d.ts +7 -0
  114. package/interfaces/query-result.d.ts +7 -0
  115. package/interfaces/script-execute-options.d.ts +4 -0
  116. package/interfaces/script-result.d.ts +1 -0
  117. package/interfaces/statement-prepare-options.d.ts +1 -0
  118. package/package.json +5 -5
  119. package/protocol/backend.d.ts +6 -0
  120. package/protocol/backend.js +35 -5
  121. package/protocol/cert-signature.d.ts +8 -0
  122. package/protocol/cert-signature.js +8 -0
  123. package/protocol/database-error.d.ts +8 -0
  124. package/protocol/database-error.js +8 -0
  125. package/protocol/frontend.d.ts +16 -0
  126. package/protocol/frontend.js +13 -6
  127. package/protocol/pg-socket.d.ts +10 -0
  128. package/protocol/pg-socket.js +14 -7
  129. package/protocol/pgoutput.d.ts +15 -0
  130. package/protocol/pgoutput.js +7 -0
  131. package/protocol/protocol.d.ts +50 -2
  132. package/protocol/protocol.js +17 -1
  133. package/protocol/sasl.d.ts +22 -0
  134. package/protocol/sasl.js +21 -0
  135. package/protocol/smart-buffer.d.ts +4 -0
  136. package/safe-event-emitter.d.ts +7 -0
  137. package/safe-event-emitter.js +7 -0
  138. package/types.d.ts +21 -0
  139. package/util/array-calculatedim.d.ts +10 -0
  140. package/util/array-calculatedim.js +10 -0
  141. package/util/bigint-methods.d.ts +7 -0
  142. package/util/bigint-methods.js +9 -0
  143. package/util/config-from-env.d.ts +10 -0
  144. package/util/config-from-env.js +10 -0
  145. package/util/connection-config.d.ts +17 -0
  146. package/util/connection-config.js +17 -0
  147. package/util/copy-from-rows.d.ts +12 -0
  148. package/util/copy-from-rows.js +9 -0
  149. package/util/decimal-as-string.d.ts +6 -0
  150. package/util/decimal-as-string.js +6 -0
  151. package/util/decode-binaryarray.d.ts +9 -0
  152. package/util/decode-binaryarray.js +9 -0
  153. package/util/escape-literal.d.ts +11 -0
  154. package/util/escape-literal.js +13 -0
  155. package/util/fast-parseint.d.ts +14 -0
  156. package/util/fast-parseint.js +14 -0
  157. package/util/format-datetime.d.ts +26 -0
  158. package/util/format-datetime.js +26 -0
  159. package/util/get-parsers.d.ts +9 -0
  160. package/util/get-parsers.js +9 -0
  161. package/util/hex-text.d.ts +13 -21
  162. package/util/hex-text.js +24 -21
  163. package/util/local-date.d.ts +2 -41
  164. package/util/local-date.js +39 -41
  165. package/util/parse-array.d.ts +8 -0
  166. package/util/parse-array.js +8 -0
  167. package/util/parse-bytea.d.ts +15 -0
  168. package/util/parse-bytea.js +15 -0
  169. package/util/parse-datetime.d.ts +26 -0
  170. package/util/parse-datetime.js +26 -0
  171. package/util/parse-row.d.ts +23 -2
  172. package/util/parse-row.js +56 -36
  173. package/util/parse-time.d.ts +10 -0
  174. package/util/parse-time.js +10 -0
  175. package/util/resolve-column-formats.d.ts +1 -0
  176. package/util/row-decoder.d.ts +32 -0
  177. package/util/row-decoder.js +22 -0
  178. package/util/stringify-arrayliteral.d.ts +33 -0
  179. package/util/stringify-arrayliteral.js +220 -20
  180. package/util/stringify-for-sql.d.ts +17 -0
  181. package/util/stringify-for-sql.js +17 -0
  182. package/util/wrap-row-description.d.ts +10 -0
  183. package/util/wrap-row-description.js +10 -0
  184. package/CHANGELOG.md +0 -1453
@@ -18,7 +18,9 @@ import type { PreparedStatement } from './prepared-statement.js';
18
18
  * did.
19
19
  */
20
20
  export type PoolPipelineOptions = Pick<QueryOptions, 'pipeline'>;
21
+ /** What `Pool.query()` takes - a connection's own options, since it runs on one. */
21
22
  export type PoolQueryOptions = QueryOptions;
23
+ /** What `Pool.execute()` takes, for the same reason. */
22
24
  export type PoolScriptExecuteOptions = ScriptExecuteOptions;
23
25
  /**
24
26
  * One pooled connection the pipelined path is currently borrowing.
@@ -33,10 +35,37 @@ export type PoolScriptExecuteOptions = ScriptExecuteOptions;
33
35
  * reports zero running queries.
34
36
  */
35
37
  interface PipelineSlot {
38
+ /** The connection once it is open. */
36
39
  connection?: Connection;
40
+ /** What everything waiting for this slot awaits. */
37
41
  promise: Promise<Connection>;
42
+ /** Queries handed to this slot that have not settled yet. */
38
43
  load: number;
39
44
  }
45
+ /**
46
+ * A set of connections, handed out and taken back.
47
+ *
48
+ * Takes the same configuration a `Connection` does, plus how many to
49
+ * keep (`min`, `max`) and how long an idle one lives. Nothing connects
50
+ * until something is asked for.
51
+ *
52
+ * Most callers never hold a connection at all: `query()`, `execute()`
53
+ * and `transaction()` take one, use it and give it back, so the pool is
54
+ * a drop-in for a connection.
55
+ *
56
+ * ```ts
57
+ * const pool = new Pool({ host: 'localhost', database: 'mydb', max: 10 });
58
+ * const result = await pool.query('select * from customers');
59
+ * await pool.close();
60
+ * ```
61
+ *
62
+ * `acquire()` is for the cases that need one connection for several
63
+ * statements - a session setting, an advisory lock - and its connection
64
+ * must be released.
65
+ *
66
+ * Emits `connect`, `acquire`, `release`, `remove`, `close`, `error` and
67
+ * `terminate`.
68
+ */
40
69
  export declare class Pool extends SafeEventEmitter {
41
70
  protected readonly _pool: LightningPool<IntlConnection>;
42
71
  protected readonly _notificationListeners: SafeEventEmitter;
@@ -52,7 +81,13 @@ export declare class Pool extends SafeEventEmitter {
52
81
  protected readonly _destroyReasons: WeakMap<IntlConnection, Error>;
53
82
  protected _pipelineMaxQueries: number;
54
83
  protected _pipelineMaxConnections: number;
84
+ /** What this pool was built with, frozen. */
55
85
  readonly config: PoolConfiguration;
86
+ /**
87
+ * @param config How to reach the server and how many connections to
88
+ * keep - an object, a connection string, or nothing to take it all
89
+ * from the environment.
90
+ */
56
91
  constructor(config?: PoolConfiguration | string);
57
92
  /**
58
93
  * Returns the number of connections that are currently acquired
@@ -66,6 +101,13 @@ export declare class Pool extends SafeEventEmitter {
66
101
  * Returns total number of connections in the pool regardless of whether they are idle or in use
67
102
  */
68
103
  get totalConnections(): number;
104
+ /**
105
+ * Opens the `min` connections now instead of waiting for the first
106
+ * caller to need one.
107
+ *
108
+ * Never required: a pool that is only used starts itself. Worth it
109
+ * where the first query should not pay for the handshake.
110
+ */
69
111
  start(): void;
70
112
  /**
71
113
  * Obtains a connection from the connection pool
@@ -99,10 +141,48 @@ export declare class Pool extends SafeEventEmitter {
99
141
  * one. This is the way to get several statements onto the same one.
100
142
  */
101
143
  transaction<T>(fn: (connection: Connection) => Promise<T>, options?: TransactionOptions): Promise<T>;
144
+ /**
145
+ * Prepares a statement on a connection of the pool's, which that
146
+ * statement keeps until it is closed.
147
+ *
148
+ * The connection is released by `close()` on the statement, so one
149
+ * that is never closed holds a connection out of the pool for good.
150
+ *
151
+ * @param sql The statement to prepare.
152
+ * @param options Declared parameter types.
153
+ * @returns The prepared statement, ready to execute.
154
+ */
102
155
  prepare(sql: string, options?: StatementPrepareOptions): Promise<PreparedStatement>;
156
+ /**
157
+ * Gives a connection taken with `acquire()` back to the pool.
158
+ *
159
+ * @param connection The connection to release.
160
+ */
103
161
  release(connection: Connection): Promise<void>;
162
+ /**
163
+ * Subscribes to a `NOTIFY` channel on a connection the pool keeps for
164
+ * notifications.
165
+ *
166
+ * A pooled connection cannot carry a subscription - it goes back to
167
+ * the pool and the next caller would receive them - so the first
168
+ * `listen()` opens one connection outside the pool and every channel
169
+ * shares it. `unListenAll()` closes it again.
170
+ *
171
+ * @param channel The channel to subscribe to.
172
+ * @param callback Called with each notification on it.
173
+ */
104
174
  listen(channel: string, callback: NotificationCallback): Promise<void>;
175
+ /**
176
+ * Drops every callback on one channel, and closes the notification
177
+ * connection when it was the last one.
178
+ *
179
+ * @param channel The channel to unsubscribe from.
180
+ */
105
181
  unListen(channel: string): Promise<void>;
182
+ /**
183
+ * Drops every callback on every channel and closes the connection the
184
+ * pool was keeping for them.
185
+ */
106
186
  unListenAll(): Promise<void>;
107
187
  /**
108
188
  * A pooled connection reported that its socket closed. `reason` is set
@@ -8,6 +8,30 @@ import { QueryRequest } from '../util/sql-tag.js';
8
8
  import { startsCopy, startsTransaction } from '../util/starts-transaction.js';
9
9
  import { Connection } from './connection.js';
10
10
  import { getIntlConnection, IntlConnection } from './intl-connection.js';
11
+ /**
12
+ * A set of connections, handed out and taken back.
13
+ *
14
+ * Takes the same configuration a `Connection` does, plus how many to
15
+ * keep (`min`, `max`) and how long an idle one lives. Nothing connects
16
+ * until something is asked for.
17
+ *
18
+ * Most callers never hold a connection at all: `query()`, `execute()`
19
+ * and `transaction()` take one, use it and give it back, so the pool is
20
+ * a drop-in for a connection.
21
+ *
22
+ * ```ts
23
+ * const pool = new Pool({ host: 'localhost', database: 'mydb', max: 10 });
24
+ * const result = await pool.query('select * from customers');
25
+ * await pool.close();
26
+ * ```
27
+ *
28
+ * `acquire()` is for the cases that need one connection for several
29
+ * statements - a session setting, an advisory lock - and its connection
30
+ * must be released.
31
+ *
32
+ * Emits `connect`, `acquire`, `release`, `remove`, `close`, `error` and
33
+ * `terminate`.
34
+ */
11
35
  export class Pool extends SafeEventEmitter {
12
36
  _pool;
13
37
  _notificationListeners = new SafeEventEmitter();
@@ -23,7 +47,13 @@ export class Pool extends SafeEventEmitter {
23
47
  _destroyReasons = new WeakMap();
24
48
  _pipelineMaxQueries;
25
49
  _pipelineMaxConnections;
50
+ /** What this pool was built with, frozen. */
26
51
  config;
52
+ /**
53
+ * @param config How to reach the server and how many connections to
54
+ * keep - an object, a connection string, or nothing to take it all
55
+ * from the environment.
56
+ */
27
57
  constructor(config) {
28
58
  super();
29
59
  const cfg = getConnectionConfig(config);
@@ -132,6 +162,13 @@ export class Pool extends SafeEventEmitter {
132
162
  get totalConnections() {
133
163
  return this._pool.size;
134
164
  }
165
+ /**
166
+ * Opens the `min` connections now instead of waiting for the first
167
+ * caller to need one.
168
+ *
169
+ * Never required: a pool that is only used starts itself. Worth it
170
+ * where the first query should not pay for the handshake.
171
+ */
135
172
  start() {
136
173
  return this._pool.start();
137
174
  }
@@ -281,26 +318,52 @@ export class Pool extends SafeEventEmitter {
281
318
  await this.release(connection);
282
319
  }
283
320
  }
321
+ /**
322
+ * Prepares a statement on a connection of the pool's, which that
323
+ * statement keeps until it is closed.
324
+ *
325
+ * The connection is released by `close()` on the statement, so one
326
+ * that is never closed holds a connection out of the pool for good.
327
+ *
328
+ * @param sql The statement to prepare.
329
+ * @param options Declared parameter types.
330
+ * @returns The prepared statement, ready to execute.
331
+ */
284
332
  async prepare(sql, options) {
285
333
  const connection = await this.acquire();
286
334
  const statement = await connection.prepare(sql, options);
287
335
  statement.once('close', () => this._pool.release(getIntlConnection(connection)));
288
336
  return statement;
289
337
  }
338
+ /**
339
+ * Gives a connection taken with `acquire()` back to the pool.
340
+ *
341
+ * @param connection The connection to release.
342
+ */
290
343
  release(connection) {
291
344
  return this._pool.releaseAsync(getIntlConnection(connection));
292
345
  }
346
+ /**
347
+ * Subscribes to a `NOTIFY` channel on a connection the pool keeps for
348
+ * notifications.
349
+ *
350
+ * A pooled connection cannot carry a subscription - it goes back to
351
+ * the pool and the next caller would receive them - so the first
352
+ * `listen()` opens one connection outside the pool and every channel
353
+ * shares it. `unListenAll()` closes it again.
354
+ *
355
+ * @param channel The channel to subscribe to.
356
+ * @param callback Called with each notification on it.
357
+ */
358
+ /* The channel name is normalized here as well as in
359
+ Connection.listen(), because this emitter's keys have to be the same
360
+ strings the inner connection registers or unListen() would miss
361
+ them. And the test below is per channel: it used to be enough that
362
+ the shared connection existed, so a second channel added after the
363
+ first was recorded here but never reached the server - the same root
364
+ cause as the one fixed one layer up. */
293
365
  async listen(channel, callback) {
294
- // Folded here as well as in Connection.listen(): this emitter's keys
295
- // have to be the same strings the inner connection registers, or
296
- // unListen() would miss them.
297
366
  channel = normalizeChannelName(channel);
298
- // Bug: _initNotificationConnection() only ever bootstraps the shared
299
- // connection and registers every channel known at that moment - it
300
- // returns immediately once that connection already exists, so a second,
301
- // different channel added afterwards was recorded here but never
302
- // reached the server at all (same root cause fixed in
303
- // Connection.listen(), one layer up).
304
367
  const alreadyListening = !!this._notificationListeners.listenerCount(channel);
305
368
  this._notificationListeners.on(channel, callback);
306
369
  if (!this._notificationConnection) {
@@ -310,6 +373,12 @@ export class Pool extends SafeEventEmitter {
310
373
  await this._notificationConnection.listen(channel, callback);
311
374
  }
312
375
  }
376
+ /**
377
+ * Drops every callback on one channel, and closes the notification
378
+ * connection when it was the last one.
379
+ *
380
+ * @param channel The channel to unsubscribe from.
381
+ */
313
382
  async unListen(channel) {
314
383
  channel = normalizeChannelName(channel);
315
384
  this._notificationListeners.removeAllListeners(channel);
@@ -319,6 +388,10 @@ export class Pool extends SafeEventEmitter {
319
388
  else if (this._notificationConnection)
320
389
  await this._notificationConnection.unListen(channel);
321
390
  }
391
+ /**
392
+ * Drops every callback on every channel and closes the connection the
393
+ * pool was keeping for them.
394
+ */
322
395
  async unListenAll() {
323
396
  this._notificationListeners.removeAllListeners();
324
397
  if (this._notificationConnection) {
@@ -3,17 +3,40 @@ import { Protocol } from '../protocol/protocol.js';
3
3
  import type { Maybe } from '../types.js';
4
4
  import type { Connection } from './connection.js';
5
5
  import type { PreparedStatement } from './prepared-statement.js';
6
+ /** What one Execute of a portal answered with. */
6
7
  export interface PortalExecuteResult {
8
+ /**
9
+ * The message that ended it: CommandComplete when the rows ran out,
10
+ * PortalSuspended when the fetch limit stopped it first.
11
+ */
7
12
  code: Protocol.BackendMessageCode;
13
+ /** The rows, still raw - the caller decodes them. */
8
14
  rows?: Protocol.DataRowMessage[];
15
+ /** The command tag, when the portal ran to completion. */
9
16
  command?: string;
17
+ /** What the tag reported, for the commands that report one. */
10
18
  rowCount?: number;
11
19
  }
20
+ /**
21
+ * One execution of a prepared statement, named on the server so it can
22
+ * be executed a batch at a time.
23
+ *
24
+ * What a `Cursor` is built on: a portal holds the bound parameters and
25
+ * the position in the result, so `execute()` can be called again for the
26
+ * next batch. It lives inside the transaction that created it - implicit
27
+ * or otherwise - and is gone when that ends.
28
+ */
12
29
  export declare class Portal {
13
30
  private readonly _statement;
14
31
  private readonly _name?;
32
+ /**
33
+ * @param statement The statement this portal binds.
34
+ * @param name What the server will know the portal by.
35
+ */
15
36
  constructor(statement: PreparedStatement, name: string);
37
+ /** The connection the portal lives on. */
16
38
  get connection(): Connection;
39
+ /** What the server knows this portal by. */
17
40
  get name(): Maybe<string>;
18
41
  /**
19
42
  * Bind + Describe(portal) collapsed into one round trip instead of two
@@ -24,6 +47,20 @@ export declare class Portal {
24
47
  * same named portal failed with "portal ... does not exist").
25
48
  */
26
49
  bindAndRetrieveFields(params: Maybe<any[]>, queryOptions: QueryOptions, columnFormat?: Protocol.DataFormat | Protocol.DataFormat[]): Promise<Protocol.RowDescription[]>;
50
+ /**
51
+ * Runs the portal, for up to `fetchCount` rows.
52
+ *
53
+ * Called again for each batch: the result's `code` says which happened
54
+ * - PortalSuspended when the limit stopped it and there is more,
55
+ * CommandComplete when the rows ran out.
56
+ *
57
+ * @param fetchCount How many rows to ask for; every remaining row when
58
+ * it is absent or zero.
59
+ * @returns The raw rows and how the execution ended.
60
+ * @throws DatabaseError When the server refuses it - `34000` when the
61
+ * portal is gone, which is what another statement on the connection
62
+ * does to it.
63
+ */
27
64
  execute(fetchCount?: number): Promise<PortalExecuteResult>;
28
65
  /**
29
66
  * Close + Sync as one request (see PgSocket.sendCloseAndSyncMessages()
@@ -1,16 +1,31 @@
1
1
  import { GlobalTypeMap } from '../data-type-map.js';
2
2
  import { Protocol } from '../protocol/protocol.js';
3
3
  import { getIntlConnection } from './intl-connection.js';
4
+ /**
5
+ * One execution of a prepared statement, named on the server so it can
6
+ * be executed a batch at a time.
7
+ *
8
+ * What a `Cursor` is built on: a portal holds the bound parameters and
9
+ * the position in the result, so `execute()` can be called again for the
10
+ * next batch. It lives inside the transaction that created it - implicit
11
+ * or otherwise - and is gone when that ends.
12
+ */
4
13
  export class Portal {
5
14
  _statement;
6
15
  _name;
16
+ /**
17
+ * @param statement The statement this portal binds.
18
+ * @param name What the server will know the portal by.
19
+ */
7
20
  constructor(statement, name) {
8
21
  this._statement = statement;
9
22
  this._name = name;
10
23
  }
24
+ /** The connection the portal lives on. */
11
25
  get connection() {
12
26
  return this._statement.connection;
13
27
  }
28
+ /** What the server knows this portal by. */
14
29
  get name() {
15
30
  return this._name;
16
31
  }
@@ -61,6 +76,20 @@ export class Portal {
61
76
  intoCon.unref();
62
77
  }
63
78
  }
79
+ /**
80
+ * Runs the portal, for up to `fetchCount` rows.
81
+ *
82
+ * Called again for each batch: the result's `code` says which happened
83
+ * - PortalSuspended when the limit stopped it and there is more,
84
+ * CommandComplete when the rows ran out.
85
+ *
86
+ * @param fetchCount How many rows to ask for; every remaining row when
87
+ * it is absent or zero.
88
+ * @returns The raw rows and how the execution ended.
89
+ * @throws DatabaseError When the server refuses it - `34000` when the
90
+ * portal is gone, which is what another statement on the connection
91
+ * does to it.
92
+ */
64
93
  async execute(fetchCount) {
65
94
  const intoCon = getIntlConnection(this.connection);
66
95
  intoCon.ref();
@@ -5,6 +5,31 @@ import type { StatementPrepareOptions } from '../interfaces/statement-prepare-op
5
5
  import { SafeEventEmitter } from '../safe-event-emitter.js';
6
6
  import type { Maybe, OID } from '../types.js';
7
7
  import type { Connection } from './connection.js';
8
+ /**
9
+ * A statement parsed once by the server and executed as many times as
10
+ * the caller likes.
11
+ *
12
+ * Obtained from `Connection.prepare()` or `Pool.prepare()`. Each
13
+ * `execute()` then costs a Bind and an Execute instead of a full Parse,
14
+ * and the server plans it once - which is what makes it worth naming a
15
+ * statement that runs in a loop. `executeBatch()` goes further and sends
16
+ * every parameter set before waiting for any of them.
17
+ *
18
+ * ```ts
19
+ * const statement = await connection.prepare(
20
+ * 'update customers set city = $1 where id = $2',
21
+ * );
22
+ * await statement.execute({ params: ['Berlin', 1] });
23
+ * await statement.execute({ params: ['Paris', 2] });
24
+ * await statement.close();
25
+ * ```
26
+ *
27
+ * A statement holds a server-side name until `close()`, and one taken
28
+ * from a `Pool` holds that pool's connection with it, so it must be
29
+ * closed - `await using` does it at the end of the scope.
30
+ *
31
+ * Emits `close` and `notice`.
32
+ */
8
33
  export declare class PreparedStatement extends SafeEventEmitter implements AsyncDisposable {
9
34
  private readonly _connection;
10
35
  private readonly _sql;
@@ -15,11 +40,37 @@ export declare class PreparedStatement extends SafeEventEmitter implements Async
15
40
  protected _onErrorSavePoint: string;
16
41
  private _refCount;
17
42
  private _closed;
43
+ /**
44
+ * Builds the handle; `prepare()` is what sends anything.
45
+ *
46
+ * @param connection The connection this statement lives on.
47
+ * @param sql The statement text.
48
+ * @param paramTypes Declared parameter types, if the caller named any.
49
+ */
18
50
  constructor(connection: Connection, sql: string, paramTypes?: OID[]);
51
+ /**
52
+ * Parses the statement on the server and describes it, in one round
53
+ * trip, so the first `execute()` already knows what its columns are.
54
+ *
55
+ * `Connection.prepare()` is the way to call this.
56
+ *
57
+ * @param connection The connection to prepare on.
58
+ * @param sql The statement text.
59
+ * @param options Declared parameter types.
60
+ * @returns The prepared statement.
61
+ * @throws DatabaseError When the server refuses to parse it.
62
+ */
19
63
  static prepare(connection: Connection, sql: string, options?: StatementPrepareOptions): Promise<PreparedStatement>;
64
+ /** The connection this statement was prepared on. */
20
65
  get connection(): Connection;
66
+ /** The name the server knows this statement by. */
21
67
  get name(): Maybe<string>;
68
+ /** The statement text, as it was prepared. */
22
69
  get sql(): string;
70
+ /**
71
+ * The parameter types the caller declared at `prepare()`, if any -
72
+ * `resolvedParamTypes` is what the server made of them.
73
+ */
23
74
  get paramTypes(): Maybe<Maybe<OID>[]>;
24
75
  /**
25
76
  * What the server made of each parameter, in order, as its Describe
@@ -39,6 +90,16 @@ export declare class PreparedStatement extends SafeEventEmitter implements Async
39
90
  * out untyped (see `isUnspecifiedParam`).
40
91
  */
41
92
  get resolvedParamTypes(): Maybe<OID[]>;
93
+ /**
94
+ * Runs the statement once, with one set of parameters.
95
+ *
96
+ * Takes the same options `Connection.query()` does, minus the SQL -
97
+ * parameters, row shape, type mapping, `cursor`, a signal.
98
+ *
99
+ * @param options Parameters and how to read the result.
100
+ * @returns The rows, the command tag, and what the columns were.
101
+ * @throws DatabaseError When the server refuses the execution.
102
+ */
42
103
  execute(options?: QueryOptions): Promise<QueryResult>;
43
104
  /**
44
105
  * Runs this statement once per parameter set, sending every Bind/Execute
@@ -136,6 +197,13 @@ export declare class PreparedStatement extends SafeEventEmitter implements Async
136
197
  * set back independently anyway.
137
198
  */
138
199
  protected _withTransaction<T>(options: QueryOptions | undefined, fn: (inlineSavepoint?: string) => Promise<T>, canInlineSavepoint?: boolean): Promise<T>;
200
+ /**
201
+ * Closes the statement on the server and releases what it was holding
202
+ * - the connection too, when it came from a pool.
203
+ *
204
+ * Idempotent, and deferred while a cursor of this statement is still
205
+ * open. Emits `close`.
206
+ */
139
207
  close(): Promise<void>;
140
208
  /**
141
209
  * Asks the server to cancel whatever this statement's connection is
@@ -14,6 +14,31 @@ import { getIntlConnection } from './intl-connection.js';
14
14
  import { Portal } from './portal.js';
15
15
  let statementCounter = 0;
16
16
  let portalCounter = 0;
17
+ /**
18
+ * A statement parsed once by the server and executed as many times as
19
+ * the caller likes.
20
+ *
21
+ * Obtained from `Connection.prepare()` or `Pool.prepare()`. Each
22
+ * `execute()` then costs a Bind and an Execute instead of a full Parse,
23
+ * and the server plans it once - which is what makes it worth naming a
24
+ * statement that runs in a loop. `executeBatch()` goes further and sends
25
+ * every parameter set before waiting for any of them.
26
+ *
27
+ * ```ts
28
+ * const statement = await connection.prepare(
29
+ * 'update customers set city = $1 where id = $2',
30
+ * );
31
+ * await statement.execute({ params: ['Berlin', 1] });
32
+ * await statement.execute({ params: ['Paris', 2] });
33
+ * await statement.close();
34
+ * ```
35
+ *
36
+ * A statement holds a server-side name until `close()`, and one taken
37
+ * from a `Pool` holds that pool's connection with it, so it must be
38
+ * closed - `await using` does it at the end of the scope.
39
+ *
40
+ * Emits `close` and `notice`.
41
+ */
17
42
  export class PreparedStatement extends SafeEventEmitter {
18
43
  _connection;
19
44
  _sql = '';
@@ -24,6 +49,13 @@ export class PreparedStatement extends SafeEventEmitter {
24
49
  _onErrorSavePoint;
25
50
  _refCount = 0;
26
51
  _closed = false;
52
+ /**
53
+ * Builds the handle; `prepare()` is what sends anything.
54
+ *
55
+ * @param connection The connection this statement lives on.
56
+ * @param sql The statement text.
57
+ * @param paramTypes Declared parameter types, if the caller named any.
58
+ */
27
59
  constructor(connection, sql, paramTypes) {
28
60
  super();
29
61
  this._connection = connection;
@@ -32,6 +64,18 @@ export class PreparedStatement extends SafeEventEmitter {
32
64
  this._paramTypes = paramTypes;
33
65
  this._onErrorSavePoint = 'SP_' + Math.round(Math.random() * 100000000);
34
66
  }
67
+ /**
68
+ * Parses the statement on the server and describes it, in one round
69
+ * trip, so the first `execute()` already knows what its columns are.
70
+ *
71
+ * `Connection.prepare()` is the way to call this.
72
+ *
73
+ * @param connection The connection to prepare on.
74
+ * @param sql The statement text.
75
+ * @param options Declared parameter types.
76
+ * @returns The prepared statement.
77
+ * @throws DatabaseError When the server refuses to parse it.
78
+ */
35
79
  static async prepare(connection, sql, options) {
36
80
  const intoCon = getIntlConnection(connection);
37
81
  intoCon.assertConnected();
@@ -42,15 +86,22 @@ export class PreparedStatement extends SafeEventEmitter {
42
86
  statement._refCount = 1;
43
87
  return statement;
44
88
  }
89
+ /** The connection this statement was prepared on. */
45
90
  get connection() {
46
91
  return this._connection;
47
92
  }
93
+ /** The name the server knows this statement by. */
48
94
  get name() {
49
95
  return this._name;
50
96
  }
97
+ /** The statement text, as it was prepared. */
51
98
  get sql() {
52
99
  return this._sql;
53
100
  }
101
+ /**
102
+ * The parameter types the caller declared at `prepare()`, if any -
103
+ * `resolvedParamTypes` is what the server made of them.
104
+ */
54
105
  get paramTypes() {
55
106
  return this._paramTypes;
56
107
  }
@@ -74,10 +125,21 @@ export class PreparedStatement extends SafeEventEmitter {
74
125
  get resolvedParamTypes() {
75
126
  return this._resolvedParamTypes;
76
127
  }
128
+ /**
129
+ * Runs the statement once, with one set of parameters.
130
+ *
131
+ * Takes the same options `Connection.query()` does, minus the SQL -
132
+ * parameters, row shape, type mapping, `cursor`, a signal.
133
+ *
134
+ * @param options Parameters and how to read the result.
135
+ * @returns The rows, the command tag, and what the columns were.
136
+ * @throws DatabaseError When the server refuses the execution.
137
+ */
138
+ /* The options are merged with the connection's defaults here rather
139
+ than deeper down, because a cursor keeps them and decodes its own
140
+ rows with them long after this call is over. */
77
141
  async execute(options = {}) {
78
142
  const intlCon = getIntlConnection(this.connection);
79
- // Folded in here rather than deeper: a cursor keeps these options
80
- // and decodes its own rows with them long after this call is over.
81
143
  options = intlCon.withDefaults(options);
82
144
  if (options.signal)
83
145
  return withAbortSignal(options.signal, () => intlCon.cancel(), () => this._executeWithTransaction(options));
@@ -287,6 +349,13 @@ export class PreparedStatement extends SafeEventEmitter {
287
349
  throw e;
288
350
  }
289
351
  }
352
+ /**
353
+ * Closes the statement on the server and releases what it was holding
354
+ * - the connection too, when it came from a pool.
355
+ *
356
+ * Idempotent, and deferred while a cursor of this statement is still
357
+ * open. Emits `close`.
358
+ */
290
359
  async close() {
291
360
  if (this._closed)
292
361
  return;
package/constants.d.ts CHANGED
@@ -1,7 +1,16 @@
1
1
  import { Protocol } from './protocol/protocol.js';
2
2
  import DataFormat = Protocol.DataFormat;
3
+ export declare const version = "3.12.2";
3
4
  export { DataFormat };
5
+ /**
6
+ * What columns come back in unless something asks otherwise.
7
+ *
8
+ * Binary: fewer bytes on the wire and no text to parse for most types.
9
+ * A column named by `fetchAsString`, and the types that have no binary
10
+ * decoder, come back as text regardless.
11
+ */
4
12
  export declare const DEFAULT_COLUMN_FORMAT = DataFormat.binary;
13
+ /** Where a connection is between `connect()` and `close()`. */
5
14
  export declare enum ConnectionState {
6
15
  CLOSED = 0,
7
16
  CONNECTING = 1,
@@ -9,6 +18,14 @@ export declare enum ConnectionState {
9
18
  READY = 2,
10
19
  CLOSING = 10
11
20
  }
21
+ /**
22
+ * Every type this client knows, by name, as `pg_type.oid` gives it - the
23
+ * scalar types and their array counterparts, which are named with a
24
+ * leading underscore the way the catalog names them.
25
+ *
26
+ * These are the numbers `BindParam`, `fetchAsString` and a
27
+ * `DataTypeMap`'s own registrations are written with.
28
+ */
12
29
  export declare const DataTypeOIDs: {
13
30
  bool: number;
14
31
  bytea: number;
@@ -185,4 +202,5 @@ export declare const DataTypeOIDs: {
185
202
  _datemultirange: number;
186
203
  _int8multirange: number;
187
204
  };
205
+ /** The name of each type by its OID, for reporting a column's type back to a caller. */
188
206
  export declare const DataTypeNames: Record<string, string>;
package/constants.js CHANGED
@@ -2,8 +2,17 @@ import { Protocol } from './protocol/protocol.js';
2
2
  var DataFormat = Protocol.DataFormat;
3
3
  // @ts-ignore
4
4
  Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose');
5
+ export const version = '3.12.2';
5
6
  export { DataFormat };
7
+ /**
8
+ * What columns come back in unless something asks otherwise.
9
+ *
10
+ * Binary: fewer bytes on the wire and no text to parse for most types.
11
+ * A column named by `fetchAsString`, and the types that have no binary
12
+ * decoder, come back as text regardless.
13
+ */
6
14
  export const DEFAULT_COLUMN_FORMAT = DataFormat.binary;
15
+ /** Where a connection is between `connect()` and `close()`. */
7
16
  export var ConnectionState;
8
17
  (function (ConnectionState) {
9
18
  ConnectionState[ConnectionState["CLOSED"] = 0] = "CLOSED";
@@ -12,6 +21,14 @@ export var ConnectionState;
12
21
  ConnectionState[ConnectionState["READY"] = 2] = "READY";
13
22
  ConnectionState[ConnectionState["CLOSING"] = 10] = "CLOSING";
14
23
  })(ConnectionState || (ConnectionState = {}));
24
+ /**
25
+ * Every type this client knows, by name, as `pg_type.oid` gives it - the
26
+ * scalar types and their array counterparts, which are named with a
27
+ * leading underscore the way the catalog names them.
28
+ *
29
+ * These are the numbers `BindParam`, `fetchAsString` and a
30
+ * `DataTypeMap`'s own registrations are written with.
31
+ */
15
32
  export const DataTypeOIDs = {
16
33
  bool: 16,
17
34
  bytea: 17,
@@ -188,6 +205,7 @@ export const DataTypeOIDs = {
188
205
  _datemultirange: 6155,
189
206
  _int8multirange: 6157,
190
207
  };
208
+ /** The name of each type by its OID, for reporting a column's type back to a caller. */
191
209
  export const DataTypeNames = {
192
210
  [DataTypeOIDs.bool]: 'bool',
193
211
  [DataTypeOIDs.bytea]: 'bytea',