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.
- package/connection/bind-param.d.ts +21 -0
- package/connection/bind-param.js +21 -0
- package/connection/connection.d.ts +136 -0
- package/connection/connection.js +132 -5
- package/connection/copy-stream.d.ts +2 -0
- package/connection/copy-stream.js +2 -0
- package/connection/cursor.d.ts +67 -5
- package/connection/cursor.js +88 -15
- package/connection/intl-connection.d.ts +22 -0
- package/connection/intl-connection.js +107 -17
- package/connection/large-object.d.ts +18 -0
- package/connection/large-object.js +17 -0
- package/connection/logical-replication.d.ts +15 -0
- package/connection/logical-replication.js +6 -0
- package/connection/pool.d.ts +80 -0
- package/connection/pool.js +82 -9
- package/connection/portal.d.ts +37 -0
- package/connection/portal.js +29 -0
- package/connection/prepared-statement.d.ts +109 -0
- package/connection/prepared-statement.js +150 -7
- package/constants.d.ts +18 -0
- package/constants.js +18 -0
- package/data-type-map.d.ts +38 -0
- package/data-type-map.js +38 -0
- package/data-types/bit-type.d.ts +4 -0
- package/data-types/bit-type.js +4 -0
- package/data-types/bool-type.d.ts +2 -0
- package/data-types/bool-type.js +2 -0
- package/data-types/box-type.d.ts +2 -0
- package/data-types/box-type.js +2 -0
- package/data-types/bytea-type.d.ts +2 -0
- package/data-types/bytea-type.js +2 -0
- package/data-types/char-type.d.ts +2 -0
- package/data-types/char-type.js +2 -0
- package/data-types/circle-type.d.ts +2 -0
- package/data-types/circle-type.js +2 -0
- package/data-types/date-type.d.ts +2 -0
- package/data-types/date-type.js +2 -0
- package/data-types/float4-type.d.ts +2 -0
- package/data-types/float4-type.js +2 -0
- package/data-types/float8-type.d.ts +2 -0
- package/data-types/float8-type.js +2 -0
- package/data-types/inet-type.d.ts +4 -0
- package/data-types/inet-type.js +4 -0
- package/data-types/int2-type.d.ts +2 -0
- package/data-types/int2-type.js +3 -1
- package/data-types/int2-vector-type.d.ts +2 -0
- package/data-types/int2-vector-type.js +2 -0
- package/data-types/int4-type.d.ts +2 -0
- package/data-types/int4-type.js +3 -1
- package/data-types/int8-type.d.ts +2 -0
- package/data-types/int8-type.js +4 -2
- package/data-types/interval-type.d.ts +9 -0
- package/data-types/interval-type.js +9 -0
- package/data-types/json-type.d.ts +2 -0
- package/data-types/json-type.js +2 -0
- package/data-types/jsonb-type.d.ts +2 -0
- package/data-types/jsonb-type.js +2 -0
- package/data-types/jsonpath-type.d.ts +2 -0
- package/data-types/jsonpath-type.js +2 -0
- package/data-types/line-type.d.ts +2 -0
- package/data-types/line-type.js +2 -0
- package/data-types/lseg-type.d.ts +2 -0
- package/data-types/lseg-type.js +2 -0
- package/data-types/macaddr-type.d.ts +4 -0
- package/data-types/macaddr-type.js +4 -0
- package/data-types/money-type.d.ts +1 -0
- package/data-types/money-type.js +17 -10
- package/data-types/numeric-type.d.ts +2 -0
- package/data-types/numeric-type.js +2 -0
- package/data-types/oid-type.d.ts +2 -0
- package/data-types/oid-type.js +3 -1
- package/data-types/oid-vector-type.d.ts +1 -0
- package/data-types/oid-vector-type.js +1 -0
- package/data-types/path-type.d.ts +4 -0
- package/data-types/path-type.js +4 -0
- package/data-types/pg-lsn-type.d.ts +2 -0
- package/data-types/pg-lsn-type.js +2 -0
- package/data-types/point-type.d.ts +2 -0
- package/data-types/point-type.js +2 -0
- package/data-types/range-type.d.ts +4 -0
- package/data-types/range-type.js +4 -0
- package/data-types/snapshot-type.d.ts +4 -0
- package/data-types/snapshot-type.js +4 -0
- package/data-types/tid-type.d.ts +2 -0
- package/data-types/tid-type.js +2 -0
- package/data-types/time-type.d.ts +2 -0
- package/data-types/time-type.js +2 -0
- package/data-types/timestamp-type.d.ts +2 -0
- package/data-types/timestamp-type.js +2 -0
- package/data-types/timestamptz-type.d.ts +2 -0
- package/data-types/timestamptz-type.js +2 -0
- package/data-types/timetz-type.d.ts +2 -0
- package/data-types/timetz-type.js +2 -0
- package/data-types/tsvector-type.d.ts +4 -0
- package/data-types/tsvector-type.js +4 -0
- package/data-types/uuid-type.d.ts +2 -0
- package/data-types/uuid-type.js +2 -0
- package/data-types/varchar-type.d.ts +2 -0
- package/data-types/varchar-type.js +2 -0
- package/data-types/xid-type.d.ts +6 -0
- package/data-types/xid-type.js +8 -1
- package/interfaces/command-result.d.ts +7 -0
- package/interfaces/copy-from-rows-options.d.ts +4 -0
- package/interfaces/data-mapping-options.d.ts +6 -0
- package/interfaces/data-type.d.ts +37 -0
- package/interfaces/database-connection-params.d.ts +31 -0
- package/interfaces/field-info.d.ts +8 -0
- package/interfaces/function-call-options.d.ts +1 -0
- package/interfaces/function-call-result.d.ts +6 -0
- package/interfaces/pipeline-request.d.ts +2 -0
- package/interfaces/query-options.d.ts +7 -0
- package/interfaces/query-result.d.ts +7 -0
- package/interfaces/script-execute-options.d.ts +4 -0
- package/interfaces/script-result.d.ts +1 -0
- package/interfaces/statement-prepare-options.d.ts +1 -0
- package/package.json +5 -5
- package/protocol/backend.d.ts +6 -0
- package/protocol/backend.js +35 -5
- package/protocol/cert-signature.d.ts +8 -0
- package/protocol/cert-signature.js +8 -0
- package/protocol/database-error.d.ts +8 -0
- package/protocol/database-error.js +8 -0
- package/protocol/frontend.d.ts +16 -0
- package/protocol/frontend.js +13 -6
- package/protocol/pg-socket.d.ts +10 -0
- package/protocol/pg-socket.js +14 -7
- package/protocol/pgoutput.d.ts +15 -0
- package/protocol/pgoutput.js +7 -0
- package/protocol/protocol.d.ts +50 -2
- package/protocol/protocol.js +17 -1
- package/protocol/sasl.d.ts +22 -0
- package/protocol/sasl.js +21 -0
- package/protocol/smart-buffer.d.ts +4 -0
- package/safe-event-emitter.d.ts +7 -0
- package/safe-event-emitter.js +7 -0
- package/types.d.ts +21 -0
- package/util/array-calculatedim.d.ts +10 -0
- package/util/array-calculatedim.js +10 -0
- package/util/assert-integer.d.ts +40 -25
- package/util/assert-integer.js +97 -32
- package/util/bigint-methods.d.ts +7 -0
- package/util/bigint-methods.js +9 -0
- package/util/config-from-env.d.ts +10 -0
- package/util/config-from-env.js +10 -0
- package/util/connection-config.d.ts +17 -0
- package/util/connection-config.js +17 -0
- package/util/copy-from-rows.d.ts +12 -0
- package/util/copy-from-rows.js +9 -0
- package/util/decimal-as-string.d.ts +6 -0
- package/util/decimal-as-string.js +6 -0
- package/util/decode-binaryarray.d.ts +9 -0
- package/util/decode-binaryarray.js +9 -0
- package/util/escape-literal.d.ts +11 -0
- package/util/escape-literal.js +13 -0
- package/util/fast-parseint.d.ts +14 -0
- package/util/fast-parseint.js +14 -0
- package/util/format-datetime.d.ts +26 -0
- package/util/format-datetime.js +26 -0
- package/util/get-parsers.d.ts +9 -0
- package/util/get-parsers.js +9 -0
- package/util/hex-text.d.ts +13 -21
- package/util/hex-text.js +24 -21
- package/util/local-date.d.ts +2 -41
- package/util/local-date.js +39 -41
- package/util/parse-array.d.ts +8 -0
- package/util/parse-array.js +8 -0
- package/util/parse-bytea.d.ts +15 -0
- package/util/parse-bytea.js +15 -0
- package/util/parse-datetime.d.ts +26 -0
- package/util/parse-datetime.js +26 -0
- package/util/parse-row.d.ts +23 -2
- package/util/parse-row.js +56 -36
- package/util/parse-time.d.ts +10 -0
- package/util/parse-time.js +10 -0
- package/util/resolve-column-formats.d.ts +1 -0
- package/util/row-decoder.d.ts +32 -0
- package/util/row-decoder.js +22 -0
- package/util/stringify-arrayliteral.d.ts +33 -0
- package/util/stringify-arrayliteral.js +242 -7
- package/util/stringify-for-sql.d.ts +17 -0
- package/util/stringify-for-sql.js +17 -0
- package/util/unspecified-param.d.ts +26 -2
- package/util/unspecified-param.js +26 -2
- package/util/wrap-row-description.d.ts +10 -0
- package/util/wrap-row-description.js +10 -0
- package/CHANGELOG.md +0 -1426
package/types.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import type { DataMappingOptions } from './interfaces/data-mapping-options.js';
|
|
2
2
|
import type { SmartBuffer } from './protocol/smart-buffer.js';
|
|
3
|
+
/** A PostgreSQL type identifier, as `pg_type.oid` gives it. */
|
|
3
4
|
export type OID = number;
|
|
5
|
+
/** `T`, or nothing. */
|
|
4
6
|
export type Maybe<T> = T | undefined;
|
|
7
|
+
/** `T`, or SQL NULL. */
|
|
5
8
|
export type Nullable<T> = T | null;
|
|
9
|
+
/** One decoded row - an array, an object, or whatever a custom decoder returns. */
|
|
6
10
|
export type Row = any;
|
|
11
|
+
/** The node-style callback the stream-facing methods accept. */
|
|
7
12
|
export type Callback = (err?: Error | null) => void;
|
|
8
13
|
/**
|
|
9
14
|
* What a DataType's own `decodeBinary` implements. `buf` is a shared
|
|
@@ -18,12 +23,28 @@ export type Callback = (err?: Error | null) => void;
|
|
|
18
23
|
* end of the buffer" must not.
|
|
19
24
|
*/
|
|
20
25
|
export type DecodeBinaryFunction = (buf: Buffer, offset: number, len: number, options: DataMappingOptions & Record<string, any>) => any;
|
|
26
|
+
/**
|
|
27
|
+
* What a DataType's own `decodeTextBuffer` implements: the same value
|
|
28
|
+
* `decodeText` would be given, still as bytes in the shared row buffer.
|
|
29
|
+
*/
|
|
21
30
|
export type DecodeTextBufferFunction = (buf: Buffer, offset: number, len: number, options: DataMappingOptions & Record<string, any>) => any;
|
|
31
|
+
/** What a DataType's own `encodeBinary` implements: writes the value into the send buffer. */
|
|
22
32
|
export type EncodeBinaryFunction = (buf: SmartBuffer, v: any, options: DataMappingOptions) => void;
|
|
33
|
+
/** Measures an array's dimensions, for a type whose own values are arrays. */
|
|
23
34
|
export type EncodeCalculateDimFunction = (v: any[]) => number[];
|
|
35
|
+
/** Whether this value should go out as SQL NULL instead of being encoded. */
|
|
24
36
|
export type EncodeAsNullFunction = (v: any, options: DataMappingOptions) => boolean;
|
|
37
|
+
/** What a DataType's own `decodeText` implements: reads the value out of the server's text. */
|
|
25
38
|
export type DecodeTextFunction = (v: any, options: DataMappingOptions) => any;
|
|
39
|
+
/** What a DataType's own `encodeText` implements: renders the value as text the server parses. */
|
|
26
40
|
export type EncodeTextFunction = (v: any, options: DataMappingOptions) => string;
|
|
41
|
+
/**
|
|
42
|
+
* One column's parser, already chosen for the format that column arrives
|
|
43
|
+
* in: called with the whole row's buffer and where this value sits in
|
|
44
|
+
* it.
|
|
45
|
+
*/
|
|
27
46
|
export type ResolvedColumnParser = (data: Buffer, offset: number, len: number, options: DataMappingOptions & Record<string, any>) => any;
|
|
47
|
+
/** Any of the decode functions, which all share the column parser's shape. */
|
|
28
48
|
export type AnyParseFunction = ResolvedColumnParser;
|
|
49
|
+
/** What `debugLogger` is called with - a namespace, then `util.format` arguments. */
|
|
29
50
|
export type DebugLogger = (namespace: string, format: any, ...args: any[]) => void;
|
|
@@ -1 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dimensions of a nested array, each level sized to its longest row.
|
|
3
|
+
*
|
|
4
|
+
* PostgreSQL arrays are rectangular: every row of a level has the same
|
|
5
|
+
* length. A ragged JavaScript array is squared off against these numbers
|
|
6
|
+
* by the writers, with the missing elements written as NULL.
|
|
7
|
+
*
|
|
8
|
+
* @param arr The array to measure.
|
|
9
|
+
* @returns One length per level; `[0]` for an empty array.
|
|
10
|
+
*/
|
|
1
11
|
export declare function arrayCalculateDim(arr: any[]): number[];
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dimensions of a nested array, each level sized to its longest row.
|
|
3
|
+
*
|
|
4
|
+
* PostgreSQL arrays are rectangular: every row of a level has the same
|
|
5
|
+
* length. A ragged JavaScript array is squared off against these numbers
|
|
6
|
+
* by the writers, with the missing elements written as NULL.
|
|
7
|
+
*
|
|
8
|
+
* @param arr The array to measure.
|
|
9
|
+
* @returns One length per level; `[0]` for an empty array.
|
|
10
|
+
*/
|
|
1
11
|
export function arrayCalculateDim(arr) {
|
|
2
12
|
if (!arr || arr.length === 0)
|
|
3
13
|
return [0];
|
package/util/assert-integer.d.ts
CHANGED
|
@@ -1,22 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Returns `v`
|
|
2
|
+
* Returns `v` as the integer an integer column can store, or throws.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* The rule is that the binary encoding must accept exactly what the text
|
|
5
|
+
* one does and refuse exactly what the server refuses, because which of
|
|
6
|
+
* the two a value goes out as is not the caller's choice: it depends on
|
|
7
|
+
* whether the parameter's type was declared, which statement cache the
|
|
8
|
+
* query hit, and what the server resolved. A value that means one thing
|
|
9
|
+
* through one path and another through the other is the worst outcome
|
|
10
|
+
* there is, and that is what this used to be:
|
|
9
11
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* ```
|
|
13
|
+
* text binary (before)
|
|
14
|
+
* [1.5] -> int4[] 22P02 error stored 1
|
|
15
|
+
* [-1.5] -> int4[] 22P02 error stored -2 (a floor, not a truncation)
|
|
16
|
+
* ['0x10'] -> int4[] 22P02 error stored 0
|
|
17
|
+
* [''] -> int8[] 22P02 error stored 0
|
|
18
|
+
* [40000] -> int2[] 22003 error RangeError, from Buffer
|
|
19
|
+
* ```
|
|
14
20
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
21
|
+
* Only the last of those was safe. The rest put a number in the table
|
|
22
|
+
* that the caller never wrote and the server would have refused, and
|
|
23
|
+
* silently - the row inserted, the copy succeeded.
|
|
24
|
+
*
|
|
25
|
+
* So a fractional value is refused rather than floored. Truncating 3.7
|
|
26
|
+
* to 3 was the long-standing behaviour here and the argument for it was
|
|
27
|
+
* that it is usually what a caller writing into an integer column means;
|
|
28
|
+
* what that argument misses is that the same caller writing the same
|
|
29
|
+
* value through the same client gets an error whenever the encoding
|
|
30
|
+
* happens to be text, so it cannot be what they meant. Round or truncate
|
|
31
|
+
* at the call site, or let the server do it with a cast.
|
|
32
|
+
*
|
|
33
|
+
* `min`/`max` are checked here as well, rather than left to Buffer's own
|
|
34
|
+
* bounds check, so that the message names the type and the value.
|
|
35
|
+
*/
|
|
36
|
+
export declare function assertInteger(v: any, typeName: string, min: number, max: number): number;
|
|
37
|
+
/**
|
|
38
|
+
* The same rule for int8, which counts past what a `number` holds.
|
|
39
|
+
*
|
|
40
|
+
* BigInt is happy to convert things that are not numbers at all -
|
|
41
|
+
* `BigInt('')` is 0n because an empty string reads as zero, `BigInt(true)`
|
|
42
|
+
* is 1n - so a bad value in a bigint column used to land as a plausible
|
|
43
|
+
* 0 or 1. Only what genuinely carries an exact integer is let through.
|
|
18
44
|
*/
|
|
19
|
-
export declare function
|
|
45
|
+
export declare function assertBigInt(v: any, typeName: string): bigint;
|
|
20
46
|
/**
|
|
21
47
|
* Returns `parsed` when a value that was NOT already a number coerced to
|
|
22
48
|
* one, and throws when it did not.
|
|
@@ -30,14 +56,3 @@ export declare function assertInteger(v: number, typeName: string): number;
|
|
|
30
56
|
* as NaN would be indistinguishable afterwards from a NaN that was meant.
|
|
31
57
|
*/
|
|
32
58
|
export declare function assertCoercedNumber(parsed: number, typeName: string): number;
|
|
33
|
-
/**
|
|
34
|
-
* Refuses a value that only has an integer reading through a coercion
|
|
35
|
-
* nobody asked for.
|
|
36
|
-
*
|
|
37
|
-
* int8 goes to `BigInt` rather than through `fastParseInt()`, and BigInt is
|
|
38
|
-
* happy to convert things that are not numbers at all: `BigInt([])` is 0n
|
|
39
|
-
* because an empty array stringifies to "", and `BigInt(true)` is 1n. A
|
|
40
|
-
* bad value in a bigint column would land as a plausible-looking 0 or 1,
|
|
41
|
-
* so only the types that genuinely carry an integer are let through.
|
|
42
|
-
*/
|
|
43
|
-
export declare function assertBigIntSource(v: any, typeName: string): bigint | number;
|
package/util/assert-integer.js
CHANGED
|
@@ -1,26 +1,107 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* What the server reads as an integer from text: optional sign, digits,
|
|
3
|
+
* nothing else. `'1.5'`, `'1e3'` and `'0x10'` are all refused by
|
|
4
|
+
* `int4in()`, and surrounding whitespace is trimmed before it looks.
|
|
5
|
+
*/
|
|
6
|
+
const DECIMAL_INTEGER = /^[+-]?\d+$/;
|
|
7
|
+
/**
|
|
8
|
+
* Returns `v` as the integer an integer column can store, or throws.
|
|
9
|
+
*
|
|
10
|
+
* The rule is that the binary encoding must accept exactly what the text
|
|
11
|
+
* one does and refuse exactly what the server refuses, because which of
|
|
12
|
+
* the two a value goes out as is not the caller's choice: it depends on
|
|
13
|
+
* whether the parameter's type was declared, which statement cache the
|
|
14
|
+
* query hit, and what the server resolved. A value that means one thing
|
|
15
|
+
* through one path and another through the other is the worst outcome
|
|
16
|
+
* there is, and that is what this used to be:
|
|
3
17
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
18
|
+
* ```
|
|
19
|
+
* text binary (before)
|
|
20
|
+
* [1.5] -> int4[] 22P02 error stored 1
|
|
21
|
+
* [-1.5] -> int4[] 22P02 error stored -2 (a floor, not a truncation)
|
|
22
|
+
* ['0x10'] -> int4[] 22P02 error stored 0
|
|
23
|
+
* [''] -> int8[] 22P02 error stored 0
|
|
24
|
+
* [40000] -> int2[] 22003 error RangeError, from Buffer
|
|
25
|
+
* ```
|
|
9
26
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* is refused.
|
|
27
|
+
* Only the last of those was safe. The rest put a number in the table
|
|
28
|
+
* that the caller never wrote and the server would have refused, and
|
|
29
|
+
* silently - the row inserted, the copy succeeded.
|
|
14
30
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
31
|
+
* So a fractional value is refused rather than floored. Truncating 3.7
|
|
32
|
+
* to 3 was the long-standing behaviour here and the argument for it was
|
|
33
|
+
* that it is usually what a caller writing into an integer column means;
|
|
34
|
+
* what that argument misses is that the same caller writing the same
|
|
35
|
+
* value through the same client gets an error whenever the encoding
|
|
36
|
+
* happens to be text, so it cannot be what they meant. Round or truncate
|
|
37
|
+
* at the call site, or let the server do it with a cast.
|
|
38
|
+
*
|
|
39
|
+
* `min`/`max` are checked here as well, rather than left to Buffer's own
|
|
40
|
+
* bounds check, so that the message names the type and the value.
|
|
18
41
|
*/
|
|
19
|
-
export function assertInteger(v, typeName) {
|
|
20
|
-
|
|
42
|
+
export function assertInteger(v, typeName, min, max) {
|
|
43
|
+
let n;
|
|
44
|
+
const t = typeof v;
|
|
45
|
+
if (t === 'number') {
|
|
46
|
+
n = v;
|
|
47
|
+
if (Number.isNaN(n))
|
|
48
|
+
throw new TypeError(`Cannot encode value as ${typeName}: it has no integer value`);
|
|
49
|
+
}
|
|
50
|
+
else if (t === 'bigint') {
|
|
51
|
+
// Exact up to 2^53; beyond that the range check below refuses it
|
|
52
|
+
// anyway, since nothing this function guards is that wide.
|
|
53
|
+
n = Number(v);
|
|
54
|
+
}
|
|
55
|
+
else if (t === 'string') {
|
|
56
|
+
const s = v.trim();
|
|
57
|
+
if (!DECIMAL_INTEGER.test(s))
|
|
58
|
+
throw new TypeError(`Cannot encode "${v}" as ${typeName}: it is not an integer`);
|
|
59
|
+
n = +s;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
21
62
|
throw new TypeError(`Cannot encode value as ${typeName}: it has no integer value`);
|
|
22
63
|
}
|
|
23
|
-
|
|
64
|
+
if (!Number.isInteger(n))
|
|
65
|
+
throw new TypeError(`Cannot encode ${n} as ${typeName}: it is not an integer`);
|
|
66
|
+
if (n < min || n > max)
|
|
67
|
+
throw new RangeError(`Cannot encode ${n} as ${typeName}: it is out of range (${min} to ${max})`);
|
|
68
|
+
return n;
|
|
69
|
+
}
|
|
70
|
+
const INT8_MIN = BigInt('-9223372036854775808');
|
|
71
|
+
const INT8_MAX = BigInt('9223372036854775807');
|
|
72
|
+
/**
|
|
73
|
+
* The same rule for int8, which counts past what a `number` holds.
|
|
74
|
+
*
|
|
75
|
+
* BigInt is happy to convert things that are not numbers at all -
|
|
76
|
+
* `BigInt('')` is 0n because an empty string reads as zero, `BigInt(true)`
|
|
77
|
+
* is 1n - so a bad value in a bigint column used to land as a plausible
|
|
78
|
+
* 0 or 1. Only what genuinely carries an exact integer is let through.
|
|
79
|
+
*/
|
|
80
|
+
export function assertBigInt(v, typeName) {
|
|
81
|
+
let n;
|
|
82
|
+
const t = typeof v;
|
|
83
|
+
if (t === 'bigint') {
|
|
84
|
+
n = v;
|
|
85
|
+
}
|
|
86
|
+
else if (t === 'number') {
|
|
87
|
+
if (!Number.isInteger(v))
|
|
88
|
+
throw new TypeError(Number.isNaN(v)
|
|
89
|
+
? `Cannot encode value as ${typeName}: it has no integer value`
|
|
90
|
+
: `Cannot encode ${v} as ${typeName}: it is not an integer`);
|
|
91
|
+
n = BigInt(v);
|
|
92
|
+
}
|
|
93
|
+
else if (t === 'string') {
|
|
94
|
+
const s = v.trim();
|
|
95
|
+
if (!DECIMAL_INTEGER.test(s))
|
|
96
|
+
throw new TypeError(`Cannot encode "${v}" as ${typeName}: it is not an integer`);
|
|
97
|
+
n = BigInt(s);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
throw new TypeError(`Cannot encode value as ${typeName}: it has no integer value`);
|
|
101
|
+
}
|
|
102
|
+
if (n < INT8_MIN || n > INT8_MAX)
|
|
103
|
+
throw new RangeError(`Cannot encode ${n} as ${typeName}: it is out of range (${INT8_MIN} to ${INT8_MAX})`);
|
|
104
|
+
return n;
|
|
24
105
|
}
|
|
25
106
|
/**
|
|
26
107
|
* Returns `parsed` when a value that was NOT already a number coerced to
|
|
@@ -40,19 +121,3 @@ export function assertCoercedNumber(parsed, typeName) {
|
|
|
40
121
|
}
|
|
41
122
|
return parsed;
|
|
42
123
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Refuses a value that only has an integer reading through a coercion
|
|
45
|
-
* nobody asked for.
|
|
46
|
-
*
|
|
47
|
-
* int8 goes to `BigInt` rather than through `fastParseInt()`, and BigInt is
|
|
48
|
-
* happy to convert things that are not numbers at all: `BigInt([])` is 0n
|
|
49
|
-
* because an empty array stringifies to "", and `BigInt(true)` is 1n. A
|
|
50
|
-
* bad value in a bigint column would land as a plausible-looking 0 or 1,
|
|
51
|
-
* so only the types that genuinely carry an integer are let through.
|
|
52
|
-
*/
|
|
53
|
-
export function assertBigIntSource(v, typeName) {
|
|
54
|
-
const t = typeof v;
|
|
55
|
-
if (t === 'bigint' || t === 'number' || t === 'string')
|
|
56
|
-
return v;
|
|
57
|
-
throw new TypeError(`Cannot encode value as ${typeName}: it has no integer value`);
|
|
58
|
-
}
|
package/util/bigint-methods.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Buffer.readBigInt64BE` for a runtime that does not have it.
|
|
3
|
+
*
|
|
4
|
+
* @param buf The buffer to read from.
|
|
5
|
+
* @param offset Where the eight bytes start.
|
|
6
|
+
* @returns The signed 64-bit value.
|
|
7
|
+
*/
|
|
1
8
|
export declare function readBigInt64BE(buf: Buffer, offset?: number): bigint;
|
package/util/bigint-methods.js
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
const big0 = BigInt(0);
|
|
3
3
|
const big32 = BigInt(32);
|
|
4
4
|
// https://github.com/nodejs/node/blob/v13.9.0/lib/internal/buffer.js
|
|
5
|
+
/**
|
|
6
|
+
* `Buffer.readBigInt64BE` for a runtime that does not have it.
|
|
7
|
+
*
|
|
8
|
+
* @param buf The buffer to read from.
|
|
9
|
+
* @param offset Where the eight bytes start.
|
|
10
|
+
* @returns The signed 64-bit value.
|
|
11
|
+
*/
|
|
12
|
+
/* Ported from Node's own lib/internal/buffer.js (v13.9.0), so the two
|
|
13
|
+
agree bit for bit where both exist. */
|
|
5
14
|
export function readBigInt64BE(buf, offset = 0) {
|
|
6
15
|
const first = buf[offset];
|
|
7
16
|
const last = buf[offset + 7];
|
|
@@ -1,2 +1,12 @@
|
|
|
1
1
|
import type { ConnectionConfiguration } from '../interfaces/database-connection-params.js';
|
|
2
|
+
/**
|
|
3
|
+
* What the environment says about where to connect.
|
|
4
|
+
*
|
|
5
|
+
* The variables `psql` itself reads - `PGHOST`, `PGPORT`, `PGUSER`,
|
|
6
|
+
* `PGPASSWORD`, `PGDATABASE` and the rest - so a program configured for
|
|
7
|
+
* one is configured for the other.
|
|
8
|
+
*
|
|
9
|
+
* @returns Whatever the environment named; anything it did not is left
|
|
10
|
+
* out rather than defaulted here.
|
|
11
|
+
*/
|
|
2
12
|
export declare function configFromEnv(): ConnectionConfiguration;
|
package/util/config-from-env.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { toInt, toIntDef } from 'putil-varhelpers';
|
|
2
|
+
/**
|
|
3
|
+
* What the environment says about where to connect.
|
|
4
|
+
*
|
|
5
|
+
* The variables `psql` itself reads - `PGHOST`, `PGPORT`, `PGUSER`,
|
|
6
|
+
* `PGPASSWORD`, `PGDATABASE` and the rest - so a program configured for
|
|
7
|
+
* one is configured for the other.
|
|
8
|
+
*
|
|
9
|
+
* @returns Whatever the environment named; anything it did not is left
|
|
10
|
+
* out rather than defaulted here.
|
|
11
|
+
*/
|
|
2
12
|
export function configFromEnv() {
|
|
3
13
|
const env = process.env;
|
|
4
14
|
const result = {};
|
|
@@ -1,3 +1,20 @@
|
|
|
1
1
|
import type { ConnectionConfiguration } from '../interfaces/database-connection-params.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves what a caller passed into the configuration a connection is
|
|
4
|
+
* opened with.
|
|
5
|
+
*
|
|
6
|
+
* @param config An object, a connection string, or nothing at all.
|
|
7
|
+
* @returns The configuration, with the environment and PostgreSQL's own
|
|
8
|
+
* defaults filling what was left out.
|
|
9
|
+
* @throws Error When a field is named that no configuration has, which
|
|
10
|
+
* is usually a typo and is reported with what was probably meant.
|
|
11
|
+
*/
|
|
2
12
|
export declare function getConnectionConfig(config?: ConnectionConfiguration | string): ConnectionConfiguration;
|
|
13
|
+
/**
|
|
14
|
+
* Reads a `postgres://` URL, or the `host=... port=...` keyword form,
|
|
15
|
+
* into a configuration.
|
|
16
|
+
*
|
|
17
|
+
* @param str The connection string.
|
|
18
|
+
* @returns What it named.
|
|
19
|
+
*/
|
|
3
20
|
export declare function parseConnectionString(str: string): ConnectionConfiguration;
|
|
@@ -39,6 +39,16 @@ function checkForeignKeys(config) {
|
|
|
39
39
|
'name that is nearly right does nothing and says nothing.');
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolves what a caller passed into the configuration a connection is
|
|
44
|
+
* opened with.
|
|
45
|
+
*
|
|
46
|
+
* @param config An object, a connection string, or nothing at all.
|
|
47
|
+
* @returns The configuration, with the environment and PostgreSQL's own
|
|
48
|
+
* defaults filling what was left out.
|
|
49
|
+
* @throws Error When a field is named that no configuration has, which
|
|
50
|
+
* is usually a typo and is reported with what was probably meant.
|
|
51
|
+
*/
|
|
42
52
|
export function getConnectionConfig(config) {
|
|
43
53
|
const cfg = configFromEnv();
|
|
44
54
|
if (typeof config === 'string') {
|
|
@@ -125,6 +135,13 @@ function splitHosts(value, fallbackPort) {
|
|
|
125
135
|
* has been accepted for longer than either and stays.
|
|
126
136
|
*/
|
|
127
137
|
const DATABASE_PATH_SCHEMES = ['pg:', 'postgres:', 'postgresql:'];
|
|
138
|
+
/**
|
|
139
|
+
* Reads a `postgres://` URL, or the `host=... port=...` keyword form,
|
|
140
|
+
* into a configuration.
|
|
141
|
+
*
|
|
142
|
+
* @param str The connection string.
|
|
143
|
+
* @returns What it named.
|
|
144
|
+
*/
|
|
128
145
|
export function parseConnectionString(str) {
|
|
129
146
|
if (str.startsWith('/'))
|
|
130
147
|
str = 'socket:/' + str;
|
package/util/copy-from-rows.d.ts
CHANGED
|
@@ -3,7 +3,9 @@ import type { CopyFromRowsOptions } from '../interfaces/copy-from-rows-options.j
|
|
|
3
3
|
import type { OID } from '../types.js';
|
|
4
4
|
/** Default bytes accumulated before a CopyData message goes out. */
|
|
5
5
|
export declare const DEFAULT_COPY_CHUNK_SIZE: number;
|
|
6
|
+
/** One row to copy: values in column order, or keyed by column name. */
|
|
6
7
|
export type CopyRow = any[] | Record<string, any>;
|
|
8
|
+
/** Where `copyFromRows()` takes its rows from - an array, an iterable, an async one, or a stream. */
|
|
7
9
|
export type CopyRowSource = Iterable<CopyRow> | AsyncIterable<CopyRow> | Readable;
|
|
8
10
|
/**
|
|
9
11
|
* Quotes a possibly schema-qualified name. A single dot separates schema
|
|
@@ -21,7 +23,17 @@ export declare function quoteQualifiedName(name: string): string;
|
|
|
21
23
|
* name here could only approximate.
|
|
22
24
|
*/
|
|
23
25
|
export declare function buildProbeSql(table: string, columns?: string[]): string;
|
|
26
|
+
/**
|
|
27
|
+
* Writes the `COPY ... FROM STDIN (FORMAT binary)` the rows will be sent
|
|
28
|
+
* under.
|
|
29
|
+
*
|
|
30
|
+
* @param table The destination, optionally schema-qualified.
|
|
31
|
+
* @param columns Which columns the rows line up with; all of them when
|
|
32
|
+
* absent.
|
|
33
|
+
* @returns The statement.
|
|
34
|
+
*/
|
|
24
35
|
export declare function buildCopySql(table: string, columns?: string[]): string;
|
|
36
|
+
/** What a copy in flight needs to keep between its chunks. */
|
|
25
37
|
export interface CopyFromRowsContext {
|
|
26
38
|
columns: string[];
|
|
27
39
|
dataTypeIds: OID[];
|
package/util/copy-from-rows.js
CHANGED
|
@@ -30,6 +30,15 @@ export function buildProbeSql(table, columns) {
|
|
|
30
30
|
const cols = columns?.length ? columns.map(escapeIdentifier).join(', ') : '*';
|
|
31
31
|
return `select ${cols} from ${quoteQualifiedName(table)} where false`;
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Writes the `COPY ... FROM STDIN (FORMAT binary)` the rows will be sent
|
|
35
|
+
* under.
|
|
36
|
+
*
|
|
37
|
+
* @param table The destination, optionally schema-qualified.
|
|
38
|
+
* @param columns Which columns the rows line up with; all of them when
|
|
39
|
+
* absent.
|
|
40
|
+
* @returns The statement.
|
|
41
|
+
*/
|
|
33
42
|
export function buildCopySql(table, columns) {
|
|
34
43
|
const cols = columns?.length
|
|
35
44
|
? ' (' + columns.map(escapeIdentifier).join(', ') + ')'
|
|
@@ -8,4 +8,10 @@ import type { Maybe, OID } from '../types.js';
|
|
|
8
8
|
export declare const DecimalAsStringOIDs: readonly OID[];
|
|
9
9
|
/** Whether this type's values are wanted as their exact decimal string. */
|
|
10
10
|
export declare function wantsDecimalString(options: Maybe<DataMappingOptions>, oid: OID): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Refuses a `decimalAsString` that names a type it cannot apply to.
|
|
13
|
+
*
|
|
14
|
+
* @param v The option as the caller wrote it.
|
|
15
|
+
* @throws TypeError When it names an OID outside the decimal types.
|
|
16
|
+
*/
|
|
11
17
|
export declare function validateDecimalAsString(v: Maybe<boolean | OID[]>): void;
|
|
@@ -15,6 +15,12 @@ export function wantsDecimalString(options, oid) {
|
|
|
15
15
|
return false;
|
|
16
16
|
return v === true || v.includes(oid);
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Refuses a `decimalAsString` that names a type it cannot apply to.
|
|
20
|
+
*
|
|
21
|
+
* @param v The option as the caller wrote it.
|
|
22
|
+
* @throws TypeError When it names an OID outside the decimal types.
|
|
23
|
+
*/
|
|
18
24
|
export function validateDecimalAsString(v) {
|
|
19
25
|
if (v === undefined || typeof v === 'boolean')
|
|
20
26
|
return;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
import type { DataMappingOptions } from '../interfaces/data-mapping-options.js';
|
|
2
2
|
import type { DecodeBinaryFunction, Nullable } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Reads an array out of the binary wire format, nested to whatever depth
|
|
5
|
+
* it declares.
|
|
6
|
+
*
|
|
7
|
+
* @param buf The value's own bytes.
|
|
8
|
+
* @param decode The element type's binary decoder.
|
|
9
|
+
* @param options Passed to it.
|
|
10
|
+
* @returns The array, with SQL NULLs as `null`.
|
|
11
|
+
*/
|
|
3
12
|
export declare function decodeBinaryArray<T = any>(buf: Buffer, offset: number, decoder: DecodeBinaryFunction, options?: DataMappingOptions): Nullable<T[]>;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { BufferReader } from '../protocol/buffer-reader.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reads an array out of the binary wire format, nested to whatever depth
|
|
4
|
+
* it declares.
|
|
5
|
+
*
|
|
6
|
+
* @param buf The value's own bytes.
|
|
7
|
+
* @param decode The element type's binary decoder.
|
|
8
|
+
* @param options Passed to it.
|
|
9
|
+
* @returns The array, with SQL NULLs as `null`.
|
|
10
|
+
*/
|
|
2
11
|
export function decodeBinaryArray(buf, offset, decoder, options = {}) {
|
|
3
12
|
if (!buf.length)
|
|
4
13
|
return null;
|
package/util/escape-literal.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quotes a string as an SQL literal, escaping what has to be escaped.
|
|
3
|
+
*
|
|
4
|
+
* For the `sql` tag and anything else that has to put a value into the
|
|
5
|
+
* statement text; a parameter never needs this.
|
|
6
|
+
*
|
|
7
|
+
* @param str The value to quote.
|
|
8
|
+
* @returns The quoted literal, `E`-prefixed when it carries a backslash.
|
|
9
|
+
* @throws Error When the string holds a NUL byte, which PostgreSQL text
|
|
10
|
+
* cannot.
|
|
11
|
+
*/
|
|
1
12
|
export declare function escapeLiteral(str: string): string;
|
package/util/escape-literal.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
|
|
2
|
+
/**
|
|
3
|
+
* Quotes a string as an SQL literal, escaping what has to be escaped.
|
|
4
|
+
*
|
|
5
|
+
* For the `sql` tag and anything else that has to put a value into the
|
|
6
|
+
* statement text; a parameter never needs this.
|
|
7
|
+
*
|
|
8
|
+
* @param str The value to quote.
|
|
9
|
+
* @returns The quoted literal, `E`-prefixed when it carries a backslash.
|
|
10
|
+
* @throws Error When the string holds a NUL byte, which PostgreSQL text
|
|
11
|
+
* cannot.
|
|
12
|
+
*/
|
|
13
|
+
/* Ported from PostgreSQL's own src/interfaces/libpq/fe-exec.c (9.2.4),
|
|
14
|
+
so it quotes what libpq quotes, the way libpq quotes it. */
|
|
2
15
|
export function escapeLiteral(str) {
|
|
3
16
|
if (str.indexOf('\0') >= 0) {
|
|
4
17
|
throw new Error('PostgreSQL text values cannot contain NUL (\\0) bytes');
|
package/util/fast-parseint.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads an integer out of what a decoder was handed.
|
|
3
|
+
*
|
|
4
|
+
* @param str The text, or a number already.
|
|
5
|
+
* @returns The integer, or NaN when there is none to read.
|
|
6
|
+
*/
|
|
1
7
|
export declare function fastParseInt(str: string | number): number;
|
|
8
|
+
/**
|
|
9
|
+
* The same, straight from the wire bytes - no string is made.
|
|
10
|
+
*
|
|
11
|
+
* @param buf The buffer holding the digits.
|
|
12
|
+
* @param offset Where they start.
|
|
13
|
+
* @param len How many bytes they run for.
|
|
14
|
+
* @returns The integer the digits spell.
|
|
15
|
+
*/
|
|
2
16
|
export declare function fastParseIntBuffer(buf: Buffer, offset?: number, len?: number): number;
|
package/util/fast-parseint.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads an integer out of what a decoder was handed.
|
|
3
|
+
*
|
|
4
|
+
* @param str The text, or a number already.
|
|
5
|
+
* @returns The integer, or NaN when there is none to read.
|
|
6
|
+
*/
|
|
1
7
|
export function fastParseInt(str) {
|
|
2
8
|
/* c8 ignore next */
|
|
3
9
|
if (typeof str === 'number')
|
|
@@ -7,6 +13,14 @@ export function fastParseInt(str) {
|
|
|
7
13
|
return NaN;
|
|
8
14
|
return parseInt(str, 10);
|
|
9
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* The same, straight from the wire bytes - no string is made.
|
|
18
|
+
*
|
|
19
|
+
* @param buf The buffer holding the digits.
|
|
20
|
+
* @param offset Where they start.
|
|
21
|
+
* @param len How many bytes they run for.
|
|
22
|
+
* @returns The integer the digits spell.
|
|
23
|
+
*/
|
|
10
24
|
export function fastParseIntBuffer(buf, offset = 0, len = buf.length - offset) {
|
|
11
25
|
const end = offset + len;
|
|
12
26
|
let i = offset;
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
import type { DataMappingOptions } from '../interfaces/data-mapping-options.js';
|
|
2
|
+
/**
|
|
3
|
+
* Renders a value as the `YYYY-MM-DD` a `date` parameter goes out as.
|
|
4
|
+
*
|
|
5
|
+
* @param v The date, an epoch milliseconds, or text to pass through.
|
|
6
|
+
* @param options `utcDates` decides which clock its components are read
|
|
7
|
+
* from.
|
|
8
|
+
* @returns The date's text.
|
|
9
|
+
*/
|
|
2
10
|
export declare function formatDate(v: Date | number | string, options?: DataMappingOptions): string;
|
|
11
|
+
/**
|
|
12
|
+
* Renders a value as the `HH:MM:SS.mmm` a `time` parameter goes out as.
|
|
13
|
+
*
|
|
14
|
+
* @param v The time, an epoch milliseconds, or text to pass through.
|
|
15
|
+
* @param options `utcDates` decides which clock its components are read
|
|
16
|
+
* from.
|
|
17
|
+
* @returns The time's text.
|
|
18
|
+
*/
|
|
3
19
|
export declare function formatTime(v: Date | number | string, options?: DataMappingOptions): string;
|
|
20
|
+
/**
|
|
21
|
+
* Renders a value as the `YYYY-MM-DD HH:MM:SS.mmm` a `timestamp`
|
|
22
|
+
* parameter goes out as.
|
|
23
|
+
*
|
|
24
|
+
* @param v The timestamp, an epoch milliseconds, or text to pass
|
|
25
|
+
* through.
|
|
26
|
+
* @param options `utcDates` decides which clock its components are read
|
|
27
|
+
* from.
|
|
28
|
+
* @returns The timestamp's text.
|
|
29
|
+
*/
|
|
4
30
|
export declare function formatTimestamp(v: Date | number | string, options?: DataMappingOptions): string;
|
|
5
31
|
/**
|
|
6
32
|
* timestamptz is an absolute instant - its binary encoder uses getTime()
|