@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.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/lib/assert.js +10 -0
- package/lib/connect.js +178 -0
- package/lib/context.js +93 -0
- package/lib/database-pool.js +115 -0
- package/lib/database.js +1643 -0
- package/lib/errors/README.md +13 -0
- package/lib/errors/index.js +51 -0
- package/lib/errors/parameterized-query-error.js +71 -0
- package/lib/errors/prepared-statement-error.js +71 -0
- package/lib/errors/query-file-error.js +75 -0
- package/lib/errors/query-result-error.js +157 -0
- package/lib/events.js +543 -0
- package/lib/formatting.js +932 -0
- package/lib/helpers/README.md +10 -0
- package/lib/helpers/column-set.js +614 -0
- package/lib/helpers/column.js +406 -0
- package/lib/helpers/index.js +75 -0
- package/lib/helpers/methods/concat.js +103 -0
- package/lib/helpers/methods/index.js +13 -0
- package/lib/helpers/methods/insert.js +151 -0
- package/lib/helpers/methods/sets.js +81 -0
- package/lib/helpers/methods/update.js +248 -0
- package/lib/helpers/methods/values.js +116 -0
- package/lib/helpers/table-name.js +175 -0
- package/lib/index.js +29 -0
- package/lib/inner-state.js +39 -0
- package/lib/main.js +394 -0
- package/lib/patterns.js +43 -0
- package/lib/query-file.js +379 -0
- package/lib/query-result.js +39 -0
- package/lib/query.js +273 -0
- package/lib/special-query.js +30 -0
- package/lib/stream.js +125 -0
- package/lib/task.js +404 -0
- package/lib/text.js +40 -0
- package/lib/tx-mode.js +194 -0
- package/lib/types/index.js +18 -0
- package/lib/types/parameterized-query.js +247 -0
- package/lib/types/prepared-statement.js +298 -0
- package/lib/types/server-formatting.js +92 -0
- package/lib/utils/README.md +13 -0
- package/lib/utils/color.js +68 -0
- package/lib/utils/index.js +199 -0
- package/lib/utils/public.js +312 -0
- package/package.json +77 -0
- package/typescript/README.md +63 -0
- package/typescript/pg-promise.d.ts +728 -0
- package/typescript/pg-subset.d.ts +359 -0
- package/typescript/tslint.json +21 -0
|
@@ -0,0 +1,175 @@
|
|
|
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 {assert} = require('../assert');
|
|
11
|
+
|
|
12
|
+
const npm = {
|
|
13
|
+
utils: require('../utils'),
|
|
14
|
+
format: require('../formatting').as // formatting namespace
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @class helpers.TableName
|
|
19
|
+
* @description
|
|
20
|
+
* Represents a full table name that can be injected into queries directly.
|
|
21
|
+
*
|
|
22
|
+
* This is a read-only type that can be used wherever parameter `table` is supported.
|
|
23
|
+
*
|
|
24
|
+
* It supports $[Custom Type Formatting], which means you can use the type directly as a formatting
|
|
25
|
+
* parameter, without specifying any escaping.
|
|
26
|
+
*
|
|
27
|
+
* Filter `:alias` is an alternative approach to splitting an SQL name into multiple ones.
|
|
28
|
+
*
|
|
29
|
+
* @param {string|Table} table
|
|
30
|
+
* Table name details, depending on the type:
|
|
31
|
+
*
|
|
32
|
+
* - table name, if `table` is a string
|
|
33
|
+
* - {@link Table} object
|
|
34
|
+
*
|
|
35
|
+
* @property {string} name
|
|
36
|
+
* Escaped full table name, combining `schema` + `table`.
|
|
37
|
+
*
|
|
38
|
+
* @property {string} table
|
|
39
|
+
* Table name.
|
|
40
|
+
*
|
|
41
|
+
* @property {string} schema
|
|
42
|
+
* Database schema name.
|
|
43
|
+
*
|
|
44
|
+
* It is `undefined` when no valid schema was specified.
|
|
45
|
+
*
|
|
46
|
+
* @returns {helpers.TableName}
|
|
47
|
+
*
|
|
48
|
+
* @see
|
|
49
|
+
* {@link helpers._TN _TN},
|
|
50
|
+
* {@link helpers.TableName#toPostgres toPostgres}
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
*
|
|
54
|
+
* const table = new pgp.helpers.TableName({table: 'my-table', schema: 'my-schema'});
|
|
55
|
+
* console.log(table);
|
|
56
|
+
* //=> "my-schema"."my-table"
|
|
57
|
+
*
|
|
58
|
+
* // Formatting the type directly:
|
|
59
|
+
* pgp.as.format('SELECT * FROM $1', table);
|
|
60
|
+
* //=> SELECT * FROM "my-schema"."my-table"
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
class TableName {
|
|
64
|
+
|
|
65
|
+
constructor(table) {
|
|
66
|
+
if (typeof table === 'string') {
|
|
67
|
+
this.table = table;
|
|
68
|
+
} else {
|
|
69
|
+
const config = assert(table, ['table', 'schema']);
|
|
70
|
+
this.table = config.table;
|
|
71
|
+
if (npm.utils.isText(config.schema)) {
|
|
72
|
+
this.schema = config.schema;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!npm.utils.isText(this.table)) {
|
|
76
|
+
throw new TypeError('Table name must be a non-empty text string.');
|
|
77
|
+
}
|
|
78
|
+
this.name = npm.format.name(this.table);
|
|
79
|
+
if (this.schema) {
|
|
80
|
+
this.name = npm.format.name(this.schema) + '.' + this.name;
|
|
81
|
+
}
|
|
82
|
+
Object.freeze(this);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @method helpers.TableName#toPostgres
|
|
88
|
+
* @description
|
|
89
|
+
* $[Custom Type Formatting], based on $[Symbolic CTF], i.e. the actual method is available only via {@link external:Symbol Symbol}:
|
|
90
|
+
*
|
|
91
|
+
* ```js
|
|
92
|
+
* const {toPostgres} = pgp.as.ctf; // Custom Type Formatting symbols namespace
|
|
93
|
+
* const fullName = tn[toPostgres]; // tn = an object of type TableName
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* This is a raw formatting type (`rawType = true`), i.e. when used as a query-formatting parameter, type `TableName`
|
|
97
|
+
* injects full table name as raw text.
|
|
98
|
+
*
|
|
99
|
+
* @param {helpers.TableName} [self]
|
|
100
|
+
* Optional self-reference, for ES6 arrow functions.
|
|
101
|
+
*
|
|
102
|
+
* @returns {string}
|
|
103
|
+
* Escaped full table name that includes optional schema name, if specified.
|
|
104
|
+
*/
|
|
105
|
+
TableName.prototype[npm.format.ctf.toPostgres] = function (self) {
|
|
106
|
+
self = this instanceof TableName && this || self;
|
|
107
|
+
return self.name;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
TableName.prototype[npm.format.ctf.rawType] = true; // use as pre-formatted
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @method helpers.TableName#toString
|
|
114
|
+
* @description
|
|
115
|
+
* Redirects into `name` property, for a fully escaped table name.
|
|
116
|
+
*
|
|
117
|
+
* @returns {string}
|
|
118
|
+
*/
|
|
119
|
+
TableName.prototype.toString = function () {
|
|
120
|
+
return this.name;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @interface Table
|
|
125
|
+
* @description
|
|
126
|
+
* Structure for any table name/path.
|
|
127
|
+
*
|
|
128
|
+
* Function {@link helpers._TN _TN} can help you construct it from a string.
|
|
129
|
+
*
|
|
130
|
+
* @property {string} [schema] - schema name, if specified
|
|
131
|
+
* @property {string} table - table name
|
|
132
|
+
*
|
|
133
|
+
* @see {@link helpers.TableName TableName}, {@link helpers._TN _TN}
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @external TemplateStringsArray
|
|
138
|
+
* @see https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_es5_d_.templatestringsarray.html
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @function helpers._TN
|
|
143
|
+
* @description
|
|
144
|
+
* Table-Name helper function, to convert any `"schema.table"` string
|
|
145
|
+
* into `{schema, table}` object. It also works as a template-tag function.
|
|
146
|
+
*
|
|
147
|
+
* @param {string|TemplateStringsArray} path
|
|
148
|
+
* Table-name path, as a simple string or a template string (with parameters).
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* const {ColumnSet, _TN} = pgp.helpers;
|
|
152
|
+
*
|
|
153
|
+
* // Using as a regular function:
|
|
154
|
+
* const cs1 = new ColumnSet(['id', 'name'], {table: _TN('schema.table')});
|
|
155
|
+
*
|
|
156
|
+
* // Using as a template-tag function:
|
|
157
|
+
* const schema = 'schema';
|
|
158
|
+
* const cs2 = new ColumnSet(['id', 'name'], {table: _TN`${schema}.table`});
|
|
159
|
+
*
|
|
160
|
+
* @returns {Table}
|
|
161
|
+
*
|
|
162
|
+
* @see {@link helpers.TableName TableName}, {@link external:TemplateStringsArray TemplateStringsArray}
|
|
163
|
+
*/
|
|
164
|
+
function _TN(path, ...args) {
|
|
165
|
+
if (Array.isArray(path)) {
|
|
166
|
+
path = path.reduce((a, c, i) => a + c + (args[i] ?? ''), '');
|
|
167
|
+
} // else 'path' is a string
|
|
168
|
+
const [schema, table] = path.split('.');
|
|
169
|
+
if (table === undefined) {
|
|
170
|
+
return {table: schema};
|
|
171
|
+
}
|
|
172
|
+
return schema ? {schema, table} : {table};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
module.exports = {TableName, _TN};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
/* eslint no-var: off */
|
|
11
|
+
var v = process.versions.node.split('.'),
|
|
12
|
+
highVer = +v[0];
|
|
13
|
+
|
|
14
|
+
// istanbul ignore next
|
|
15
|
+
if (highVer < 16) {
|
|
16
|
+
|
|
17
|
+
// From pg-promise v11.15.0, the oldest supported Node.js is v16.0.0
|
|
18
|
+
|
|
19
|
+
// Node.js v14.x was supported up to pg-promise v11.14.0
|
|
20
|
+
// Node.js v12.x was supported up to pg-promise v10.15.4
|
|
21
|
+
// Node.js v8.x was supported up to pg-promise v10.14.2
|
|
22
|
+
// Node.js v7.6.0 was supported up to pg-promise v10.3.5
|
|
23
|
+
// Node.js v4.5.0 was supported up to pg-promise v8.7.5
|
|
24
|
+
// Node.js v0.10 was supported up to pg-promise v5.5.8
|
|
25
|
+
|
|
26
|
+
throw new Error('Minimum Node.js version supported by pg-promise is 16.0.0');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = require('./main');
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const {addReadProp} = require('./utils');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @private
|
|
5
|
+
* @class InnerState
|
|
6
|
+
* @description
|
|
7
|
+
* Implements support for private/inner state object inside the class,
|
|
8
|
+
* which can be accessed by a derived class via hidden read-only property _inner.
|
|
9
|
+
*/
|
|
10
|
+
class InnerState {
|
|
11
|
+
|
|
12
|
+
constructor(initialState) {
|
|
13
|
+
addReadProp(this, '_inner', {}, true);
|
|
14
|
+
if (initialState && typeof initialState === 'object') {
|
|
15
|
+
this.extendState(initialState);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Extends or overrides inner state with the specified properties.
|
|
21
|
+
*
|
|
22
|
+
* Only own properties are used, i.e. inherited ones are skipped.
|
|
23
|
+
*/
|
|
24
|
+
extendState(state) {
|
|
25
|
+
for (const a in state) {
|
|
26
|
+
// istanbul ignore else
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(state, a)) {
|
|
28
|
+
this._inner[a] = state[a];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @member InnerState#_inner
|
|
36
|
+
* Private/Inner object state.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
module.exports = {InnerState};
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
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 {DatabasePool} = require('./database-pool');
|
|
11
|
+
const {PreparedStatement, ParameterizedQuery} = require('./types');
|
|
12
|
+
const {QueryFile} = require('./query-file');
|
|
13
|
+
const {queryResult} = require('./query-result');
|
|
14
|
+
const {assert} = require('./assert');
|
|
15
|
+
|
|
16
|
+
const npm = {
|
|
17
|
+
path: require('path'),
|
|
18
|
+
pg: require('pg'),
|
|
19
|
+
minify: require('pg-minify'),
|
|
20
|
+
formatting: require('./formatting'),
|
|
21
|
+
helpers: require('./helpers'),
|
|
22
|
+
errors: require('./errors'),
|
|
23
|
+
utils: require('./utils'),
|
|
24
|
+
pubUtils: require('./utils/public'),
|
|
25
|
+
mode: require('./tx-mode'),
|
|
26
|
+
package: require('../package.json'),
|
|
27
|
+
text: require('./text')
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
let originalClientConnect;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @author Vitaly Tomilov
|
|
34
|
+
* @module pg-promise
|
|
35
|
+
*
|
|
36
|
+
* @description
|
|
37
|
+
* ## pg-promise v12.5
|
|
38
|
+
* All documentation here is for the latest official release only.
|
|
39
|
+
*
|
|
40
|
+
* ### Initialization Options
|
|
41
|
+
*
|
|
42
|
+
* Below is the complete list of _Initialization Options_ for the library that can be passed in during
|
|
43
|
+
* the library's initialization:
|
|
44
|
+
*
|
|
45
|
+
* ```js
|
|
46
|
+
* const initOptions = {/* options as documented below */};
|
|
47
|
+
*
|
|
48
|
+
* const pgp = require('pg-promise')(initOptions);
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @property {{}} [options]
|
|
52
|
+
* Library Initialization Options.
|
|
53
|
+
*
|
|
54
|
+
* @property {boolean} [options.pgFormatting=false]
|
|
55
|
+
* Redirects all query formatting to the $[pg] driver.
|
|
56
|
+
*
|
|
57
|
+
* By default (`false`), the library uses its own advanced query-formatting engine.
|
|
58
|
+
* If you set this option to a truthy value, query formatting will be done entirely by the
|
|
59
|
+
* $[pg] driver, which means you won't be able to use any of the feature-rich query formatting
|
|
60
|
+
* that this library implements, restricting yourself to the very basic `$1, $2,...` syntax.
|
|
61
|
+
*
|
|
62
|
+
* This option is dynamic (can be set before or after initialization).
|
|
63
|
+
*
|
|
64
|
+
* @property {boolean} [options.pgNative=false]
|
|
65
|
+
* Use $[Native Bindings]. Library $[pg-native] must be included and installed independently, or else there will
|
|
66
|
+
* be an error thrown: {@link external:Error Error} = `Failed to initialize Native Bindings.`
|
|
67
|
+
*
|
|
68
|
+
* This is a static option (can only be set prior to initialization).
|
|
69
|
+
*
|
|
70
|
+
* @property {boolean} [options.capSQL=false]
|
|
71
|
+
* Capitalizes any SQL generated by the library.
|
|
72
|
+
*
|
|
73
|
+
* By default, all internal SQL within the library is generated using the low case.
|
|
74
|
+
* If, however, you want all SQL to be capitalized instead, set `capSQL` = `true`.
|
|
75
|
+
*
|
|
76
|
+
* It is purely a cosmetic feature.
|
|
77
|
+
*
|
|
78
|
+
* This option is dynamic (can be set before or after initialization).
|
|
79
|
+
*
|
|
80
|
+
* @property {string|Array<string>|null|undefined|function} [options.schema]
|
|
81
|
+
* Forces change of the default database schema(s) for every fresh connection, i.e.
|
|
82
|
+
* the library will execute `SET search_path TO schema_1, schema_2, ...` in the background
|
|
83
|
+
* whenever a fresh physical connection is allocated.
|
|
84
|
+
*
|
|
85
|
+
* Normally, one changes the default schema(s) by $[changing the database or the role], but sometimes you
|
|
86
|
+
* may want to switch the default schema(s) without persisting the change, and then use this option.
|
|
87
|
+
*
|
|
88
|
+
* It can be a string, an array of strings, or a callback function that takes `dc` (database context)
|
|
89
|
+
* as the only parameter (and as `this`), and returns schema(s) according to the database context. A callback function
|
|
90
|
+
* can also return nothing (`undefined` or `null`), if no schema change needed for the specified database context.
|
|
91
|
+
*
|
|
92
|
+
* The order of schema names matters, so if a table name exists in more than one schema, its default access resolves
|
|
93
|
+
* to the table from the first such schema on the list.
|
|
94
|
+
*
|
|
95
|
+
* This option is dynamic (can be set before or after initialization).
|
|
96
|
+
*
|
|
97
|
+
* @property {boolean} [options.noWarnings=false]
|
|
98
|
+
* Disables all diagnostic warnings in the library (it is ill-advised).
|
|
99
|
+
*
|
|
100
|
+
* This option is dynamic (can be set before or after initialization).
|
|
101
|
+
*
|
|
102
|
+
* @property {function} [options.connect]
|
|
103
|
+
* Global event {@link event:connect connect} handler.
|
|
104
|
+
*
|
|
105
|
+
* This option is dynamic (can be set before or after initialization).
|
|
106
|
+
*
|
|
107
|
+
* @property {function} [options.disconnect]
|
|
108
|
+
* Global event {@link event:disconnect disconnect} handler.
|
|
109
|
+
*
|
|
110
|
+
* This option is dynamic (can be set before or after initialization).
|
|
111
|
+
*
|
|
112
|
+
* @property {function} [options.query]
|
|
113
|
+
* Global event {@link event:query query} handler.
|
|
114
|
+
*
|
|
115
|
+
* This option is dynamic (can be set before or after initialization).
|
|
116
|
+
*
|
|
117
|
+
* @property {function} [options.receive]
|
|
118
|
+
* Global event {@link event:receive receive} handler.
|
|
119
|
+
*
|
|
120
|
+
* This option is dynamic (can be set before or after initialization).
|
|
121
|
+
*
|
|
122
|
+
* @property {function} [options.task]
|
|
123
|
+
* Global event {@link event:task task} handler.
|
|
124
|
+
*
|
|
125
|
+
* This option is dynamic (can be set before or after initialization).
|
|
126
|
+
*
|
|
127
|
+
* @property {function} [options.transact]
|
|
128
|
+
* Global event {@link event:transact transact} handler.
|
|
129
|
+
*
|
|
130
|
+
* This option is dynamic (can be set before or after initialization).
|
|
131
|
+
*
|
|
132
|
+
* @property {function} [options.error]
|
|
133
|
+
* Global event {@link event:error error} handler.
|
|
134
|
+
*
|
|
135
|
+
* This option is dynamic (can be set before or after initialization).
|
|
136
|
+
*
|
|
137
|
+
* @property {function} [options.extend]
|
|
138
|
+
* Global event {@link event:extend extend} handler.
|
|
139
|
+
*
|
|
140
|
+
* This option is dynamic (can be set before or after initialization).
|
|
141
|
+
*
|
|
142
|
+
* @see
|
|
143
|
+
* {@link module:pg-promise~end end},
|
|
144
|
+
* {@link module:pg-promise~as as},
|
|
145
|
+
* {@link module:pg-promise~errors errors},
|
|
146
|
+
* {@link module:pg-promise~helpers helpers},
|
|
147
|
+
* {@link module:pg-promise~minify minify},
|
|
148
|
+
* {@link module:pg-promise~ParameterizedQuery ParameterizedQuery},
|
|
149
|
+
* {@link module:pg-promise~PreparedStatement PreparedStatement},
|
|
150
|
+
* {@link module:pg-promise~pg pg},
|
|
151
|
+
* {@link module:pg-promise~QueryFile QueryFile},
|
|
152
|
+
* {@link module:pg-promise~queryResult queryResult},
|
|
153
|
+
* {@link module:pg-promise~spex spex},
|
|
154
|
+
* {@link module:pg-promise~txMode txMode},
|
|
155
|
+
* {@link module:pg-promise~utils utils}
|
|
156
|
+
*
|
|
157
|
+
*/
|
|
158
|
+
function $main(options) {
|
|
159
|
+
|
|
160
|
+
options = assert(options, ['pgFormatting', 'pgNative', 'capSQL', 'noWarnings',
|
|
161
|
+
'connect', 'disconnect', 'query', 'receive', 'task', 'transact', 'error', 'extend', 'schema']);
|
|
162
|
+
|
|
163
|
+
let pg = npm.pg;
|
|
164
|
+
|
|
165
|
+
const config = {
|
|
166
|
+
version: npm.package.version
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
npm.utils.addReadProp(config, '$npm', {}, true);
|
|
170
|
+
|
|
171
|
+
// Locking properties that cannot be changed later:
|
|
172
|
+
npm.utils.addReadProp(options, 'pgNative', !!options.pgNative);
|
|
173
|
+
|
|
174
|
+
config.options = options;
|
|
175
|
+
|
|
176
|
+
// istanbul ignore next:
|
|
177
|
+
// we do not cover code specific to Native Bindings
|
|
178
|
+
if (options.pgNative) {
|
|
179
|
+
pg = npm.pg.native;
|
|
180
|
+
if (npm.utils.isNull(pg)) {
|
|
181
|
+
throw new Error(npm.text.nativeError);
|
|
182
|
+
}
|
|
183
|
+
} else {
|
|
184
|
+
if (!originalClientConnect) {
|
|
185
|
+
originalClientConnect = pg.Client.prototype.connect;
|
|
186
|
+
pg.Client.prototype.connect = function () {
|
|
187
|
+
const handler = msg => {
|
|
188
|
+
if (msg.parameterName === 'server_version') {
|
|
189
|
+
this.serverVersion = msg.parameterValue;
|
|
190
|
+
this.connection.removeListener('parameterStatus', handler);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
this.connection.on('parameterStatus', handler);
|
|
194
|
+
return originalClientConnect.call(this, ...arguments);
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const Database = require('./database')(config);
|
|
200
|
+
|
|
201
|
+
const inst = (cn, dc) => {
|
|
202
|
+
if (npm.utils.isText(cn) || (cn && typeof cn === 'object')) {
|
|
203
|
+
return new Database(cn, dc, config);
|
|
204
|
+
}
|
|
205
|
+
throw new TypeError('Invalid connection details: ' + npm.utils.toJson(cn));
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
npm.utils.addReadProperties(inst, rootNameSpace);
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @member {external:PG} pg
|
|
212
|
+
* @description
|
|
213
|
+
* Instance of the $[pg] library being used, depending on initialization option `pgNative`:
|
|
214
|
+
* - regular `pg` module instance, without option `pgNative`, or equal to `false` (default)
|
|
215
|
+
* - `pg` module instance with $[Native Bindings], if option `pgNative` was set.
|
|
216
|
+
*
|
|
217
|
+
* Available as `pgp.pg`, after initializing the library.
|
|
218
|
+
*/
|
|
219
|
+
inst.pg = pg; // keep it modifiable, so the protocol can be mocked
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @member {function} end
|
|
223
|
+
* @readonly
|
|
224
|
+
* @description
|
|
225
|
+
* Shuts down all connection pools created in the process, so it can terminate without delay.
|
|
226
|
+
* It is available as `pgp.end`, after initializing the library.
|
|
227
|
+
*
|
|
228
|
+
* All {@link Database} objects created previously can no longer be used, and their query methods will be rejecting
|
|
229
|
+
* with {@link external:Error Error} = `Connection pool of the database object has been destroyed.`
|
|
230
|
+
*
|
|
231
|
+
* And if you want to shut down only a specific connection pool, you do so via the {@link Database}
|
|
232
|
+
* object that owns the pool: `db.$pool.end()` (see {@link Database#$pool Database.$pool}).
|
|
233
|
+
*
|
|
234
|
+
* For more details see $[Library de-initialization].
|
|
235
|
+
*/
|
|
236
|
+
npm.utils.addReadProp(inst, 'end', () => {
|
|
237
|
+
DatabasePool.shutDown();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @member {helpers} helpers
|
|
242
|
+
* @readonly
|
|
243
|
+
* @description
|
|
244
|
+
* Namespace for {@link helpers all query-formatting helper functions}.
|
|
245
|
+
*
|
|
246
|
+
* Available as `pgp.helpers`, after initializing the library.
|
|
247
|
+
*
|
|
248
|
+
* @see {@link helpers}.
|
|
249
|
+
*/
|
|
250
|
+
npm.utils.addReadProp(inst, 'helpers', npm.helpers(config));
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @member {external:spex} spex
|
|
254
|
+
* @readonly
|
|
255
|
+
* @description
|
|
256
|
+
* Initialized instance of the $[spex] module, used by the library within tasks and transactions.
|
|
257
|
+
*
|
|
258
|
+
* Available as `pgp.spex`, after initializing the library.
|
|
259
|
+
*
|
|
260
|
+
* @see
|
|
261
|
+
* {@link Task#batch},
|
|
262
|
+
* {@link Task#page},
|
|
263
|
+
* {@link Task#sequence}
|
|
264
|
+
*/
|
|
265
|
+
npm.utils.addReadProp(inst, 'spex', config.$npm.spex);
|
|
266
|
+
|
|
267
|
+
config.pgp = inst;
|
|
268
|
+
|
|
269
|
+
return inst;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const rootNameSpace = {
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @member {formatting} as
|
|
276
|
+
* @readonly
|
|
277
|
+
* @description
|
|
278
|
+
* Namespace for {@link formatting all query-formatting functions}.
|
|
279
|
+
*
|
|
280
|
+
* Available as `pgp.as`, before and after initializing the library.
|
|
281
|
+
*
|
|
282
|
+
* @see {@link formatting}.
|
|
283
|
+
*/
|
|
284
|
+
as: npm.formatting.as,
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* @member {external:pg-minify} minify
|
|
288
|
+
* @readonly
|
|
289
|
+
* @description
|
|
290
|
+
* Instance of the $[pg-minify] library used internally to minify SQL scripts.
|
|
291
|
+
*
|
|
292
|
+
* Available as `pgp.minify`, before and after initializing the library.
|
|
293
|
+
*/
|
|
294
|
+
minify: npm.minify,
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* @member {queryResult} queryResult
|
|
298
|
+
* @readonly
|
|
299
|
+
* @description
|
|
300
|
+
* Query Result Mask enumerator.
|
|
301
|
+
*
|
|
302
|
+
* Available as `pgp.queryResult`, before and after initializing the library.
|
|
303
|
+
*/
|
|
304
|
+
queryResult,
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* @member {ParameterizedQuery} ParameterizedQuery
|
|
308
|
+
* @readonly
|
|
309
|
+
* @description
|
|
310
|
+
* {@link ParameterizedQuery} class.
|
|
311
|
+
*
|
|
312
|
+
* Available as `pgp.ParameterizedQuery`, before and after initializing the library.
|
|
313
|
+
*/
|
|
314
|
+
ParameterizedQuery,
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @member {PreparedStatement} PreparedStatement
|
|
318
|
+
* @readonly
|
|
319
|
+
* @description
|
|
320
|
+
* {@link PreparedStatement} class.
|
|
321
|
+
*
|
|
322
|
+
* Available as `pgp.PreparedStatement`, before and after initializing the library.
|
|
323
|
+
*/
|
|
324
|
+
PreparedStatement,
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* @member {QueryFile} QueryFile
|
|
328
|
+
* @readonly
|
|
329
|
+
* @description
|
|
330
|
+
* {@link QueryFile} class.
|
|
331
|
+
*
|
|
332
|
+
* Available as `pgp.QueryFile`, before and after initializing the library.
|
|
333
|
+
*/
|
|
334
|
+
QueryFile,
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @member {errors} errors
|
|
338
|
+
* @readonly
|
|
339
|
+
* @description
|
|
340
|
+
* {@link errors} - namespace for all error types.
|
|
341
|
+
*
|
|
342
|
+
* Available as `pgp.errors`, before and after initializing the library.
|
|
343
|
+
*/
|
|
344
|
+
errors: npm.errors,
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* @member {utils} utils
|
|
348
|
+
* @readonly
|
|
349
|
+
* @description
|
|
350
|
+
* {@link utils} - namespace for utility functions.
|
|
351
|
+
*
|
|
352
|
+
* Available as `pgp.utils`, before and after initializing the library.
|
|
353
|
+
*/
|
|
354
|
+
utils: npm.pubUtils,
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* @member {txMode} txMode
|
|
358
|
+
* @readonly
|
|
359
|
+
* @description
|
|
360
|
+
* {@link txMode Transaction Mode} namespace.
|
|
361
|
+
*
|
|
362
|
+
* Available as `pgp.txMode`, before and after initializing the library.
|
|
363
|
+
*/
|
|
364
|
+
txMode: npm.mode
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
npm.utils.addReadProperties($main, rootNameSpace);
|
|
368
|
+
|
|
369
|
+
module.exports = $main;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* @external Promise
|
|
373
|
+
* @see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
|
374
|
+
*/
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* @external PG
|
|
378
|
+
* @see https://node-postgres.com
|
|
379
|
+
*/
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @external Client
|
|
383
|
+
* @see https://node-postgres.com/apis/client
|
|
384
|
+
*/
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* @external pg-minify
|
|
388
|
+
* @see https://github.com/vitaly-t/pg-minify
|
|
389
|
+
*/
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* @external spex
|
|
393
|
+
* @see https://github.com/vitaly-t/spex
|
|
394
|
+
*/
|
package/lib/patterns.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
/*
|
|
11
|
+
The most important regular expressions and data as used by the library,
|
|
12
|
+
isolated here to help with possible edge cases during integration.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
module.exports = {
|
|
16
|
+
// Searches for all Named Parameters, supporting any of the following syntax:
|
|
17
|
+
// ${propName}, $(propName), $[propName], $/propName/, $<propName>
|
|
18
|
+
// Nested property names are also supported: ${propName.abc}
|
|
19
|
+
namedParameters: /\$(?:({)|(\()|(<)|(\[)|(\/))\s*[a-zA-Z0-9$_.]+(\^|~|#|:raw|:alias|:name|:json|:csv|:list|:value)?\s*(?:(?=\2)(?=\3)(?=\4)(?=\5)}|(?=\1)(?=\3)(?=\4)(?=\5)\)|(?=\1)(?=\2)(?=\4)(?=\5)>|(?=\1)(?=\2)(?=\3)(?=\5)]|(?=\1)(?=\2)(?=\3)(?=\4)\/)/g,
|
|
20
|
+
|
|
21
|
+
// Searches for all variables $1, $2, ...$100000, and while it will find greater than $100000
|
|
22
|
+
// variables, the formatting engine is expected to throw an error for those.
|
|
23
|
+
multipleValues: /\$([1-9][0-9]{0,16}(?![0-9])(\^|~|#|:raw|:alias|:name|:json|:csv|:list|:value)?)/g,
|
|
24
|
+
|
|
25
|
+
// Searches for all occurrences of variable $1
|
|
26
|
+
singleValue: /\$1(?![0-9])(\^|~|#|:raw|:alias|:name|:json|:csv|:list|:value)?/g,
|
|
27
|
+
|
|
28
|
+
// Matches a valid column name for the Column type parser, according to the following rules:
|
|
29
|
+
// - can contain: any combination of a-z, A-Z, 0-9, $ or _
|
|
30
|
+
// - can contain ? at the start
|
|
31
|
+
// - can contain one of the supported filters/modifiers
|
|
32
|
+
validColumn: /\??[a-zA-Z0-9$_]+(\^|~|#|:raw|:alias|:name|:json|:csv|:list|:value)?/,
|
|
33
|
+
|
|
34
|
+
// Matches a valid open-name JavaScript variable, according to the following rules:
|
|
35
|
+
// - can contain: any combination of a-z, A-Z, 0-9, $ or _
|
|
36
|
+
validVariable: /[a-zA-Z0-9$_]+/,
|
|
37
|
+
|
|
38
|
+
// Matches a valid modifier in a column/property:
|
|
39
|
+
hasValidModifier: /\^|~|#|:raw|:alias|:name|:json|:csv|:list|:value/,
|
|
40
|
+
|
|
41
|
+
// List of all supported formatting modifiers:
|
|
42
|
+
validModifiers: ['^', '~', '#', ':raw', ':alias', ':name', ':json', ':csv', ':list', ':value']
|
|
43
|
+
};
|