postgrejs 3.11.1 → 3.12.1

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 (187) hide show
  1. package/connection/bind-param.d.ts +21 -0
  2. package/connection/bind-param.js +21 -0
  3. package/connection/connection.d.ts +136 -0
  4. package/connection/connection.js +132 -5
  5. package/connection/copy-stream.d.ts +2 -0
  6. package/connection/copy-stream.js +2 -0
  7. package/connection/cursor.d.ts +67 -5
  8. package/connection/cursor.js +88 -15
  9. package/connection/intl-connection.d.ts +22 -0
  10. package/connection/intl-connection.js +107 -17
  11. package/connection/large-object.d.ts +18 -0
  12. package/connection/large-object.js +17 -0
  13. package/connection/logical-replication.d.ts +15 -0
  14. package/connection/logical-replication.js +6 -0
  15. package/connection/pool.d.ts +80 -0
  16. package/connection/pool.js +82 -9
  17. package/connection/portal.d.ts +37 -0
  18. package/connection/portal.js +29 -0
  19. package/connection/prepared-statement.d.ts +109 -0
  20. package/connection/prepared-statement.js +150 -7
  21. package/constants.d.ts +18 -0
  22. package/constants.js +18 -0
  23. package/data-type-map.d.ts +38 -0
  24. package/data-type-map.js +38 -0
  25. package/data-types/bit-type.d.ts +4 -0
  26. package/data-types/bit-type.js +4 -0
  27. package/data-types/bool-type.d.ts +2 -0
  28. package/data-types/bool-type.js +2 -0
  29. package/data-types/box-type.d.ts +2 -0
  30. package/data-types/box-type.js +2 -0
  31. package/data-types/bytea-type.d.ts +2 -0
  32. package/data-types/bytea-type.js +2 -0
  33. package/data-types/char-type.d.ts +2 -0
  34. package/data-types/char-type.js +2 -0
  35. package/data-types/circle-type.d.ts +2 -0
  36. package/data-types/circle-type.js +2 -0
  37. package/data-types/date-type.d.ts +2 -0
  38. package/data-types/date-type.js +2 -0
  39. package/data-types/float4-type.d.ts +2 -0
  40. package/data-types/float4-type.js +2 -0
  41. package/data-types/float8-type.d.ts +2 -0
  42. package/data-types/float8-type.js +2 -0
  43. package/data-types/inet-type.d.ts +4 -0
  44. package/data-types/inet-type.js +4 -0
  45. package/data-types/int2-type.d.ts +2 -0
  46. package/data-types/int2-type.js +3 -1
  47. package/data-types/int2-vector-type.d.ts +2 -0
  48. package/data-types/int2-vector-type.js +2 -0
  49. package/data-types/int4-type.d.ts +2 -0
  50. package/data-types/int4-type.js +3 -1
  51. package/data-types/int8-type.d.ts +2 -0
  52. package/data-types/int8-type.js +4 -2
  53. package/data-types/interval-type.d.ts +9 -0
  54. package/data-types/interval-type.js +9 -0
  55. package/data-types/json-type.d.ts +2 -0
  56. package/data-types/json-type.js +2 -0
  57. package/data-types/jsonb-type.d.ts +2 -0
  58. package/data-types/jsonb-type.js +2 -0
  59. package/data-types/jsonpath-type.d.ts +2 -0
  60. package/data-types/jsonpath-type.js +2 -0
  61. package/data-types/line-type.d.ts +2 -0
  62. package/data-types/line-type.js +2 -0
  63. package/data-types/lseg-type.d.ts +2 -0
  64. package/data-types/lseg-type.js +2 -0
  65. package/data-types/macaddr-type.d.ts +4 -0
  66. package/data-types/macaddr-type.js +4 -0
  67. package/data-types/money-type.d.ts +1 -0
  68. package/data-types/money-type.js +17 -10
  69. package/data-types/numeric-type.d.ts +2 -0
  70. package/data-types/numeric-type.js +2 -0
  71. package/data-types/oid-type.d.ts +2 -0
  72. package/data-types/oid-type.js +3 -1
  73. package/data-types/oid-vector-type.d.ts +1 -0
  74. package/data-types/oid-vector-type.js +1 -0
  75. package/data-types/path-type.d.ts +4 -0
  76. package/data-types/path-type.js +4 -0
  77. package/data-types/pg-lsn-type.d.ts +2 -0
  78. package/data-types/pg-lsn-type.js +2 -0
  79. package/data-types/point-type.d.ts +2 -0
  80. package/data-types/point-type.js +2 -0
  81. package/data-types/range-type.d.ts +4 -0
  82. package/data-types/range-type.js +4 -0
  83. package/data-types/snapshot-type.d.ts +4 -0
  84. package/data-types/snapshot-type.js +4 -0
  85. package/data-types/tid-type.d.ts +2 -0
  86. package/data-types/tid-type.js +2 -0
  87. package/data-types/time-type.d.ts +2 -0
  88. package/data-types/time-type.js +2 -0
  89. package/data-types/timestamp-type.d.ts +2 -0
  90. package/data-types/timestamp-type.js +2 -0
  91. package/data-types/timestamptz-type.d.ts +2 -0
  92. package/data-types/timestamptz-type.js +2 -0
  93. package/data-types/timetz-type.d.ts +2 -0
  94. package/data-types/timetz-type.js +2 -0
  95. package/data-types/tsvector-type.d.ts +4 -0
  96. package/data-types/tsvector-type.js +4 -0
  97. package/data-types/uuid-type.d.ts +2 -0
  98. package/data-types/uuid-type.js +2 -0
  99. package/data-types/varchar-type.d.ts +2 -0
  100. package/data-types/varchar-type.js +2 -0
  101. package/data-types/xid-type.d.ts +6 -0
  102. package/data-types/xid-type.js +8 -1
  103. package/interfaces/command-result.d.ts +7 -0
  104. package/interfaces/copy-from-rows-options.d.ts +4 -0
  105. package/interfaces/data-mapping-options.d.ts +6 -0
  106. package/interfaces/data-type.d.ts +37 -0
  107. package/interfaces/database-connection-params.d.ts +31 -0
  108. package/interfaces/field-info.d.ts +8 -0
  109. package/interfaces/function-call-options.d.ts +1 -0
  110. package/interfaces/function-call-result.d.ts +6 -0
  111. package/interfaces/pipeline-request.d.ts +2 -0
  112. package/interfaces/query-options.d.ts +7 -0
  113. package/interfaces/query-result.d.ts +7 -0
  114. package/interfaces/script-execute-options.d.ts +4 -0
  115. package/interfaces/script-result.d.ts +1 -0
  116. package/interfaces/statement-prepare-options.d.ts +1 -0
  117. package/package.json +5 -5
  118. package/protocol/backend.d.ts +6 -0
  119. package/protocol/backend.js +35 -5
  120. package/protocol/cert-signature.d.ts +8 -0
  121. package/protocol/cert-signature.js +8 -0
  122. package/protocol/database-error.d.ts +8 -0
  123. package/protocol/database-error.js +8 -0
  124. package/protocol/frontend.d.ts +16 -0
  125. package/protocol/frontend.js +13 -6
  126. package/protocol/pg-socket.d.ts +10 -0
  127. package/protocol/pg-socket.js +14 -7
  128. package/protocol/pgoutput.d.ts +15 -0
  129. package/protocol/pgoutput.js +7 -0
  130. package/protocol/protocol.d.ts +50 -2
  131. package/protocol/protocol.js +17 -1
  132. package/protocol/sasl.d.ts +22 -0
  133. package/protocol/sasl.js +21 -0
  134. package/protocol/smart-buffer.d.ts +4 -0
  135. package/safe-event-emitter.d.ts +7 -0
  136. package/safe-event-emitter.js +7 -0
  137. package/types.d.ts +21 -0
  138. package/util/array-calculatedim.d.ts +10 -0
  139. package/util/array-calculatedim.js +10 -0
  140. package/util/assert-integer.d.ts +40 -25
  141. package/util/assert-integer.js +97 -32
  142. package/util/bigint-methods.d.ts +7 -0
  143. package/util/bigint-methods.js +9 -0
  144. package/util/config-from-env.d.ts +10 -0
  145. package/util/config-from-env.js +10 -0
  146. package/util/connection-config.d.ts +17 -0
  147. package/util/connection-config.js +17 -0
  148. package/util/copy-from-rows.d.ts +12 -0
  149. package/util/copy-from-rows.js +9 -0
  150. package/util/decimal-as-string.d.ts +6 -0
  151. package/util/decimal-as-string.js +6 -0
  152. package/util/decode-binaryarray.d.ts +9 -0
  153. package/util/decode-binaryarray.js +9 -0
  154. package/util/escape-literal.d.ts +11 -0
  155. package/util/escape-literal.js +13 -0
  156. package/util/fast-parseint.d.ts +14 -0
  157. package/util/fast-parseint.js +14 -0
  158. package/util/format-datetime.d.ts +26 -0
  159. package/util/format-datetime.js +26 -0
  160. package/util/get-parsers.d.ts +9 -0
  161. package/util/get-parsers.js +9 -0
  162. package/util/hex-text.d.ts +13 -21
  163. package/util/hex-text.js +24 -21
  164. package/util/local-date.d.ts +2 -41
  165. package/util/local-date.js +39 -41
  166. package/util/parse-array.d.ts +8 -0
  167. package/util/parse-array.js +8 -0
  168. package/util/parse-bytea.d.ts +15 -0
  169. package/util/parse-bytea.js +15 -0
  170. package/util/parse-datetime.d.ts +26 -0
  171. package/util/parse-datetime.js +26 -0
  172. package/util/parse-row.d.ts +23 -2
  173. package/util/parse-row.js +56 -36
  174. package/util/parse-time.d.ts +10 -0
  175. package/util/parse-time.js +10 -0
  176. package/util/resolve-column-formats.d.ts +1 -0
  177. package/util/row-decoder.d.ts +32 -0
  178. package/util/row-decoder.js +22 -0
  179. package/util/stringify-arrayliteral.d.ts +33 -0
  180. package/util/stringify-arrayliteral.js +242 -7
  181. package/util/stringify-for-sql.d.ts +17 -0
  182. package/util/stringify-for-sql.js +17 -0
  183. package/util/unspecified-param.d.ts +26 -2
  184. package/util/unspecified-param.js +26 -2
  185. package/util/wrap-row-description.d.ts +10 -0
  186. package/util/wrap-row-description.js +10 -0
  187. package/CHANGELOG.md +0 -1426
