@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
@@ -0,0 +1,13 @@
1
+ ### `errors` namespace
2
+
3
+ This folder contains everything that's available via the [errors] namespace, before and after initialization:
4
+
5
+ ```js
6
+ const pgpLib = require('pg-promise');
7
+ const pgp = pgpLib(/*initialization options*/);
8
+
9
+ pgpLib.errors; // `errors` namespace
10
+ pgp.errors; // `errors` namespace
11
+ ```
12
+
13
+ [errors]:http://vitaly-t.github.io/pg-promise/errors.html
@@ -0,0 +1,51 @@
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 {QueryResultError, queryResultErrorCode} = require('./query-result-error');
11
+ const {PreparedStatementError} = require('./prepared-statement-error');
12
+ const {ParameterizedQueryError} = require('./parameterized-query-error');
13
+ const {QueryFileError} = require('./query-file-error');
14
+
15
+ /**
16
+ * @namespace errors
17
+ * @description
18
+ * Error types namespace, available as `pgp.errors`, before and after initializing the library.
19
+ *
20
+ * @property {function} PreparedStatementError
21
+ * {@link errors.PreparedStatementError PreparedStatementError} class constructor.
22
+ *
23
+ * Represents all errors that can be reported by class {@link PreparedStatement}.
24
+ *
25
+ * @property {function} ParameterizedQueryError
26
+ * {@link errors.ParameterizedQueryError ParameterizedQueryError} class constructor.
27
+ *
28
+ * Represents all errors that can be reported by class {@link ParameterizedQuery}.
29
+ *
30
+ * @property {function} QueryFileError
31
+ * {@link errors.QueryFileError QueryFileError} class constructor.
32
+ *
33
+ * Represents all errors that can be reported by class {@link QueryFile}.
34
+ *
35
+ * @property {function} QueryResultError
36
+ * {@link errors.QueryResultError QueryResultError} class constructor.
37
+ *
38
+ * Represents all result-specific errors from query methods.
39
+ *
40
+ * @property {errors.queryResultErrorCode} queryResultErrorCode
41
+ * Error codes `enum` used by class {@link errors.QueryResultError QueryResultError}.
42
+ *
43
+ */
44
+
45
+ module.exports = {
46
+ QueryResultError,
47
+ queryResultErrorCode,
48
+ PreparedStatementError,
49
+ ParameterizedQueryError,
50
+ QueryFileError
51
+ };
@@ -0,0 +1,71 @@
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 {QueryFileError} = require('./query-file-error');
11
+
12
+ const npm = {
13
+ os: require('os'),
14
+ utils: require('../utils'),
15
+ inspect: require('util').inspect
16
+ };
17
+
18
+ /**
19
+ * @class errors.ParameterizedQueryError
20
+ * @augments external:Error
21
+ * @description
22
+ * {@link errors.ParameterizedQueryError ParameterizedQueryError} class, available from the {@link errors} namespace.
23
+ *
24
+ * This type represents all errors that can be reported by class {@link ParameterizedQuery}, whether it is used
25
+ * explicitly or implicitly (via a simple `{text, values}` object).
26
+ *
27
+ * @property {string} name
28
+ * Standard {@link external:Error Error} property - error type name = `ParameterizedQueryError`.
29
+ *
30
+ * @property {string} message
31
+ * Standard {@link external:Error Error} property - the error message.
32
+ *
33
+ * @property {string} stack
34
+ * Standard {@link external:Error Error} property - the stack trace.
35
+ *
36
+ * @property {errors.QueryFileError} error
37
+ * Internal {@link errors.QueryFileError} object.
38
+ *
39
+ * It is set only when the source {@link ParameterizedQuery} used a {@link QueryFile} which threw the error.
40
+ *
41
+ * @property {object} result
42
+ * Resulting Parameterized Query object.
43
+ *
44
+ * @see ParameterizedQuery
45
+ */
46
+ class ParameterizedQueryError extends Error {
47
+ constructor(error, pq) {
48
+ const isQueryFileError = error instanceof QueryFileError;
49
+ const message = isQueryFileError ? 'Failed to initialize \'text\' from a QueryFile.' : error;
50
+ super(message);
51
+ this.name = this.constructor.name;
52
+ if (isQueryFileError) {
53
+ this.error = error;
54
+ }
55
+ this.result = pq;
56
+ Error.captureStackTrace(this, this.constructor);
57
+ }
58
+ }
59
+
60
+ npm.utils.addInspection(ParameterizedQueryError, function () {
61
+ const obj = {
62
+ message: this.message,
63
+ result: this.result
64
+ };
65
+ if (this.error) {
66
+ obj.error = this.error;
67
+ }
68
+ return 'ParameterizedQueryError ' + npm.inspect(obj, {breakLength: 100, depth: 5, colors: true});
69
+ });
70
+
71
+ module.exports = {ParameterizedQueryError};
@@ -0,0 +1,71 @@
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 {QueryFileError} = require('./query-file-error');
11
+
12
+ const npm = {
13
+ os: require('os'),
14
+ utils: require('../utils'),
15
+ inspect: require('util').inspect
16
+ };
17
+
18
+ /**
19
+ * @class errors.PreparedStatementError
20
+ * @augments external:Error
21
+ * @description
22
+ * {@link errors.PreparedStatementError PreparedStatementError} class, available from the {@link errors} namespace.
23
+ *
24
+ * This type represents all errors that can be reported by class {@link PreparedStatement}, whether it is used
25
+ * explicitly or implicitly (via a simple `{name, text, values}` object).
26
+ *
27
+ * @property {string} name
28
+ * Standard {@link external:Error Error} property - error type name = `PreparedStatementError`.
29
+ *
30
+ * @property {string} message
31
+ * Standard {@link external:Error Error} property - the error message.
32
+ *
33
+ * @property {string} stack
34
+ * Standard {@link external:Error Error} property - the stack trace.
35
+ *
36
+ * @property {errors.QueryFileError} error
37
+ * Internal {@link errors.QueryFileError} object.
38
+ *
39
+ * It is set only when the source {@link PreparedStatement} used a {@link QueryFile} which threw the error.
40
+ *
41
+ * @property {object} result
42
+ * Resulting Prepared Statement object.
43
+ *
44
+ * @see PreparedStatement
45
+ */
46
+ class PreparedStatementError extends Error {
47
+ constructor(error, ps) {
48
+ const isQueryFileError = error instanceof QueryFileError;
49
+ const message = isQueryFileError ? 'Failed to initialize \'text\' from a QueryFile.' : error;
50
+ super(message);
51
+ this.name = this.constructor.name;
52
+ if (isQueryFileError) {
53
+ this.error = error;
54
+ }
55
+ this.result = ps;
56
+ Error.captureStackTrace(this, this.constructor);
57
+ }
58
+ }
59
+
60
+ npm.utils.addInspection(PreparedStatementError, function () {
61
+ const obj = {
62
+ message: this.message,
63
+ result: this.result
64
+ };
65
+ if (this.error) {
66
+ obj.error = this.error;
67
+ }
68
+ return 'PreparedStatementError ' + npm.inspect(obj, {breakLength: 100, depth: 5, colors: true});
69
+ });
70
+
71
+ module.exports = {PreparedStatementError};
@@ -0,0 +1,75 @@
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 npm = {
11
+ os: require('os'),
12
+ utils: require('../utils'),
13
+ minify: require('pg-minify'),
14
+ inspect: require('util').inspect
15
+ };
16
+
17
+ /**
18
+ * @class errors.QueryFileError
19
+ * @augments external:Error
20
+ * @description
21
+ * {@link errors.QueryFileError QueryFileError} class, available from the {@link errors} namespace.
22
+ *
23
+ * This type represents all errors related to {@link QueryFile}.
24
+ *
25
+ * @property {string} name
26
+ * Standard {@link external:Error Error} property - error type name = `QueryFileError`.
27
+ *
28
+ * @property {string} message
29
+ * Standard {@link external:Error Error} property - the error message.
30
+ *
31
+ * @property {string} stack
32
+ * Standard {@link external:Error Error} property - the stack trace.
33
+ *
34
+ * @property {string} file
35
+ * File path/name that was passed into the {@link QueryFile} constructor.
36
+ *
37
+ * @property {object} options
38
+ * Set of options that was used by the {@link QueryFile} object.
39
+ *
40
+ * @property {SQLParsingError} error
41
+ * Internal $[SQLParsingError] object.
42
+ *
43
+ * It is set only when the error was thrown by $[pg-minify] while parsing the SQL file.
44
+ *
45
+ * @see QueryFile
46
+ *
47
+ */
48
+ class QueryFileError extends Error {
49
+ constructor(error, qf) {
50
+ const isSqlError = error instanceof npm.minify.SQLParsingError;
51
+ const message = isSqlError ? 'Failed to parse the SQL.' : error.message;
52
+ super(message);
53
+ this.name = this.constructor.name;
54
+ if (isSqlError) {
55
+ this.error = error;
56
+ }
57
+ this.file = qf.file;
58
+ this.options = qf.options;
59
+ Error.captureStackTrace(this, this.constructor);
60
+ }
61
+ }
62
+
63
+ npm.utils.addInspection(QueryFileError, function () {
64
+ const obj = {
65
+ message: this.message,
66
+ file: this.file,
67
+ options: this.options
68
+ };
69
+ if (this.error) {
70
+ obj.error = this.error;
71
+ }
72
+ return 'QueryFileError ' + npm.inspect(obj, {breakLength: 100, depth: 5, colors: true});
73
+ });
74
+
75
+ module.exports = {QueryFileError};
@@ -0,0 +1,157 @@
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 npm = {
11
+ os: require('os'),
12
+ utils: require('../utils'),
13
+ text: require('../text'),
14
+ inspect: require('util').inspect
15
+ };
16
+
17
+ /**
18
+ * @enum {number}
19
+ * @alias errors.queryResultErrorCode
20
+ * @readonly
21
+ * @description
22
+ * `queryResultErrorCode` enumerator, available from the {@link errors} namespace.
23
+ *
24
+ * Represents an integer code for each type of error supported by type {@link errors.QueryResultError}.
25
+ *
26
+ * @see {@link errors.QueryResultError}
27
+ */
28
+ const queryResultErrorCode = {
29
+ /** No data returned from the query. */
30
+ noData: 0,
31
+
32
+ /** No return data was expected. */
33
+ notEmpty: 1,
34
+
35
+ /** Multiple rows were not expected. */
36
+ multiple: 2
37
+ };
38
+
39
+ const errorMessages = [
40
+ {name: 'noData', message: npm.text.noData},
41
+ {name: 'notEmpty', message: npm.text.notEmpty},
42
+ {name: 'multiple', message: npm.text.multiple}
43
+ ];
44
+
45
+ /**
46
+ * @class errors.QueryResultError
47
+ * @augments external:Error
48
+ * @description
49
+ *
50
+ * This error is specified as the rejection reason for all result-specific methods when the result doesn't match
51
+ * the expectation, i.e. when a query result doesn't match its Query Result Mask - the value of {@link queryResult}.
52
+ *
53
+ * The error applies to the result from the following methods: {@link Database#none none},
54
+ * {@link Database#one one}, {@link Database#oneOrNone oneOrNone} and {@link Database#many many}.
55
+ *
56
+ * Supported errors:
57
+ *
58
+ * - `No return data was expected.`, method {@link Database#none none}
59
+ * - `No data returned from the query.`, methods {@link Database#one one} and {@link Database#many many}
60
+ * - `Multiple rows were not expected.`, methods {@link Database#one one} and {@link Database#oneOrNone oneOrNone}
61
+ *
62
+ * Like any other error, this one is notified with through the global event {@link event:error error}.
63
+ *
64
+ * The type is available from the {@link errors} namespace.
65
+ *
66
+ * @property {string} name
67
+ * Standard {@link external:Error Error} property - error type name = `QueryResultError`.
68
+ *
69
+ * @property {string} message
70
+ * Standard {@link external:Error Error} property - the error message.
71
+ *
72
+ * @property {string} stack
73
+ * Standard {@link external:Error Error} property - the stack trace.
74
+ *
75
+ * @property {object} result
76
+ * The original $[Result] object that was received.
77
+ *
78
+ * @property {number} received
79
+ * Total number of rows received. It is simply the value of `result.rows.length`.
80
+ *
81
+ * @property {number} code
82
+ * Error code - {@link errors.queryResultErrorCode queryResultErrorCode} value.
83
+ *
84
+ * @property {string} query
85
+ * Query that was executed.
86
+ *
87
+ * Normally, it is the query already formatted with values, if there were any.
88
+ * But if you are using initialization option `pgFormatting`, then the query string is before formatting.
89
+ *
90
+ * @property {*} values
91
+ * Values passed in as query parameters. Available only when initialization option `pgFormatting` is used.
92
+ * Otherwise, the values are within the pre-formatted `query` string.
93
+ *
94
+ * @example
95
+ *
96
+ * const QueryResultError = pgp.errors.QueryResultError;
97
+ * const qrec = pgp.errors.queryResultErrorCode;
98
+ *
99
+ * const initOptions = {
100
+ *
101
+ * // pg-promise initialization options...
102
+ *
103
+ * error(err, e) {
104
+ * if (err instanceof QueryResultError) {
105
+ * // A query returned unexpected number of records, and thus rejected;
106
+ *
107
+ * // we can check the error code, if we want specifics:
108
+ * if(err.code === qrec.noData) {
109
+ * // expected some data, but received none;
110
+ * }
111
+ *
112
+ * // If you write QueryResultError into the console,
113
+ * // you will get a nicely formatted output.
114
+ *
115
+ * console.log(err);
116
+ *
117
+ * // See also: err, e.query, e.params, etc.
118
+ * }
119
+ * }
120
+ * };
121
+ *
122
+ * @see
123
+ * {@link queryResult}, {@link Database#none none}, {@link Database#one one},
124
+ * {@link Database#oneOrNone oneOrNone}, {@link Database#many many}
125
+ *
126
+ */
127
+ class QueryResultError extends Error {
128
+ constructor(code, result, query, values) {
129
+ const message = errorMessages[code].message;
130
+ super(message);
131
+ this.name = this.constructor.name;
132
+ this.code = code;
133
+ this.result = result;
134
+ this.query = query;
135
+ this.values = values;
136
+ this.received = result.rows.length;
137
+ Error.captureStackTrace(this, this.constructor);
138
+ }
139
+ }
140
+
141
+ npm.utils.addInspection(QueryResultError, function () {
142
+ const obj = {
143
+ code: 'queryResultErrorCode.' + errorMessages[this.code].name,
144
+ message: this.message,
145
+ received: this.received,
146
+ query: typeof this.query === 'string' ? this.query : npm.utils.toJson(this.query)
147
+ };
148
+ if (this.values !== undefined) {
149
+ obj.values = this.values;
150
+ }
151
+ return 'QueryResultError ' + npm.inspect(obj, {breakLength: 100, depth: 5, colors: true});
152
+ });
153
+
154
+ module.exports = {
155
+ QueryResultError,
156
+ queryResultErrorCode
157
+ };