@pi-r/postgres 0.6.0 → 0.6.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 -0
- package/client/index.js +88 -18
- package/package.json +6 -4
package/README.md
CHANGED
package/client/index.js
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = exports.setAuthentication = void 0;
|
|
4
4
|
const postgres = require("pg");
|
|
5
|
+
const connectionString = require("pg-connection-string");
|
|
5
6
|
const util_1 = require("@e-mc/db/util");
|
|
6
7
|
const types_1 = require("@e-mc/types");
|
|
7
8
|
const Db = require("@e-mc/db");
|
|
9
|
+
const QueryStream = require("pg-query-stream");
|
|
8
10
|
const DbPool = require('@e-mc/db/pool');
|
|
9
11
|
class PostgresPool extends DbPool {
|
|
10
12
|
async getConnection() {
|
|
@@ -43,11 +45,39 @@ function removePoolProperties(credential) {
|
|
|
43
45
|
}
|
|
44
46
|
return credential;
|
|
45
47
|
}
|
|
46
|
-
|
|
48
|
+
function fromConnectionString(credential, uri) {
|
|
49
|
+
const { user, password, host, port, database, application_name, ssl } = connectionString.parse(uri);
|
|
50
|
+
if (!credential.user) {
|
|
51
|
+
credential.user = user;
|
|
52
|
+
credential.password = password;
|
|
53
|
+
}
|
|
54
|
+
credential.host || (credential.host = host);
|
|
55
|
+
if (port) {
|
|
56
|
+
credential.port || (credential.port = parseInt(port));
|
|
57
|
+
}
|
|
58
|
+
credential.database || (credential.database = database);
|
|
59
|
+
credential.application_name || (credential.application_name = application_name);
|
|
60
|
+
if (ssl) {
|
|
61
|
+
credential.ssl ?? (credential.ssl = (0, types_1.isPlainObject)(ssl) ? ssl : true);
|
|
62
|
+
}
|
|
63
|
+
return credential;
|
|
64
|
+
}
|
|
65
|
+
function getPoolKey(credential) {
|
|
66
|
+
if (credential.host && credential.user) {
|
|
67
|
+
if ('uuidKey' in credential) {
|
|
68
|
+
credential = { ...credential };
|
|
69
|
+
delete credential.uuidKey;
|
|
70
|
+
}
|
|
71
|
+
return JSON.stringify(credential);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
47
74
|
function setAuthentication(credential) {
|
|
48
75
|
const auth = (0, util_1.parseServerAuth)(credential);
|
|
49
76
|
credential.host || (credential.host = auth.hostname);
|
|
50
77
|
credential.user || (credential.user = auth.username);
|
|
78
|
+
if ((0, types_1.isString)(credential.connectionString)) {
|
|
79
|
+
fromConnectionString(credential, credential.connectionString);
|
|
80
|
+
}
|
|
51
81
|
if ((0, types_1.isPlainObject)(credential.ssl)) {
|
|
52
82
|
this.readTLSConfig(credential.ssl);
|
|
53
83
|
}
|
|
@@ -59,8 +89,10 @@ function setCredential(item) {
|
|
|
59
89
|
setAuthentication.call(this, credential);
|
|
60
90
|
}
|
|
61
91
|
else {
|
|
62
|
-
credential = {
|
|
63
|
-
item.
|
|
92
|
+
credential = {};
|
|
93
|
+
if ((0, types_1.isString)(item.uri)) {
|
|
94
|
+
item.credential = fromConnectionString(credential, item.uri);
|
|
95
|
+
}
|
|
64
96
|
}
|
|
65
97
|
if (!credential.connectionString) {
|
|
66
98
|
const errors = [];
|
|
@@ -82,18 +114,19 @@ function setCredential(item) {
|
|
|
82
114
|
return;
|
|
83
115
|
}
|
|
84
116
|
let username, password, pool;
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
pool
|
|
117
|
+
if ((0, types_1.isString)(usePool)) {
|
|
118
|
+
if (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username) {
|
|
119
|
+
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
120
|
+
if (pool) {
|
|
121
|
+
pool.add(item, password);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!password) {
|
|
126
|
+
item.usePool = '';
|
|
89
127
|
return;
|
|
90
128
|
}
|
|
91
129
|
}
|
|
92
|
-
const poolKey = getPoolKey(credential);
|
|
93
|
-
if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
|
|
94
|
-
pool.add(item);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
130
|
const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
|
|
98
131
|
if (config) {
|
|
99
132
|
const { min, max, idle, timeout } = config;
|
|
@@ -110,6 +143,11 @@ function setCredential(item) {
|
|
|
110
143
|
credential.connectionTimeoutMillis ?? (credential.connectionTimeoutMillis = timeout);
|
|
111
144
|
}
|
|
112
145
|
}
|
|
146
|
+
const poolKey = getPoolKey(credential);
|
|
147
|
+
if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
|
|
148
|
+
pool.add(item);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
113
151
|
new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
114
152
|
}
|
|
115
153
|
exports.setCredential = setCredential;
|
|
@@ -221,10 +259,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
221
259
|
continue;
|
|
222
260
|
}
|
|
223
261
|
item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
|
|
224
|
-
const
|
|
225
|
-
const cacheValue =
|
|
262
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres" /* STRINGS.MODULE_NAME */, 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
|
|
263
|
+
const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
|
|
226
264
|
let queryString = '';
|
|
227
|
-
if (caching && ignoreCache !== true
|
|
265
|
+
if (caching && ignoreCache !== true) {
|
|
228
266
|
queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
|
|
229
267
|
if (ignoreCache !== 1) {
|
|
230
268
|
const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
|
|
@@ -261,9 +299,41 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
261
299
|
postgresClient = undefined;
|
|
262
300
|
}
|
|
263
301
|
commandType = this.commandType.SELECT;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
302
|
+
if (streamRow && (0, types_1.isString)(query)) {
|
|
303
|
+
const rows = [];
|
|
304
|
+
const processRow = typeof streamRow === 'function' && streamRow;
|
|
305
|
+
const stream = client.query(new QueryStream(query, params));
|
|
306
|
+
stream
|
|
307
|
+
.on('data', row => {
|
|
308
|
+
if (processRow) {
|
|
309
|
+
const err = processRow(row);
|
|
310
|
+
if (err === false) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (err instanceof Error) {
|
|
314
|
+
error = err;
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
rows.push(row);
|
|
319
|
+
})
|
|
320
|
+
.on('error', err => error || (error = err))
|
|
321
|
+
.on('close', () => {
|
|
322
|
+
if (error && this.handleFail(error, item, { errorQuery, commandType })) {
|
|
323
|
+
reject(error);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
|
|
327
|
+
resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows);
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
.on('end', () => stream.destroy());
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
const { rows, command } = await client.query(query, params);
|
|
334
|
+
this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
|
|
335
|
+
resolve(rows.length || command === 'SELECT' ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
|
|
336
|
+
}
|
|
267
337
|
}
|
|
268
338
|
catch (err) {
|
|
269
339
|
if (this.handleFail(err, item, { errorQuery, commandType })) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/postgres",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
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,10 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.8.
|
|
25
|
-
"@e-mc/types": "^0.8.
|
|
26
|
-
"pg": "^8.11.3"
|
|
24
|
+
"@e-mc/db": "^0.8.2",
|
|
25
|
+
"@e-mc/types": "^0.8.2",
|
|
26
|
+
"pg": "^8.11.3",
|
|
27
|
+
"pg-connection-string": "^2.6.2",
|
|
28
|
+
"pg-query-stream": "^4.5.3"
|
|
27
29
|
}
|
|
28
30
|
}
|