@depup/pg-promise 12.6.2-depup.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/lib/assert.js +10 -0
  4. package/lib/connect.js +178 -0
  5. package/lib/context.js +93 -0
  6. package/lib/database-pool.js +115 -0
  7. package/lib/database.js +1643 -0
  8. package/lib/errors/README.md +13 -0
  9. package/lib/errors/index.js +51 -0
  10. package/lib/errors/parameterized-query-error.js +71 -0
  11. package/lib/errors/prepared-statement-error.js +71 -0
  12. package/lib/errors/query-file-error.js +75 -0
  13. package/lib/errors/query-result-error.js +157 -0
  14. package/lib/events.js +543 -0
  15. package/lib/formatting.js +932 -0
  16. package/lib/helpers/README.md +10 -0
  17. package/lib/helpers/column-set.js +614 -0
  18. package/lib/helpers/column.js +406 -0
  19. package/lib/helpers/index.js +75 -0
  20. package/lib/helpers/methods/concat.js +103 -0
  21. package/lib/helpers/methods/index.js +13 -0
  22. package/lib/helpers/methods/insert.js +151 -0
  23. package/lib/helpers/methods/sets.js +81 -0
  24. package/lib/helpers/methods/update.js +248 -0
  25. package/lib/helpers/methods/values.js +116 -0
  26. package/lib/helpers/table-name.js +175 -0
  27. package/lib/index.js +29 -0
  28. package/lib/inner-state.js +39 -0
  29. package/lib/main.js +394 -0
  30. package/lib/patterns.js +43 -0
  31. package/lib/query-file.js +379 -0
  32. package/lib/query-result.js +39 -0
  33. package/lib/query.js +273 -0
  34. package/lib/special-query.js +30 -0
  35. package/lib/stream.js +125 -0
  36. package/lib/task.js +404 -0
  37. package/lib/text.js +40 -0
  38. package/lib/tx-mode.js +194 -0
  39. package/lib/types/index.js +18 -0
  40. package/lib/types/parameterized-query.js +247 -0
  41. package/lib/types/prepared-statement.js +298 -0
  42. package/lib/types/server-formatting.js +92 -0
  43. package/lib/utils/README.md +13 -0
  44. package/lib/utils/color.js +68 -0
  45. package/lib/utils/index.js +199 -0
  46. package/lib/utils/public.js +312 -0
  47. package/package.json +77 -0
  48. package/typescript/README.md +63 -0
  49. package/typescript/pg-promise.d.ts +728 -0
  50. package/typescript/pg-subset.d.ts +359 -0
  51. package/typescript/tslint.json +21 -0
