@pi-r/postgres 0.11.3 → 0.12.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 +39 -42
- package/client/pool.js +6 -4
- package/package.json +6 -3
package/client/index.js
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const QueryStream = require("pg-query-stream");
|
|
11
|
-
const Db = require("@e-mc/db");
|
|
12
|
-
const types_1 = require("@e-mc/types");
|
|
13
|
-
const util_1 = require("@e-mc/db/util");
|
|
14
|
-
const DbPool = require("@pi-r/postgres/client/pool");
|
|
2
|
+
|
|
3
|
+
const postgres = require('pg');
|
|
4
|
+
const connectionString = require('pg-connection-string');
|
|
5
|
+
const QueryStream = require('pg-query-stream');
|
|
6
|
+
const Db = require('@e-mc/db');
|
|
7
|
+
const { DB_TYPE, asFunction, errorMessage, isError, isErrorCode, isPlainObject, isString } = require('@e-mc/types');
|
|
8
|
+
const { parseConnectionString, parseServerAuth } = require('@e-mc/db/util');
|
|
9
|
+
const DbPool = require('@pi-r/postgres/client/pool');
|
|
15
10
|
const POOL_STATE = {};
|
|
16
11
|
function fromConnectionString(credential, uri) {
|
|
17
12
|
const { user, password, host, port, database, application_name, ssl, options } = connectionString.parse(uri);
|
|
@@ -33,23 +28,23 @@ function fromConnectionString(credential, uri) {
|
|
|
33
28
|
credential.options ||= options;
|
|
34
29
|
}
|
|
35
30
|
if (ssl) {
|
|
36
|
-
if (!
|
|
31
|
+
if (!isPlainObject(ssl)) {
|
|
37
32
|
credential.ssl ??= true;
|
|
38
33
|
}
|
|
39
|
-
else if (!
|
|
34
|
+
else if (!isPlainObject(credential.ssl)) {
|
|
40
35
|
credential.ssl = ssl;
|
|
41
36
|
}
|
|
42
37
|
}
|
|
43
38
|
return credential;
|
|
44
39
|
}
|
|
45
40
|
function setAuthentication(credential) {
|
|
46
|
-
const auth =
|
|
41
|
+
const auth = parseServerAuth(credential);
|
|
47
42
|
credential.host ||= auth.hostname;
|
|
48
43
|
credential.user ||= auth.username;
|
|
49
|
-
if (
|
|
44
|
+
if (isString(credential.connectionString)) {
|
|
50
45
|
fromConnectionString(credential, credential.connectionString);
|
|
51
46
|
}
|
|
52
|
-
if (
|
|
47
|
+
if (isPlainObject(credential.ssl)) {
|
|
53
48
|
this.readTLSConfig(credential.ssl);
|
|
54
49
|
}
|
|
55
50
|
}
|
|
@@ -73,13 +68,13 @@ function setCredential(item) {
|
|
|
73
68
|
errors.push('user');
|
|
74
69
|
}
|
|
75
70
|
if (errors.length > 0) {
|
|
76
|
-
throw
|
|
71
|
+
throw errorMessage("postgres", "Invalid credentials", errors.join(', '));
|
|
77
72
|
}
|
|
78
73
|
}
|
|
79
74
|
if (credential.host?.endsWith('.rds.amazonaws.com')) {
|
|
80
75
|
try {
|
|
81
76
|
const ssl = credential.ssl ||= {};
|
|
82
|
-
if (
|
|
77
|
+
if (isPlainObject(ssl) && !ssl.ca) {
|
|
83
78
|
const bundle = require('aws-ssl-profiles');
|
|
84
79
|
ssl.ca = bundle.ca;
|
|
85
80
|
}
|
|
@@ -95,8 +90,8 @@ function setCredential(item) {
|
|
|
95
90
|
return;
|
|
96
91
|
}
|
|
97
92
|
let username, password, pool;
|
|
98
|
-
if (
|
|
99
|
-
username = credential.user || credential.connectionString &&
|
|
93
|
+
if (isString(usePool)) {
|
|
94
|
+
username = credential.user || credential.connectionString && parseConnectionString(credential.connectionString)?.username;
|
|
100
95
|
if (username) {
|
|
101
96
|
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
102
97
|
if (pool) {
|
|
@@ -144,7 +139,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
144
139
|
return [];
|
|
145
140
|
}
|
|
146
141
|
let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
147
|
-
if (
|
|
142
|
+
if (isPlainObject(options)) {
|
|
148
143
|
({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
149
144
|
}
|
|
150
145
|
else {
|
|
@@ -187,18 +182,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
187
182
|
}
|
|
188
183
|
catch (err) {
|
|
189
184
|
let close;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
case '28P01':
|
|
193
|
-
close = 1;
|
|
194
|
-
break;
|
|
195
|
-
default:
|
|
196
|
-
close = pool.closeable;
|
|
197
|
-
break;
|
|
185
|
+
if (!isErrorCode(err, '28000', '28P01')) {
|
|
186
|
+
close = pool.closeable;
|
|
198
187
|
}
|
|
199
|
-
if (close) {
|
|
188
|
+
if (close !== false) {
|
|
200
189
|
await pool.detach(true);
|
|
201
|
-
if (close
|
|
190
|
+
if (!close) {
|
|
202
191
|
throw err;
|
|
203
192
|
}
|
|
204
193
|
}
|
|
@@ -206,7 +195,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
206
195
|
}
|
|
207
196
|
}
|
|
208
197
|
if (!client) {
|
|
209
|
-
clients.push(client = new postgres.Client(
|
|
198
|
+
clients.push(client = new postgres.Client(postgresCredential || credential));
|
|
210
199
|
await client.connect();
|
|
211
200
|
}
|
|
212
201
|
if (connectOnce) {
|
|
@@ -225,7 +214,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
225
214
|
const item = batch[i];
|
|
226
215
|
const { source, query, params, ignoreCache } = item;
|
|
227
216
|
let credential = postgresCredential || onceCredential, error;
|
|
228
|
-
if (!credential && !
|
|
217
|
+
if (!credential && !isPlainObject(credential = item.credential) && (error = errorMessage(source, "Invalid credentials")) || !query && (error = errorMessage(source, "Missing database query"))) {
|
|
229
218
|
if (this.handleFail(error, item, { errorQuery })) {
|
|
230
219
|
if (!parallel) {
|
|
231
220
|
tasks.length = 0;
|
|
@@ -242,11 +231,11 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
242
231
|
continue;
|
|
243
232
|
}
|
|
244
233
|
item.transactionState = 1;
|
|
245
|
-
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres", 'options', credential) &&
|
|
234
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres", 'options', credential) && asFunction(item.streamRow) : item.streamRow;
|
|
246
235
|
const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
|
|
247
236
|
let queryString = '';
|
|
248
237
|
if (caching && ignoreCache !== true) {
|
|
249
|
-
queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
|
|
238
|
+
queryString = Db.asString(query, true) + '_' + Db.asString(params, true) + (streamRow && item.options && isString(query) ? Db.asString(item.options, true) : '');
|
|
250
239
|
if (ignoreCache !== 1) {
|
|
251
240
|
const result = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue);
|
|
252
241
|
if (result) {
|
|
@@ -282,10 +271,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
282
271
|
postgresClient = undefined;
|
|
283
272
|
}
|
|
284
273
|
commandType = this.commandType.SELECT;
|
|
285
|
-
if (streamRow &&
|
|
274
|
+
if (streamRow && isString(query)) {
|
|
286
275
|
const rows = [];
|
|
287
276
|
const processRow = typeof streamRow === 'function' ? streamRow : null;
|
|
288
|
-
const stream = client.query(new QueryStream(query, params));
|
|
277
|
+
const stream = client.query(new QueryStream(query, params, item.options));
|
|
289
278
|
stream
|
|
290
279
|
.on('data', row => {
|
|
291
280
|
if (processRow) {
|
|
@@ -293,7 +282,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
293
282
|
if (err === false) {
|
|
294
283
|
return;
|
|
295
284
|
}
|
|
296
|
-
if (err
|
|
285
|
+
if (isError(err)) {
|
|
297
286
|
error = err;
|
|
298
287
|
return;
|
|
299
288
|
}
|
|
@@ -356,5 +345,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
356
345
|
async function checkTimeout(value, limit = 0) {
|
|
357
346
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
358
347
|
}
|
|
359
|
-
|
|
360
|
-
|
|
348
|
+
const DB_SOURCE_CLIENT = true;
|
|
349
|
+
const DB_SOURCE_TYPE = DB_TYPE.SQL;
|
|
350
|
+
|
|
351
|
+
exports.DB_SOURCE_CLIENT = DB_SOURCE_CLIENT;
|
|
352
|
+
exports.DB_SOURCE_TYPE = DB_SOURCE_TYPE;
|
|
353
|
+
exports.checkTimeout = checkTimeout;
|
|
354
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
355
|
+
exports.executeQuery = executeQuery;
|
|
356
|
+
exports.setAuthentication = setAuthentication;
|
|
357
|
+
exports.setCredential = setCredential;
|
package/client/pool.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
const { isArray, isObject, isString } = require('@e-mc/types');
|
|
3
4
|
const DbPool = require('@e-mc/db/pool');
|
|
4
5
|
const POOL_ACTIVE = new WeakSet();
|
|
5
6
|
class PostgresPool extends DbPool {
|
|
@@ -16,18 +17,18 @@ class PostgresPool extends DbPool {
|
|
|
16
17
|
}
|
|
17
18
|
const ssl = credential.ssl;
|
|
18
19
|
let result = super.sanitize(credential);
|
|
19
|
-
if (
|
|
20
|
+
if (isObject(ssl)) {
|
|
20
21
|
if (result === credential) {
|
|
21
22
|
result = { ...credential };
|
|
22
23
|
}
|
|
23
24
|
const cert = ssl.cert;
|
|
24
|
-
if (
|
|
25
|
+
if (isString(cert)) {
|
|
25
26
|
result.ssl = { cert };
|
|
26
27
|
}
|
|
27
28
|
else if (Buffer.isBuffer(cert)) {
|
|
28
29
|
result.ssl = { cert: cert.toString('utf8') };
|
|
29
30
|
}
|
|
30
|
-
else if (
|
|
31
|
+
else if (isArray(cert)) {
|
|
31
32
|
result.ssl = { cert: cert.reduce((a, b) => a + (Buffer.isBuffer(b) ? b.toString('utf8') : b), '') };
|
|
32
33
|
}
|
|
33
34
|
else {
|
|
@@ -52,4 +53,5 @@ class PostgresPool extends DbPool {
|
|
|
52
53
|
return this.client.ended;
|
|
53
54
|
}
|
|
54
55
|
}
|
|
56
|
+
|
|
55
57
|
module.exports = PostgresPool;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "PostgreSQL client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -20,10 +20,13 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/db": "^0.
|
|
24
|
-
"@e-mc/types": "^0.
|
|
23
|
+
"@e-mc/db": "^0.14.0",
|
|
24
|
+
"@e-mc/types": "^0.14.0",
|
|
25
25
|
"pg": "^8.20.0",
|
|
26
26
|
"pg-connection-string": "^2.12.0",
|
|
27
27
|
"pg-query-stream": "^4.14.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@types/pg": "^8"
|
|
28
31
|
}
|
|
29
32
|
}
|