pg-promise 10.9.3 → 10.10.2
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/README.md +2 -5
- package/lib/connect.js +6 -2
- package/lib/context.js +5 -4
- package/lib/database.js +7 -3
- package/package.json +6 -7
- package/typescript/README.MD +14 -6
- package/typescript/pg-promise.d.ts +12 -2
package/README.md
CHANGED
|
@@ -5,7 +5,6 @@ PostgreSQL interface for Node.js
|
|
|
5
5
|
|
|
6
6
|
[](https://travis-ci.org/vitaly-t/pg-promise)
|
|
7
7
|
[](https://gitter.im/vitaly-t/pg-promise?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
8
|
-
[](https://xscode.com/vitaly-tomilov/pg-promise)
|
|
9
8
|
|
|
10
9
|
---
|
|
11
10
|
|
|
@@ -60,11 +59,9 @@ only its tiny part.
|
|
|
60
59
|
|
|
61
60
|
# Support & Sponsorship
|
|
62
61
|
|
|
63
|
-
|
|
62
|
+
I do free support here and on [StackOverflow](https://stackoverflow.com/questions/tagged/pg-promise).
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
We also do free support here and on [StackOverflow](https://stackoverflow.com/questions/tagged/pg-promise).
|
|
64
|
+
And if you want to help this project, I can accept Bitcoin: `1yki7MXMkuDw8qqe5icVdh1GJZSQSzKZp`
|
|
68
65
|
|
|
69
66
|
# Documentation
|
|
70
67
|
|
package/lib/connect.js
CHANGED
|
@@ -37,7 +37,10 @@ function poolConnect(ctx, db, config) {
|
|
|
37
37
|
reject(err);
|
|
38
38
|
} else {
|
|
39
39
|
if (`$useCount` in client) {
|
|
40
|
-
|
|
40
|
+
// Make sure useCount drops to 1, if it ever reaches 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
|
+
client.$useCount = client.$useCount >= Number.MAX_SAFE_INTEGER ? 1 : ++client.$useCount;
|
|
41
44
|
} else {
|
|
42
45
|
Object.defineProperty(client, `$useCount`, {
|
|
43
46
|
value: 0,
|
|
@@ -86,9 +89,10 @@ function directConnect(ctx, config) {
|
|
|
86
89
|
useCount: 0,
|
|
87
90
|
release() {
|
|
88
91
|
client.end = end;
|
|
89
|
-
client.end();
|
|
92
|
+
const p = config.promise((res, rej) => client.end().then(res).catch(rej));
|
|
90
93
|
Events.disconnect(ctx, client);
|
|
91
94
|
client.removeListener(`error`, onError);
|
|
95
|
+
return p;
|
|
92
96
|
}
|
|
93
97
|
});
|
|
94
98
|
Events.connect(ctx, client, 0);
|
package/lib/context.js
CHANGED
|
@@ -59,8 +59,9 @@ class ConnectionContext {
|
|
|
59
59
|
|
|
60
60
|
disconnect(kill) {
|
|
61
61
|
if (this.db) {
|
|
62
|
-
this.db.release(kill);
|
|
62
|
+
const p = this.db.release(kill);
|
|
63
63
|
this.db = null;
|
|
64
|
+
return p;
|
|
64
65
|
}
|
|
65
66
|
}
|
|
66
67
|
|
|
@@ -73,13 +74,13 @@ class ConnectionContext {
|
|
|
73
74
|
|
|
74
75
|
get nextTxCount() {
|
|
75
76
|
let txCurrent = this, txTop = this;
|
|
76
|
-
while(txCurrent.parent) {
|
|
77
|
+
while (txCurrent.parent) {
|
|
77
78
|
txCurrent = txCurrent.parent;
|
|
78
|
-
if(txCurrent.taskCtx && txCurrent.taskCtx.isTX) {
|
|
79
|
+
if (txCurrent.taskCtx && txCurrent.taskCtx.isTX) {
|
|
79
80
|
txTop = txCurrent;
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
|
-
return txTop.txCount
|
|
83
|
+
return txTop.txCount++;
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
|
package/lib/database.js
CHANGED
|
@@ -147,7 +147,7 @@ function Database(cn, dc, config) {
|
|
|
147
147
|
* in the end of the chain by calling `done()` on the connection object.
|
|
148
148
|
*
|
|
149
149
|
* Method `done` takes one optional parameter - boolean `kill` flag, to signal the connection pool that you want it to kill
|
|
150
|
-
* the physical connection. This flag is ignored for direct connections, as they always
|
|
150
|
+
* the physical connection. This flag is ignored for direct connections, as they always close when released.
|
|
151
151
|
*
|
|
152
152
|
* It should not be used just for chaining queries on the same connection, methods {@link Database#task task} and
|
|
153
153
|
* {@link Database#tx tx} (for transactions) are to be used for that. This method is primarily for special cases, like
|
|
@@ -167,6 +167,9 @@ function Database(cn, dc, config) {
|
|
|
167
167
|
* By default, all connections are acquired from the connection pool. But if you set this option, the library will instead
|
|
168
168
|
* create a new {@link external:Client Client} object directly (separately from the pool), and then call its `connect` method.
|
|
169
169
|
*
|
|
170
|
+
* Note that specifically for direct connections, method `done` returns a {@link external:Promise Promise}, because those connections
|
|
171
|
+
* are closed physically, which may take time.
|
|
172
|
+
*
|
|
170
173
|
* **WARNING:**
|
|
171
174
|
*
|
|
172
175
|
* Do not use this option for regular query execution, because it exclusively occupies one physical channel, and it cannot scale.
|
|
@@ -199,7 +202,8 @@ function Database(cn, dc, config) {
|
|
|
199
202
|
* A promise object that represents the connection result:
|
|
200
203
|
* - resolves with the complete {@link Database} protocol, extended with:
|
|
201
204
|
* - property `client` of type {@link external:Client Client} that represents the open connection
|
|
202
|
-
* - method `done` that must be called in the end, in order to release the connection
|
|
205
|
+
* - method `done` that must be called in the end, in order to release the connection (returns a {@link external:Promise Promise}
|
|
206
|
+
* in case of direct connections)
|
|
203
207
|
* - methods `batch`, `page` and `sequence`, same as inside a {@link Task}
|
|
204
208
|
* - rejects with a connection-related error when it fails to connect.
|
|
205
209
|
*
|
|
@@ -253,7 +257,7 @@ function Database(cn, dc, config) {
|
|
|
253
257
|
if (!ctx.db) {
|
|
254
258
|
throw new Error(npm.text.looseQuery);
|
|
255
259
|
}
|
|
256
|
-
ctx.disconnect(kill);
|
|
260
|
+
return ctx.disconnect(kill);
|
|
257
261
|
},
|
|
258
262
|
batch(values, opt) {
|
|
259
263
|
return config.$npm.spex.batch.call(this, values, opt);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pg-promise",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.10.2",
|
|
4
4
|
"description": "PostgreSQL interface for Node.js",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "typescript/pg-promise.d.ts",
|
|
@@ -41,22 +41,21 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"assert-options": "0.7.0",
|
|
44
|
-
"pg": "8.
|
|
44
|
+
"pg": "8.6.0",
|
|
45
45
|
"pg-minify": "1.6.2",
|
|
46
|
-
"pg-native": "^3.0.0",
|
|
47
46
|
"spex": "3.2.0"
|
|
48
47
|
},
|
|
49
48
|
"devDependencies": {
|
|
50
|
-
"@types/node": "
|
|
49
|
+
"@types/node": "15.0.1",
|
|
51
50
|
"bluebird": "3.7.2",
|
|
52
51
|
"coveralls": "3.1.0",
|
|
53
|
-
"eslint": "7.
|
|
52
|
+
"eslint": "7.25.0",
|
|
54
53
|
"istanbul": "0.4.5",
|
|
55
54
|
"jasmine-node": "3.0.0",
|
|
56
55
|
"jsdoc": "3.6.6",
|
|
57
56
|
"JSONStream": "1.3.5",
|
|
58
|
-
"pg-query-stream": "4.
|
|
57
|
+
"pg-query-stream": "4.1.0",
|
|
59
58
|
"tslint": "6.1.3",
|
|
60
|
-
"typescript": "4.2.
|
|
59
|
+
"typescript": "4.2.4"
|
|
61
60
|
}
|
|
62
61
|
}
|
package/typescript/README.MD
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
## TypeScript for pg-promise
|
|
2
2
|
|
|
3
|
-
Complete TypeScript
|
|
3
|
+
Complete TypeScript declarations for [pg-promise].
|
|
4
4
|
|
|
5
5
|
### Inclusion
|
|
6
6
|
|
|
@@ -14,12 +14,14 @@ import * as pgPromise from 'pg-promise';
|
|
|
14
14
|
const pgp = pgPromise({/* Initialization Options */});
|
|
15
15
|
|
|
16
16
|
const db = pgp('postgres://username:password@host:port/database');
|
|
17
|
+
|
|
18
|
+
const {value} = await db.one('SELECT 123 as value');
|
|
17
19
|
```
|
|
18
20
|
|
|
19
21
|
#### With Extensions
|
|
20
22
|
|
|
21
23
|
The library supports dynamic protocol extensions, via event [extend], which requires
|
|
22
|
-
explicit extension interface to be declared
|
|
24
|
+
explicit extension interface to be declared and parameterized, as shown below.
|
|
23
25
|
|
|
24
26
|
```ts
|
|
25
27
|
import * as pgPromise from 'pg-promise';
|
|
@@ -33,7 +35,7 @@ interface IExtensions {
|
|
|
33
35
|
const options: pgPromise.IInitOptions<IExtensions> = {
|
|
34
36
|
extend(obj) {
|
|
35
37
|
obj.findUser = userId => {
|
|
36
|
-
return obj.one('SELECT * FROM Users WHERE id = $1', userId);
|
|
38
|
+
return obj.one('SELECT * FROM Users WHERE id = $1', [userId]);
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
41
|
};
|
|
@@ -44,11 +46,17 @@ const pgp = pgPromise(options);
|
|
|
44
46
|
// database object:
|
|
45
47
|
const db = pgp('postgres://username:password@host:port/database');
|
|
46
48
|
|
|
47
|
-
//
|
|
48
|
-
db.findUser(123)
|
|
49
|
+
// protocol is extended on each level:
|
|
50
|
+
const user = await db.findUser(123);
|
|
51
|
+
|
|
52
|
+
// ...including inside tasks and transactions:
|
|
53
|
+
await db.task(async t => {
|
|
54
|
+
const user = await t.findUser(123);
|
|
55
|
+
// ...etc
|
|
56
|
+
});
|
|
49
57
|
```
|
|
50
58
|
|
|
51
|
-
For a
|
|
59
|
+
For a comprehensive example, check out [pg-promise-demo].
|
|
52
60
|
|
|
53
61
|
[pg-promise-demo]:https://github.com/vitaly-t/pg-promise-demo
|
|
54
62
|
[extend]:http://vitaly-t.github.io/pg-promise/global.html#event:extend
|
|
@@ -413,7 +413,16 @@ declare namespace pgPromise {
|
|
|
413
413
|
interface IConnected<Ext, C extends pg.IClient> extends IBaseProtocol<Ext>, spexLib.ISpexBase {
|
|
414
414
|
readonly client: C
|
|
415
415
|
|
|
416
|
-
|
|
416
|
+
// Note that for normal connections (working with the pool), method `done` accepts `kill`
|
|
417
|
+
// flag to terminate the connection within the pool, so it can be auto-recreated;
|
|
418
|
+
// And in this case the method returns nothing / void.
|
|
419
|
+
|
|
420
|
+
// But for direct connections (connect({direct: true})), `kill` flag is ignored, because
|
|
421
|
+
// the connection is always closed physically, which may take time, and so in this case
|
|
422
|
+
// the method returns a Promise, to indicate when the connection finished closing.
|
|
423
|
+
done(kill?: boolean): void | XPromise<void>;
|
|
424
|
+
|
|
425
|
+
// Repeated calls are not allowed, and will throw an error.
|
|
417
426
|
}
|
|
418
427
|
|
|
419
428
|
// Event context extension for tasks + transactions;
|
|
@@ -570,7 +579,8 @@ declare namespace pgPromise {
|
|
|
570
579
|
|
|
571
580
|
query?(e: IEventContext<C>): void
|
|
572
581
|
|
|
573
|
-
|
|
582
|
+
// NOTE: result is undefined when data comes from QueryStream, i.e. via method Database.stream
|
|
583
|
+
receive?(data: any[], result: IResultExt | void, e: IEventContext<C>): void
|
|
574
584
|
|
|
575
585
|
task?(e: IEventContext<C>): void
|
|
576
586
|
|