package/lib/tx-mode.js ADDED
@@ -0,0 +1,194 @@
1
+ /*
2
+ * Copyright (c) 2015-present, Vitaly Tomilov
3
+ *
4
+ * See the LICENSE file at the top-level directory of this distribution
5
+ * for licensing information.
6
+ *
7
+ * Removal or modification of this copyright notice is prohibited.
8
+ */
9
+
10
+ const {InnerState} = require('./inner-state');
11
+ const {assert} = require('./assert');
12
+
13
+ const npm = {
14
+ addInspection: require('./utils').addInspection,
15
+ inspect: require('util').inspect
16
+ };
17
+
18
+ /**
19
+ * @enum {number}
20
+ * @alias txMode.isolationLevel
21
+ * @readonly
22
+ * @summary Transaction Isolation Level.
23
+ * @description
24
+ * The type is available from the {@link txMode} namespace.
25
+ *
26
+ * @see $[Transaction Isolation]
27
+ */
28
+ const isolationLevel = {
29
+ /** Isolation level isn't specified. */
30
+ none: 0,
31
+
32
+ /** `ISOLATION LEVEL SERIALIZABLE` */
33
+ serializable: 1,
34
+
35
+ /** `ISOLATION LEVEL REPEATABLE READ` */
36
+ repeatableRead: 2,
37
+
38
+ /** `ISOLATION LEVEL READ COMMITTED` */
39
+ readCommitted: 3
40
+
41
+ // From the official documentation: https://www.postgresql.org/docs/17/sql-set-transaction.html
42
+ // The SQL standard defines one additional level, READ UNCOMMITTED. In PostgreSQL READ UNCOMMITTED is treated as READ COMMITTED.
43
+ // => skipping `READ UNCOMMITTED`.
44
+ };
45
+
46
+ /**
47
+ * @class txMode.TransactionMode
48
+ * @description
49
+ * Constructs a complete transaction-opening `BEGIN` command, from these options:
50
+ * - isolation level
51
+ * - access mode
52
+ * - deferrable mode
53
+ *
54
+ * The type is available from the {@link txMode} namespace.
55
+ *
56
+ * @param {} [options]
57
+ * Transaction Mode options.
58
+ *
59
+ * @param {txMode.isolationLevel} [options.tiLevel]
60
+ * Transaction Isolation Level.
61
+ *
62
+ * @param {boolean} [options.readOnly]
63
+ * Sets transaction access mode based on the read-only flag:
64
+ * - `undefined` - access mode not specified (default)
65
+ * - `true` - access mode is set to `READ ONLY`
66
+ * - `false` - access mode is set to `READ WRITE`
67
+ *
68
+ * @param {boolean} [options.deferrable]
69
+ * Sets transaction deferrable mode based on the boolean value:
70
+ * - `undefined` - deferrable mode not specified (default)
71
+ * - `true` - mode is set to `DEFERRABLE`
72
+ * - `false` - mode is set to `NOT DEFERRABLE`
73
+ *
74
+ * It is used only when `tiLevel`=`isolationLevel.serializable`
75
+ * and `readOnly`=`true`, or else it is ignored.
76
+ *
77
+ * @returns {txMode.TransactionMode}
78
+ *
79
+ * @see $[BEGIN], {@link txMode.isolationLevel}
80
+ *
81
+ * @example
82
+ *
83
+ * const {TransactionMode, isolationLevel} = pgp.txMode;
84
+ *
85
+ * // Create a reusable transaction mode (serializable + read-only + deferrable):
86
+ * const mode = new TransactionMode({
87
+ * tiLevel: isolationLevel.serializable,
88
+ * readOnly: true,
89
+ * deferrable: true
90
+ * });
91
+ *
92
+ * db.tx({mode}, t => {
93
+ * return t.any('SELECT * FROM table');
94
+ * })
95
+ * .then(data => {
96
+ * // success;
97
+ * })
98
+ * .catch(error => {
99
+ * // error
100
+ * });
101
+ *
102
+ * // Instead of the default BEGIN, such transaction will start with:
103
+ *
104
+ * // BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE
105
+ *
106
+ */
107
+ class TransactionMode extends InnerState {
108
+
109
+ constructor(options) {
110
+ options = assert(options, ['tiLevel', 'deferrable', 'readOnly']);
111
+ const {readOnly, deferrable} = options;
112
+ let {tiLevel} = options;
113
+ let level, accessMode, deferrableMode, begin = 'begin';
114
+ tiLevel = (tiLevel > 0) ? parseInt(tiLevel) : 0;
115
+
116
+ if (tiLevel > 0 && tiLevel < 4) {
117
+ const values = ['serializable', 'repeatable read', 'read committed'];
118
+ level = 'isolation level ' + values[tiLevel - 1];
119
+ }
120
+ if (readOnly) {
121
+ accessMode = 'read only';
122
+ } else {
123
+ if (readOnly !== undefined) {
124
+ accessMode = 'read write';
125
+ }
126
+ }
127
+ // From the official documentation: https://www.postgresql.org/docs/17/sql-set-transaction.html
128
+ // The DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY
129
+ if (tiLevel === isolationLevel.serializable && readOnly) {
130
+ if (deferrable) {
131
+ deferrableMode = 'deferrable';
132
+ } else {
133
+ if (deferrable !== undefined) {
134
+ deferrableMode = 'not deferrable';
135
+ }
136
+ }
137
+ }
138
+ if (level) {
139
+ begin += ' ' + level;
140
+ }
141
+ if (accessMode) {
142
+ begin += ' ' + accessMode;
143
+ }
144
+ if (deferrableMode) {
145
+ begin += ' ' + deferrableMode;
146
+ }
147
+
148
+ super({begin, capBegin: begin.toUpperCase()});
149
+ }
150
+
151
+ /**
152
+ * @method txMode.TransactionMode#begin
153
+ * @description
154
+ * Returns a complete BEGIN statement, according to all the parameters passed into the class.
155
+ *
156
+ * This method is primarily for internal use by the library.
157
+ *
158
+ * @param {boolean} [cap=false]
159
+ * Indicates whether the returned SQL must be capitalized.
160
+ *
161
+ * @returns {string}
162
+ */
163
+ begin(cap) {
164
+ return cap ? this._inner.capBegin : this._inner.begin;
165
+ }
166
+ }
167
+
168
+ npm.addInspection(TransactionMode, function () {
169
+ const obj = {SQL: this.begin(true)};
170
+ return 'TransactionMode ' + npm.inspect(obj, {breakLength: 100, depth: 5, colors: true});
171
+ });
172
+
173
+ /**
174
+ * @namespace txMode
175
+ * @description
176
+ * Transaction Mode namespace, available as `pgp.txMode`, before and after initializing the library.
177
+ *
178
+ * Extends the default `BEGIN` with Transaction Mode parameters:
179
+ * - isolation level
180
+ * - access mode
181
+ * - deferrable mode
182
+ *
183
+ * @property {function} TransactionMode
184
+ * {@link txMode.TransactionMode TransactionMode} class constructor.
185
+ *
186
+ * @property {txMode.isolationLevel} isolationLevel
187
+ * Transaction Isolation Level enumerator
188
+ *
189
+ * @see $[BEGIN]
190
+ */
191
+ module.exports = {
192
+ isolationLevel,
193
+ TransactionMode
194
+ };
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) 2015-present, Vitaly Tomilov
3
+ *
4
+ * See the LICENSE file at the top-level directory of this distribution
5
+ * for licensing information.
6
+ *
7
+ * Removal or modification of this copyright notice is prohibited.
8
+ */
9
+
10
+ const {ServerFormatting} = require('./server-formatting');
11
+ const {PreparedStatement} = require('./prepared-statement');
12
+ const {ParameterizedQuery} = require('./parameterized-query');
13
+
14
+ module.exports = {
15
+ ServerFormatting,
16
+ PreparedStatement,
17
+ ParameterizedQuery
18
+ };
@@ -0,0 +1,247 @@
1
+ /*
2
+ * Copyright (c) 2015-present, Vitaly Tomilov
3
+ *
4
+ * See the LICENSE file at the top-level directory of this distribution
5
+ * for licensing information.
6
+ *
7
+ * Removal or modification of this copyright notice is prohibited.
8
+ */
9
+
10
+ const {ServerFormatting} = require('./server-formatting');
11
+ const {ParameterizedQueryError} = require('../errors');
12
+ const {QueryFile} = require('../query-file');
13
+ const {assert} = require('../assert');
14
+
15
+ const npm = {
16
+ EOL: require('os').EOL,
17
+ utils: require('../utils'),
18
+ inspect: require('util').inspect
19
+ };
20
+
21
+ /**
22
+ * @class ParameterizedQuery
23
+ * @description
24
+ * Constructs a new {@link ParameterizedQuery} object. All properties can also be set after the object's construction.
25
+ *
26
+ * This type extends the basic `{text, values}` object, i.e. when the basic object is used with a query method,
27
+ * a new {@link ParameterizedQuery} object is created in its place.
28
+ *
29
+ * The type can be used in place of the `query` parameter, with any query method directly.
30
+ *
31
+ * The type is available from the library's root: `pgp.ParameterizedQuery`.
32
+ *
33
+ * @param {string|QueryFile|Object} [options]
34
+ * Object configuration options / properties.
35
+ *
36
+ * @param {string|QueryFile} [options.text] - See property {@link ParameterizedQuery#text text}.
37
+ * @param {array} [options.values] - See property {@link ParameterizedQuery#values values}.
38
+ * @param {boolean} [options.binary] - See property {@link ParameterizedQuery#binary binary}.
39
+ * @param {string} [options.rowMode] - See property {@link ParameterizedQuery#rowMode rowMode}.
40
+ * @param {ITypes} [options.types] - See property {@link ParameterizedQuery#types types}.
41
+ *
42
+ * @returns {ParameterizedQuery}
43
+ *
44
+ * @see
45
+ * {@link errors.ParameterizedQueryError ParameterizedQueryError}
46
+ *
47
+ * @example
48
+ *
49
+ * const {ParameterizedQuery: PQ} = require('pg-promise');
50
+ *
51
+ * // Creating a complete Parameterized Query with parameters:
52
+ * const findUser = new PQ({text: 'SELECT * FROM Users WHERE id = $1', values: [123]});
53
+ *
54
+ * db.one(findUser)
55
+ * .then(user => {
56
+ * // user found;
57
+ * })
58
+ * .catch(error => {
59
+ * // error;
60
+ * });
61
+ *
62
+ * @example
63
+ *
64
+ * const {ParameterizedQuery: PQ} = require('pg-promise');
65
+ *
66
+ * // Creating a reusable Parameterized Query without values:
67
+ * const addUser = new PQ('INSERT INTO Users(name, age) VALUES($1, $2)');
68
+ *
69
+ * // setting values explicitly:
70
+ * addUser.values = ['John', 30];
71
+ *
72
+ * db.none(addUser)
73
+ * .then(() => {
74
+ * // user added;
75
+ * })
76
+ * .catch(error=> {
77
+ * // error;
78
+ * });
79
+ *
80
+ * // setting values implicitly, by passing them into the query method:
81
+ * db.none(addUser, ['Mike', 25])
82
+ * .then(() => {
83
+ * // user added;
84
+ * })
85
+ * .catch(error=> {
86
+ * // error;
87
+ * });
88
+ */
89
+ class ParameterizedQuery extends ServerFormatting {
90
+ constructor(options) {
91
+ if (typeof options === 'string' || options instanceof QueryFile) {
92
+ options = {
93
+ text: options
94
+ };
95
+ } else {
96
+ options = assert(options, ['text', 'values', 'binary', 'rowMode', 'types']);
97
+ }
98
+ super(options);
99
+ }
100
+ }
101
+
102
+ /**
103
+ * @method ParameterizedQuery#parse
104
+ * @description
105
+ * Parses the current object and returns a simple `{text, values}`, if successful,
106
+ * or else it returns a {@link errors.ParameterizedQueryError ParameterizedQueryError} object.
107
+ *
108
+ * This method is primarily for internal use by the library.
109
+ *
110
+ * @returns {{text, values}|errors.ParameterizedQueryError}
111
+ */
112
+ ParameterizedQuery.prototype.parse = function () {
113
+
114
+ const _i = this._inner, options = _i.options;
115
+ const qf = options.text instanceof QueryFile ? options.text : null;
116
+
117
+ if (!_i.changed && !qf) {
118
+ return _i.target;
119
+ }
120
+
121
+ const errors = [], values = _i.target.values;
122
+ _i.target = {
123
+ text: options.text
124
+ };
125
+ _i.changed = true;
126
+ _i.currentError = undefined;
127
+
128
+ if (qf) {
129
+ qf.prepare();
130
+ if (qf.error) {
131
+ errors.push(qf.error);
132
+ } else {
133
+ _i.target.text = qf[QueryFile.$query];
134
+ }
135
+ }
136
+
137
+ if (!npm.utils.isText(_i.target.text)) {
138
+ errors.push('Property \'text\' must be a non-empty text string.');
139
+ }
140
+
141
+ if (!npm.utils.isNull(values)) {
142
+ _i.target.values = values;
143
+ }
144
+
145
+ if (options.binary !== undefined) {
146
+ _i.target.binary = !!options.binary;
147
+ }
148
+
149
+ if (options.rowMode !== undefined) {
150
+ _i.target.rowMode = options.rowMode;
151
+ }
152
+
153
+ if (options.types !== undefined) {
154
+ _i.target.types = options.types;
155
+ }
156
+
157
+ if (errors.length) {
158
+ return _i.currentError = new ParameterizedQueryError(errors[0], _i.target);
159
+ }
160
+
161
+ _i.changed = false;
162
+
163
+ return _i.target;
164
+ };
165
+
166
+ npm.utils.addInspection(ParameterizedQuery, function () {
167
+ const pq = this.parse();
168
+ const obj = {};
169
+ if (npm.utils.isText(pq.text)) {
170
+ obj.text = pq.text;
171
+ }
172
+ if (this.values !== undefined) {
173
+ obj.values = this.values;
174
+ }
175
+ if (this.binary !== undefined) {
176
+ obj.binary = this.binary;
177
+ }
178
+ if (this.rowMode !== undefined) {
179
+ obj.rowMode = this.rowMode;
180
+ }
181
+ if (this.error !== undefined) {
182
+ obj.error = this.error.message;
183
+ }
184
+ return 'ParameterizedQuery ' + npm.inspect(obj, {breakLength: 100, depth: 5, colors: true});
185
+ });
186
+
187
+ module.exports = {ParameterizedQuery};
188
+
189
+ /**
190
+ * @name ParameterizedQuery#text
191
+ * @type {string|QueryFile}
192
+ * @description
193
+ * A non-empty query string or a {@link QueryFile} object.
194
+ *
195
+ * Only the basic variables (`$1`, `$2`, etc) can be used in the query, because _Parameterized Queries_
196
+ * are formatted on the server side.
197
+ */
198
+
199
+ /**
200
+ * @name ParameterizedQuery#values
201
+ * @type {array}
202
+ * @description
203
+ * Query formatting parameters, depending on the type:
204
+ *
205
+ * - `null` / `undefined` means the query has no formatting parameters
206
+ * - `Array` - it is an array of formatting parameters
207
+ * - None of the above, means it is a single formatting value, which
208
+ * is then automatically wrapped into an array
209
+ */
210
+
211
+ /**
212
+ * @name ParameterizedQuery#binary
213
+ * @type {boolean}
214
+ * @default undefined
215
+ * @description
216
+ * Activates binary result mode. The default is the text mode.
217
+ *
218
+ * @see {@link http://www.postgresql.org/docs/devel/static/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY Extended Query}
219
+ */
220
+
221
+ /**
222
+ * @name ParameterizedQuery#rowMode
223
+ * @type {string}
224
+ * @default undefined
225
+ * @description
226
+ * Changes the way data arrives to the client, with only one value supported by $[pg]:
227
+ * - `array` will make all data rows arrive as arrays of values. By default, rows arrive as objects.
228
+ */
229
+
230
+ /**
231
+ * @name ParameterizedQuery#types
232
+ * @type {ITypes}
233
+ * @default undefined
234
+ * @description
235
+ * Custom type parsers just for this query result.
236
+ */
237
+
238
+ /**
239
+ * @name ParameterizedQuery#error
240
+ * @type {errors.ParameterizedQueryError}
241
+ * @default undefined
242
+ * @readonly
243
+ * @description
244
+ * When in an error state, it is set to a {@link errors.ParameterizedQueryError ParameterizedQueryError} object. Otherwise, it is `undefined`.
245
+ *
246
+ * This property is primarily for internal use by the library.
247
+ */