postgrejs 3.12.2 → 3.14.0

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.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Follows a value's `toPostgres()` until it reaches one that has none.
3
+ *
4
+ * `toPostgres()` is `pg`'s extension point for a type that knows how to
5
+ * write itself: a library with its own geometry, money or domain class
6
+ * implements it, and the value goes out as that class says rather than
7
+ * as whatever `String()` makes of it. The result is followed rather than
8
+ * returned, because what it hands back may itself be a `Date`, an array,
9
+ * a `Buffer` or another such value.
10
+ *
11
+ * @param v The value a parameter is about to be written from.
12
+ * @returns The value to write, which is `v` itself unless it had one.
13
+ * @throws TypeError if the values form a cycle.
14
+ */
15
+ /* Only for a parameter with no declared type. A declared type owns its
16
+ own encoding and is an answer the caller gave, not a guess.
17
+
18
+ This client's own classes are unaffected: every one of their
19
+ `toPostgres()` is `return this.toString()`, which is what the path
20
+ below used to reach anyway. What changes is a caller's class, which
21
+ used to be written `[object Object]` - silently, with a row written
22
+ and no error. */
23
+ export function resolveToPostgres(v) {
24
+ if (v === null || typeof v !== 'object' || typeof v.toPostgres !== 'function')
25
+ return v;
26
+ const seen = [];
27
+ while (v !== null &&
28
+ typeof v === 'object' &&
29
+ typeof v.toPostgres === 'function') {
30
+ if (seen.includes(v))
31
+ throw new TypeError('Circular reference while preparing a parameter: toPostgres() returned a value that leads back to itself');
32
+ seen.push(v);
33
+ v = v.toPostgres();
34
+ }
35
+ return v;
36
+ }
37
+ /**
38
+ * The text an undeclared parameter - or one element of an undeclared
39
+ * array - goes out as.
40
+ *
41
+ * Follows `toPostgres()` first, then writes a plain object as JSON and
42
+ * anything else as its own text.
43
+ *
44
+ * @param v The value to render.
45
+ * @returns What to put on the wire, before any array quoting.
46
+ */
47
+ /* The two rules `pg`'s `prepareValue` has that this path did not. The
48
+ rest of its rules are either already here in their own branch - a
49
+ Date, an array, a Buffer - or deliberately not taken: it writes a Date
50
+ with a `T` where this client writes a space, and quotes the elements
51
+ of a number array where this client writes them bare, which is 10-26%
52
+ smaller and parses identically. Those are spellings the server reads
53
+ the same way; these two are a value that arrives as something else. */
54
+ export function unspecifiedText(v) {
55
+ v = resolveToPostgres(v);
56
+ if (v !== null &&
57
+ typeof v === 'object' &&
58
+ !(v instanceof Date) &&
59
+ !Buffer.isBuffer(v) &&
60
+ !Array.isArray(v))
61
+ return JSON.stringify(v);
62
+ return '' + v;
63
+ }