@pi-r/postgres 0.6.0 → 0.6.1
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 +64 -8
- package/package.json +6 -4
- package/types/index.d.ts +1 -0
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,31 @@ function removePoolProperties(credential) {
|
|
|
43
45
|
}
|
|
44
46
|
return credential;
|
|
45
47
|
}
|
|
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
|
+
}
|
|
46
65
|
const getPoolKey = (credential) => (credential.host || '') + '_' + (credential.port || '5432') + (credential.user || '') + '_' + (credential.password || '') + '_' + (credential.database || '') + '_' + (credential.connectionString || '');
|
|
47
66
|
function setAuthentication(credential) {
|
|
48
67
|
const auth = (0, util_1.parseServerAuth)(credential);
|
|
49
68
|
credential.host || (credential.host = auth.hostname);
|
|
50
69
|
credential.user || (credential.user = auth.username);
|
|
70
|
+
if ((0, types_1.isString)(credential.connectionString)) {
|
|
71
|
+
fromConnectionString(credential, credential.connectionString);
|
|
72
|
+
}
|
|
51
73
|
if ((0, types_1.isPlainObject)(credential.ssl)) {
|
|
52
74
|
this.readTLSConfig(credential.ssl);
|
|
53
75
|
}
|
|
@@ -59,8 +81,10 @@ function setCredential(item) {
|
|
|
59
81
|
setAuthentication.call(this, credential);
|
|
60
82
|
}
|
|
61
83
|
else {
|
|
62
|
-
credential = {
|
|
63
|
-
item.
|
|
84
|
+
credential = {};
|
|
85
|
+
if ((0, types_1.isString)(item.uri)) {
|
|
86
|
+
item.credential = fromConnectionString(credential, item.uri);
|
|
87
|
+
}
|
|
64
88
|
}
|
|
65
89
|
if (!credential.connectionString) {
|
|
66
90
|
const errors = [];
|
|
@@ -221,10 +245,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
221
245
|
continue;
|
|
222
246
|
}
|
|
223
247
|
item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
|
|
224
|
-
const
|
|
225
|
-
const cacheValue =
|
|
248
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres" /* STRINGS.MODULE_NAME */, 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
|
|
249
|
+
const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
|
|
226
250
|
let queryString = '';
|
|
227
|
-
if (caching && ignoreCache !== true
|
|
251
|
+
if (caching && ignoreCache !== true) {
|
|
228
252
|
queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
|
|
229
253
|
if (ignoreCache !== 1) {
|
|
230
254
|
const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
|
|
@@ -261,9 +285,41 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
261
285
|
postgresClient = undefined;
|
|
262
286
|
}
|
|
263
287
|
commandType = this.commandType.SELECT;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
288
|
+
if (streamRow && (0, types_1.isString)(query)) {
|
|
289
|
+
const rows = [];
|
|
290
|
+
const processRow = typeof streamRow === 'function' && streamRow;
|
|
291
|
+
const stream = client.query(new QueryStream(query, params));
|
|
292
|
+
stream
|
|
293
|
+
.on('data', row => {
|
|
294
|
+
if (processRow) {
|
|
295
|
+
const err = processRow(row);
|
|
296
|
+
if (err === false) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (err instanceof Error) {
|
|
300
|
+
error = err;
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
rows.push(row);
|
|
305
|
+
})
|
|
306
|
+
.on('error', err => error || (error = err))
|
|
307
|
+
.on('close', () => {
|
|
308
|
+
if (error && this.handleFail(error, item, { errorQuery, commandType })) {
|
|
309
|
+
reject(error);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
|
|
313
|
+
resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows);
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
.on('end', () => stream.destroy());
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
const { rows, command } = await client.query(query, params);
|
|
320
|
+
this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
|
|
321
|
+
resolve(rows.length || command === 'SELECT' ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
|
|
322
|
+
}
|
|
267
323
|
}
|
|
268
324
|
catch (err) {
|
|
269
325
|
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.1",
|
|
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.1",
|
|
25
|
+
"@e-mc/types": "^0.8.1",
|
|
26
|
+
"pg": "^8.11.3",
|
|
27
|
+
"pg-connection-string": "^2.6.2",
|
|
28
|
+
"pg-query-stream": "^4.5.3"
|
|
27
29
|
}
|
|
28
30
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
|
2
2
|
|
|
3
3
|
import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
4
|
|
|
5
|
+
// @ts-ignore
|
|
5
6
|
import type { PoolConfig, QueryArrayConfig } from 'pg';
|
|
6
7
|
|
|
7
8
|
export interface PostgresDataSource<T = unknown[]> extends DbDataSource<string | QueryArrayConfig, unknown, unknown, string | PostgresCredential, string>, ExecuteAction<T> {
|