@pi-r/postgres 0.3.0 → 0.6.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/client/index.js +38 -32
- package/package.json +4 -4
package/client/index.js
CHANGED
|
@@ -7,10 +7,10 @@ const types_1 = require("@e-mc/types");
|
|
|
7
7
|
const Db = require("@e-mc/db");
|
|
8
8
|
const DbPool = require('@e-mc/db/pool');
|
|
9
9
|
class PostgresPool extends DbPool {
|
|
10
|
-
getConnection() {
|
|
10
|
+
async getConnection() {
|
|
11
11
|
return this.client.connect();
|
|
12
12
|
}
|
|
13
|
-
close() {
|
|
13
|
+
async close() {
|
|
14
14
|
return this.client.end();
|
|
15
15
|
}
|
|
16
16
|
isEmpty() {
|
|
@@ -75,39 +75,42 @@ function setCredential(item) {
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
const usePool = item.usePool;
|
|
78
|
-
if (item.usePool
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
82
|
-
if (pool) {
|
|
83
|
-
pool.add(item, password);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
78
|
+
if (!item.usePool || typeof credential.password === 'function') {
|
|
79
|
+
if ('usePool' in item) {
|
|
80
|
+
item.usePool = false;
|
|
86
81
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (max > 0) {
|
|
96
|
-
credential.max ?? (credential.max = max);
|
|
97
|
-
}
|
|
98
|
-
if (idle > 0) {
|
|
99
|
-
credential.idleTimeoutMillis ?? (credential.idleTimeoutMillis = idle);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
pool.add(item);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
let username, password, pool;
|
|
85
|
+
if (typeof usePool === 'string' && (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username)) {
|
|
86
|
+
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
87
|
+
if (pool) {
|
|
88
|
+
pool.add(item, password);
|
|
89
|
+
return;
|
|
106
90
|
}
|
|
107
91
|
}
|
|
108
|
-
|
|
109
|
-
|
|
92
|
+
const poolKey = getPoolKey(credential);
|
|
93
|
+
if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
|
|
94
|
+
pool.add(item);
|
|
95
|
+
return;
|
|
110
96
|
}
|
|
97
|
+
const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
|
|
98
|
+
if (config) {
|
|
99
|
+
const { min, max, idle, timeout } = config;
|
|
100
|
+
if (min >= 0) {
|
|
101
|
+
credential.min ?? (credential.min = min);
|
|
102
|
+
}
|
|
103
|
+
if (max > 0) {
|
|
104
|
+
credential.max ?? (credential.max = max);
|
|
105
|
+
}
|
|
106
|
+
if (idle > 0) {
|
|
107
|
+
credential.idleTimeoutMillis ?? (credential.idleTimeoutMillis = idle);
|
|
108
|
+
}
|
|
109
|
+
if (timeout > 0) {
|
|
110
|
+
credential.connectionTimeoutMillis ?? (credential.connectionTimeoutMillis = timeout);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
111
114
|
}
|
|
112
115
|
exports.setCredential = setCredential;
|
|
113
116
|
async function executeQuery(item, options) {
|
|
@@ -194,6 +197,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
194
197
|
item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
|
|
195
198
|
return client;
|
|
196
199
|
};
|
|
200
|
+
if (this.host?.username) {
|
|
201
|
+
this.applyCommand(...batch);
|
|
202
|
+
}
|
|
197
203
|
for (let i = 0; i < length; ++i) {
|
|
198
204
|
const item = batch[i];
|
|
199
205
|
const { source, query, params, ignoreCache } = item;
|
|
@@ -287,7 +293,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
287
293
|
}, outResult);
|
|
288
294
|
}
|
|
289
295
|
exports.executeBatchQuery = executeBatchQuery;
|
|
290
|
-
function checkTimeout(value, limit = 0) {
|
|
296
|
+
async function checkTimeout(value, limit = 0) {
|
|
291
297
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
292
298
|
}
|
|
293
299
|
exports.checkTimeout = checkTimeout;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "PostgreSQL client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
26
|
-
"pg": "^8.11.
|
|
24
|
+
"@e-mc/db": "^0.8.0",
|
|
25
|
+
"@e-mc/types": "^0.8.0",
|
|
26
|
+
"pg": "^8.11.3"
|
|
27
27
|
}
|
|
28
28
|
}
|