@@ -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,21 +5,101 @@ 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;
11
36
  private readonly _name;
12
37
  private readonly _paramTypes?;
38
+ private _resolvedParamTypes?;
13
39
  private _fields?;
14
40
  protected _onErrorSavePoint: string;
15
41
  private _refCount;
16
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
+ */
17
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
+ */
18
63
  static prepare(connection: Connection, sql: string, options?: StatementPrepareOptions): Promise<PreparedStatement>;
64
+ /** The connection this statement was prepared on. */
19
65
  get connection(): Connection;
66
+ /** The name the server knows this statement by. */
20
67
  get name(): Maybe<string>;
68
+ /** The statement text, as it was prepared. */
21
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
+ */
22
74
  get paramTypes(): Maybe<Maybe<OID>[]>;
75
+ /**
76
+ * What the server made of each parameter, in order, as its Describe
77
+ * answered - not what was asked for in `paramTypes`.
78
+ *
79
+ * A parameter the caller declared nothing for is resolved by the
80
+ * server from where it appears: the column it is inserted into, the
81
+ * operator it sits beside, the function it is passed to. It never
82
+ * looks at the value, which is still three messages away, so this is
83
+ * a property of the statement and does not change between executions.
84
+ *
85
+ * Useful for two things. It says what a parameter with no context
86
+ * became - `select $1` resolves to `text`, which is why an array
87
+ * there comes back as an array literal rather than an array. And it
88
+ * is the type to name with `new BindParam(oid, v)` for a caller who
89
+ * wants the binary encoding a large numeric array gives up by going
90
+ * out untyped (see `isUnspecifiedParam`).
91
+ */
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
+ */
23
103
  execute(options?: QueryOptions): Promise<QueryResult>;
