@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2018 Vitaly Tomilov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @depup/pg-promise
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [pg-promise](https://www.npmjs.com/package/pg-promise)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/pg-promise
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [pg-promise](https://www.npmjs.com/package/pg-promise) @ 12.6.2 |
|
|
17
|
+
| Processed | 2026-03-09 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 0 |
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/pg-promise
|
|
24
|
+
|
|
25
|
+
License inherited from the original package.
|
package/lib/assert.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const {assertOptions} = require('assert-options');
|
|
2
|
+
|
|
3
|
+
// this to allow override options-related errors globally (for pg-promise)
|
|
4
|
+
global.pgPromiseAssert = assertOptions;
|
|
5
|
+
|
|
6
|
+
module.exports = {
|
|
7
|
+
assert() {
|
|
8
|
+
return global.pgPromiseAssert.apply(null, [...arguments]);
|
|
9
|
+
}
|
|
10
|
+
};
|
package/lib/connect.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
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 {Events} = require('./events');
|
|
11
|
+
const {ColorConsole} = require('./utils/color');
|
|
12
|
+
|
|
13
|
+
const npm = {
|
|
14
|
+
utils: require('./utils'),
|
|
15
|
+
text: require('./text'),
|
|
16
|
+
formatting: require('./formatting')
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function poolConnect(ctx, db) {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const p = db.$pool;
|
|
22
|
+
if (p.ending) {
|
|
23
|
+
db.$destroy();
|
|
24
|
+
const err = new Error(npm.text.poolDestroyed);
|
|
25
|
+
Events.error(ctx.options, err, {
|
|
26
|
+
dc: ctx.dc
|
|
27
|
+
});
|
|
28
|
+
reject(err);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
p.connect((err, client) => {
|
|
32
|
+
if (err) {
|
|
33
|
+
Events.error(ctx.options, err, {
|
|
34
|
+
cn: npm.utils.getSafeConnection(ctx.cn),
|
|
35
|
+
dc: ctx.dc
|
|
36
|
+
});
|
|
37
|
+
reject(err);
|
|
38
|
+
} else {
|
|
39
|
+
if ('$useCount' in client) {
|
|
40
|
+
// Make sure useCount drops to 1, if it ever reaches the maximum integer number;
|
|
41
|
+
// We do not drop it to zero, to avoid rerun of initialization queries that
|
|
42
|
+
// usually check for useCount === 0;
|
|
43
|
+
// istanbul ignore if
|
|
44
|
+
if (client.$useCount >= Number.MAX_SAFE_INTEGER) {
|
|
45
|
+
client.$useCount = 1; // resetting; cannot auto-test this
|
|
46
|
+
} else {
|
|
47
|
+
client.$useCount = ++client.$useCount;
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
Object.defineProperty(client, '$useCount', {
|
|
51
|
+
value: 0,
|
|
52
|
+
configurable: false,
|
|
53
|
+
enumerable: false,
|
|
54
|
+
writable: true
|
|
55
|
+
});
|
|
56
|
+
setSchema(client, ctx);
|
|
57
|
+
}
|
|
58
|
+
setCtx(client, ctx);
|
|
59
|
+
const end = lockClientEnd(client);
|
|
60
|
+
client.on('error', onError);
|
|
61
|
+
resolve({
|
|
62
|
+
client,
|
|
63
|
+
useCount: client.$useCount,
|
|
64
|
+
release(kill) {
|
|
65
|
+
client.end = end;
|
|
66
|
+
client.release(kill || client.$connectionError);
|
|
67
|
+
Events.disconnect(ctx, client);
|
|
68
|
+
client.removeListener('error', onError);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
Events.connect(ctx, client, client.$useCount);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function directConnect(ctx, config) {
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
const client = new config.pgp.pg.Client(ctx.cn);
|
|
80
|
+
client.connect(err => {
|
|
81
|
+
if (err) {
|
|
82
|
+
Events.error(ctx.options, err, {
|
|
83
|
+
cn: npm.utils.getSafeConnection(ctx.cn),
|
|
84
|
+
dc: ctx.dc
|
|
85
|
+
});
|
|
86
|
+
reject(err);
|
|
87
|
+
} else {
|
|
88
|
+
setSchema(client, ctx);
|
|
89
|
+
setCtx(client, ctx);
|
|
90
|
+
const end = lockClientEnd(client);
|
|
91
|
+
client.on('error', onError);
|
|
92
|
+
resolve({
|
|
93
|
+
client,
|
|
94
|
+
useCount: 0,
|
|
95
|
+
release() {
|
|
96
|
+
client.end = end;
|
|
97
|
+
const p = new Promise((res, rej) => client.end().then(res).catch(rej));
|
|
98
|
+
Events.disconnect(ctx, client);
|
|
99
|
+
client.removeListener('error', onError);
|
|
100
|
+
return p;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
Events.connect(ctx, client, 0);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// this event only happens when the connection is lost physically,
|
|
110
|
+
// which cannot be tested automatically; removing from coverage:
|
|
111
|
+
// istanbul ignore next
|
|
112
|
+
function onError(err) {
|
|
113
|
+
const ctx = this.$ctx;
|
|
114
|
+
const cn = npm.utils.getSafeConnection(ctx.cn);
|
|
115
|
+
Events.error(ctx.options, err, {cn, dc: ctx.dc});
|
|
116
|
+
if (ctx.cnOptions && typeof ctx.cnOptions.onLost === 'function' && !ctx.notified) {
|
|
117
|
+
try {
|
|
118
|
+
ctx.cnOptions.onLost.call(this, err, {
|
|
119
|
+
cn,
|
|
120
|
+
dc: ctx.dc,
|
|
121
|
+
start: ctx.start,
|
|
122
|
+
client: this
|
|
123
|
+
});
|
|
124
|
+
} catch (e) {
|
|
125
|
+
ColorConsole.error(e && e.stack || e);
|
|
126
|
+
}
|
|
127
|
+
ctx.notified = true;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function lockClientEnd(client) {
|
|
132
|
+
const end = client.end;
|
|
133
|
+
client.end = doNotCall => {
|
|
134
|
+
// This call can happen only in the following two cases:
|
|
135
|
+
// 1. the client made the call directly, against the library's documentation (invalid code)
|
|
136
|
+
// 2. connection with the server broke, and the pool is terminating all clients forcefully.
|
|
137
|
+
ColorConsole.error(`${npm.text.clientEnd}\n${npm.utils.getLocalStack(1, 3)}\n`);
|
|
138
|
+
if (!doNotCall) {
|
|
139
|
+
end.call(client);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
return end;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function setCtx(client, ctx) {
|
|
146
|
+
Object.defineProperty(client, '$ctx', {
|
|
147
|
+
value: ctx,
|
|
148
|
+
writable: true
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function setSchema(client, ctx) {
|
|
153
|
+
let s = ctx.options.schema;
|
|
154
|
+
if (!s) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (typeof s === 'function') {
|
|
158
|
+
s = s.call(ctx.dc, ctx.dc);
|
|
159
|
+
}
|
|
160
|
+
if (Array.isArray(s)) {
|
|
161
|
+
s = s.filter(a => a && typeof a === 'string');
|
|
162
|
+
}
|
|
163
|
+
if (typeof s === 'string' || (Array.isArray(s) && s.length)) {
|
|
164
|
+
client.query(npm.formatting.as.format('SET search_path TO $1:name', [s]), err => {
|
|
165
|
+
// istanbul ignore if;
|
|
166
|
+
if (err) {
|
|
167
|
+
// This is unlikely to ever happen, unless the connection is created faulty,
|
|
168
|
+
// and fails on the very first query, which is impossible to test automatically.
|
|
169
|
+
throw err;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
module.exports = config => ({
|
|
176
|
+
pool: (ctx, db) => poolConnect(ctx, db),
|
|
177
|
+
direct: ctx => directConnect(ctx, config)
|
|
178
|
+
});
|
package/lib/context.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
* @class ConnectionContext
|
|
12
|
+
* @private
|
|
13
|
+
* @summary Internal connection context.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} cc
|
|
16
|
+
* Connection Context.
|
|
17
|
+
*
|
|
18
|
+
* @param {object} cc.cn
|
|
19
|
+
* Connection details
|
|
20
|
+
*
|
|
21
|
+
* @param {*} cc.dc
|
|
22
|
+
* Database Context
|
|
23
|
+
*
|
|
24
|
+
* @param {object} cc.options
|
|
25
|
+
* Library's Initialization Options
|
|
26
|
+
*
|
|
27
|
+
* @param {object} cc.db
|
|
28
|
+
* Database Session we're attached to, if any.
|
|
29
|
+
*
|
|
30
|
+
* @param {number} cc.level
|
|
31
|
+
* Task Level
|
|
32
|
+
*
|
|
33
|
+
* @param {number} cc.txLevel
|
|
34
|
+
* Transaction Level
|
|
35
|
+
*
|
|
36
|
+
* @param {object} cc.parentCtx
|
|
37
|
+
* Connection Context of the parent operation, if any.
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
class ConnectionContext {
|
|
41
|
+
|
|
42
|
+
constructor(cc) {
|
|
43
|
+
this.cn = cc.cn; // connection details;
|
|
44
|
+
this.dc = cc.dc; // database context;
|
|
45
|
+
this.options = cc.options; // library options;
|
|
46
|
+
this.db = cc.db; // database session;
|
|
47
|
+
this.level = cc.level; // task level;
|
|
48
|
+
this.txLevel = cc.txLevel; // transaction level;
|
|
49
|
+
this.parentCtx = null; // parent context
|
|
50
|
+
this.taskCtx = null; // task context
|
|
51
|
+
this.start = null; // Date/Time when connected
|
|
52
|
+
this.txCount = 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
connect(db) {
|
|
56
|
+
this.db = db;
|
|
57
|
+
this.start = new Date();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
disconnect(kill) {
|
|
61
|
+
if (this.db) {
|
|
62
|
+
const p = this.db.release(kill);
|
|
63
|
+
this.db = null;
|
|
64
|
+
return p;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
clone() {
|
|
69
|
+
const obj = new ConnectionContext(this);
|
|
70
|
+
obj.parent = this;
|
|
71
|
+
obj.parentCtx = this.taskCtx;
|
|
72
|
+
return obj;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get nextTxCount() {
|
|
76
|
+
let txCurrent = this, txTop = this;
|
|
77
|
+
while (txCurrent.parent) {
|
|
78
|
+
txCurrent = txCurrent.parent;
|
|
79
|
+
if (txCurrent.taskCtx && txCurrent.taskCtx.isTX) {
|
|
80
|
+
txTop = txCurrent;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return txTop.txCount++;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Connection Context
|
|
89
|
+
* @module context
|
|
90
|
+
* @author Vitaly Tomilov
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
module.exports = {ConnectionContext};
|
|
@@ -0,0 +1,115 @@
|
|
|
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 {ColorConsole} = require('./utils/color');
|
|
11
|
+
|
|
12
|
+
const npm = {
|
|
13
|
+
utils: require('./utils')
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @class DatabasePool
|
|
18
|
+
* @static
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
class DatabasePool {
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Global instance of the database pool repository.
|
|
25
|
+
*
|
|
26
|
+
* @returns {{dbMap: {}, dbs: Array}}
|
|
27
|
+
*/
|
|
28
|
+
static get instance() {
|
|
29
|
+
const s = Symbol.for('pgPromiseDatabasePool');
|
|
30
|
+
let scope = global[s];
|
|
31
|
+
if (!scope) {
|
|
32
|
+
scope = {
|
|
33
|
+
dbMap: {}, // map of used database context keys (connection + dc)
|
|
34
|
+
dbs: [] // all database objects
|
|
35
|
+
};
|
|
36
|
+
global[s] = scope;
|
|
37
|
+
}
|
|
38
|
+
return scope;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @method DatabasePool.register
|
|
43
|
+
* @static
|
|
44
|
+
* @description
|
|
45
|
+
* - Registers each database object, to make sure no duplicates connections are used,
|
|
46
|
+
* and if they are, produce a warning;
|
|
47
|
+
* - Registers each Pool object, to be able to release them all when requested.
|
|
48
|
+
*
|
|
49
|
+
* @param {Database} db - The new Database object being registered.
|
|
50
|
+
*/
|
|
51
|
+
static register(db) {
|
|
52
|
+
const cnKey = DatabasePool.createContextKey(db);
|
|
53
|
+
npm.utils.addReadProp(db, '$cnKey', cnKey, true);
|
|
54
|
+
const {dbMap, dbs} = DatabasePool.instance;
|
|
55
|
+
if (cnKey in dbMap) {
|
|
56
|
+
dbMap[cnKey]++;
|
|
57
|
+
/* istanbul ignore if */
|
|
58
|
+
if (!db.$config.options.noWarnings) {
|
|
59
|
+
ColorConsole.warn(`WARNING: Creating a duplicate database object for the same connection.\n${npm.utils.getLocalStack(4, 3)}\n`);
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
dbMap[cnKey] = 1;
|
|
63
|
+
}
|
|
64
|
+
dbs.push(db);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @method DatabasePool.unregister
|
|
69
|
+
* @static
|
|
70
|
+
* @param db
|
|
71
|
+
*/
|
|
72
|
+
static unregister(db) {
|
|
73
|
+
const cnKey = db.$cnKey;
|
|
74
|
+
const {dbMap} = DatabasePool.instance;
|
|
75
|
+
if (!--dbMap[cnKey]) {
|
|
76
|
+
delete dbMap[cnKey];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @method DatabasePool.shutDown
|
|
82
|
+
* @static
|
|
83
|
+
*/
|
|
84
|
+
static shutDown() {
|
|
85
|
+
const {instance} = DatabasePool;
|
|
86
|
+
instance.dbs.forEach(db => {
|
|
87
|
+
db.$destroy();
|
|
88
|
+
});
|
|
89
|
+
instance.dbs.length = 0;
|
|
90
|
+
instance.dbMap = {};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @method DatabasePool.createContextKey
|
|
95
|
+
* @static
|
|
96
|
+
* @description
|
|
97
|
+
* For connections that are objects it reorders the keys alphabetically,
|
|
98
|
+
* and then serializes the result into a JSON string.
|
|
99
|
+
*
|
|
100
|
+
* @param {Database} db - Database instance.
|
|
101
|
+
*/
|
|
102
|
+
static createContextKey(db) {
|
|
103
|
+
let cn = db.$cn;
|
|
104
|
+
if (typeof cn === 'object') {
|
|
105
|
+
const obj = {}, keys = Object.keys(cn).sort();
|
|
106
|
+
keys.forEach(name => {
|
|
107
|
+
obj[name] = cn[name];
|
|
108
|
+
});
|
|
109
|
+
cn = obj;
|
|
110
|
+
}
|
|
111
|
+
return npm.utils.toJson(npm.utils.getSafeConnection(cn)) + npm.utils.toJson(db.$dc);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {DatabasePool};
|