@pi-r/postgres 0.5.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 +102 -43
- package/package.json +6 -4
- package/types/index.d.ts +1 -0
package/client/index.js
CHANGED
|
@@ -2,15 +2,17 @@
|
|
|
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
|
-
getConnection() {
|
|
12
|
+
async getConnection() {
|
|
11
13
|
return this.client.connect();
|
|
12
14
|
}
|
|
13
|
-
close() {
|
|
15
|
+
async close() {
|
|
14
16
|
return this.client.end();
|
|
15
17
|
}
|
|
16
18
|
isEmpty() {
|
|
@@ -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 = [];
|
|
@@ -75,42 +99,42 @@ function setCredential(item) {
|
|
|
75
99
|
}
|
|
76
100
|
}
|
|
77
101
|
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
|
-
}
|
|
102
|
+
if (!item.usePool || typeof credential.password === 'function') {
|
|
103
|
+
if ('usePool' in item) {
|
|
104
|
+
item.usePool = false;
|
|
86
105
|
}
|
|
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
|
-
if (timeout > 0) {
|
|
102
|
-
credential.connectionTimeoutMillis ?? (credential.connectionTimeoutMillis = timeout);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
pool.add(item);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
let username, password, pool;
|
|
109
|
+
if (typeof usePool === 'string' && (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username)) {
|
|
110
|
+
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
111
|
+
if (pool) {
|
|
112
|
+
pool.add(item, password);
|
|
113
|
+
return;
|
|
109
114
|
}
|
|
110
115
|
}
|
|
111
|
-
|
|
112
|
-
|
|
116
|
+
const poolKey = getPoolKey(credential);
|
|
117
|
+
if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
|
|
118
|
+
pool.add(item);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
|
|
122
|
+
if (config) {
|
|
123
|
+
const { min, max, idle, timeout } = config;
|
|
124
|
+
if (min >= 0) {
|
|
125
|
+
credential.min ?? (credential.min = min);
|
|
126
|
+
}
|
|
127
|
+
if (max > 0) {
|
|
128
|
+
credential.max ?? (credential.max = max);
|
|
129
|
+
}
|
|
130
|
+
if (idle > 0) {
|
|
131
|
+
credential.idleTimeoutMillis ?? (credential.idleTimeoutMillis = idle);
|
|
132
|
+
}
|
|
133
|
+
if (timeout > 0) {
|
|
134
|
+
credential.connectionTimeoutMillis ?? (credential.connectionTimeoutMillis = timeout);
|
|
135
|
+
}
|
|
113
136
|
}
|
|
137
|
+
new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
114
138
|
}
|
|
115
139
|
exports.setCredential = setCredential;
|
|
116
140
|
async function executeQuery(item, options) {
|
|
@@ -197,6 +221,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
197
221
|
item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
|
|
198
222
|
return client;
|
|
199
223
|
};
|
|
224
|
+
if (this.host?.username) {
|
|
225
|
+
this.applyCommand(...batch);
|
|
226
|
+
}
|
|
200
227
|
for (let i = 0; i < length; ++i) {
|
|
201
228
|
const item = batch[i];
|
|
202
229
|
const { source, query, params, ignoreCache } = item;
|
|
@@ -218,10 +245,10 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
218
245
|
continue;
|
|
219
246
|
}
|
|
220
247
|
item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
|
|
221
|
-
const
|
|
222
|
-
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 };
|
|
223
250
|
let queryString = '';
|
|
224
|
-
if (caching && ignoreCache !== true
|
|
251
|
+
if (caching && ignoreCache !== true) {
|
|
225
252
|
queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
|
|
226
253
|
if (ignoreCache !== 1) {
|
|
227
254
|
const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
|
|
@@ -258,9 +285,41 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
258
285
|
postgresClient = undefined;
|
|
259
286
|
}
|
|
260
287
|
commandType = this.commandType.SELECT;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
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
|
+
}
|
|
264
323
|
}
|
|
265
324
|
catch (err) {
|
|
266
325
|
if (this.handleFail(err, item, { errorQuery, commandType })) {
|
|
@@ -290,7 +349,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
290
349
|
}, outResult);
|
|
291
350
|
}
|
|
292
351
|
exports.executeBatchQuery = executeBatchQuery;
|
|
293
|
-
function checkTimeout(value, limit = 0) {
|
|
352
|
+
async function checkTimeout(value, limit = 0) {
|
|
294
353
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
295
354
|
}
|
|
296
355
|
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.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.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
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> {
|