24
104
  /**
25
105
  * Runs this statement once per parameter set, sending every Bind/Execute
@@ -78,6 +158,28 @@ export declare class PreparedStatement extends SafeEventEmitter implements Async
78
158
  * holding a `Connection` never saw. Hard to provoke - `DEALLOCATE`
79
159
  * does not normally raise one - which is why there is no test for it.
80
160
  */
161
+ /**
162
+ * Takes the value out of a `BindParam`, and refuses one that names a
163
+ * type this statement is not.
164
+ *
165
+ * A prepared statement's parameter types are fixed by its Parse, so a
166
+ * `BindParam` here cannot change one: the Bind's format code says
167
+ * binary or text, and the server decodes with the type it already
168
+ * resolved. Naming a different one and being believed would read the
169
+ * bytes as something else - which is why the mismatch is an error
170
+ * rather than something to quietly honour.
171
+ *
172
+ * It used to be neither: nothing unwrapped the object, so it reached
173
+ * the encoder as a value with no type of its own and went out as
174
+ * `'' + v`. A text column took `"[object Object]"` and said nothing.
175
+ *
176
+ * What it is compared against is what the caller declared at
177
+ * `prepare()`, or failing that what the server resolved - which is the
178
+ * same type either way, and is why an agreeing `BindParam` is simply
179
+ * unwrapped. Where there is nothing to compare against, the value is
180
+ * taken as given.
181
+ */
182
+ protected _unwrapBindParams(params: Maybe<any[]>): Maybe<any[]>;
81
183
  protected _emitNotice(msg: any): void;
82
184
  protected _executeBatchWithTransaction(paramSets: any[][], options: QueryOptions): Promise<BatchResult>;
83
185
  protected _executeWithTransaction(options?: QueryOptions): Promise<QueryResult>;
@@ -95,6 +197,13 @@ export declare class PreparedStatement extends SafeEventEmitter implements Async
95
197
  * set back independently anyway.
96
198
  */
97
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
+ */
98
207
  close(): Promise<void>;
99
208
  /**
100
209
  * Asks the server to cancel whatever this statement's connection is