@pi-r/postgres 0.6.7 → 0.7.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/README.md +3 -6
- package/client/index.js +27 -22
- package/package.json +3 -3
- package/types/index.d.ts +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
### @pi-r/postgres
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
https://e-mc.readthedocs.io/en/latest/db/postgres.html
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
- [squared](https://squared.readthedocs.io)
|
|
7
|
-
|
|
8
|
-
## LICENSE
|
|
5
|
+
### LICENSE
|
|
9
6
|
|
|
10
7
|
MIT
|
package/client/index.js
CHANGED
|
@@ -30,7 +30,7 @@ function removePoolProperties(credential) {
|
|
|
30
30
|
for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
|
|
31
31
|
const attr = POOL_UNUSED[i];
|
|
32
32
|
if (attr in credential) {
|
|
33
|
-
result
|
|
33
|
+
result ||= { ...credential };
|
|
34
34
|
if (attr === 'ssl' && (0, types_1.isObject)(result[attr])) {
|
|
35
35
|
result[attr] = true;
|
|
36
36
|
}
|
|
@@ -46,20 +46,19 @@ function removePoolProperties(credential) {
|
|
|
46
46
|
return credential;
|
|
47
47
|
}
|
|
48
48
|
function fromConnectionString(credential, uri) {
|
|
49
|
-
var _a;
|
|
50
49
|
const { user, password, host, port, database, application_name, ssl } = connectionString.parse(uri);
|
|
51
50
|
if (!credential.user) {
|
|
52
51
|
credential.user = user;
|
|
53
52
|
credential.password = password;
|
|
54
53
|
}
|
|
55
|
-
credential.host
|
|
54
|
+
credential.host ||= host;
|
|
56
55
|
if (port) {
|
|
57
|
-
credential.port
|
|
56
|
+
credential.port ||= parseInt(port);
|
|
58
57
|
}
|
|
59
|
-
credential.database
|
|
60
|
-
credential.application_name
|
|
58
|
+
credential.database ||= database;
|
|
59
|
+
credential.application_name ||= application_name;
|
|
61
60
|
if (ssl) {
|
|
62
|
-
|
|
61
|
+
credential.ssl ??= (0, types_1.isPlainObject)(ssl) ? ssl : true;
|
|
63
62
|
}
|
|
64
63
|
return credential;
|
|
65
64
|
}
|
|
@@ -74,8 +73,8 @@ function getPoolKey(credential) {
|
|
|
74
73
|
}
|
|
75
74
|
function setAuthentication(credential) {
|
|
76
75
|
const auth = (0, util_1.parseServerAuth)(credential);
|
|
77
|
-
credential.host
|
|
78
|
-
credential.user
|
|
76
|
+
credential.host ||= auth.hostname;
|
|
77
|
+
credential.user ||= auth.username;
|
|
79
78
|
if ((0, types_1.isString)(credential.connectionString)) {
|
|
80
79
|
fromConnectionString(credential, credential.connectionString);
|
|
81
80
|
}
|
|
@@ -85,7 +84,6 @@ function setAuthentication(credential) {
|
|
|
85
84
|
}
|
|
86
85
|
exports.setAuthentication = setAuthentication;
|
|
87
86
|
function setCredential(item) {
|
|
88
|
-
var _a, _b, _c, _d, _e;
|
|
89
87
|
let credential = this.getCredential(item);
|
|
90
88
|
if (credential) {
|
|
91
89
|
setAuthentication.call(this, credential);
|
|
@@ -117,7 +115,7 @@ function setCredential(item) {
|
|
|
117
115
|
}
|
|
118
116
|
let username, password, pool;
|
|
119
117
|
if ((0, types_1.isString)(usePool)) {
|
|
120
|
-
if (username = credential.user || credential.connectionString && (
|
|
118
|
+
if (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username) {
|
|
121
119
|
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
122
120
|
if (pool) {
|
|
123
121
|
pool.add(item, password);
|
|
@@ -133,16 +131,16 @@ function setCredential(item) {
|
|
|
133
131
|
if (config) {
|
|
134
132
|
const { min, max, idle, timeout } = config;
|
|
135
133
|
if (min >= 0) {
|
|
136
|
-
|
|
134
|
+
credential.min ??= min;
|
|
137
135
|
}
|
|
138
136
|
if (max > 0) {
|
|
139
|
-
|
|
137
|
+
credential.max ??= max;
|
|
140
138
|
}
|
|
141
139
|
if (idle > 0) {
|
|
142
|
-
|
|
140
|
+
credential.idleTimeoutMillis ??= idle;
|
|
143
141
|
}
|
|
144
142
|
if (timeout > 0) {
|
|
145
|
-
|
|
143
|
+
credential.connectionTimeoutMillis ??= timeout;
|
|
146
144
|
}
|
|
147
145
|
}
|
|
148
146
|
const poolKey = getPoolKey(credential);
|
|
@@ -158,7 +156,6 @@ async function executeQuery(item, options) {
|
|
|
158
156
|
}
|
|
159
157
|
exports.executeQuery = executeQuery;
|
|
160
158
|
async function executeBatchQuery(batch, options = '', outResult) {
|
|
161
|
-
var _a;
|
|
162
159
|
const length = batch.length;
|
|
163
160
|
if (length === 0) {
|
|
164
161
|
return [];
|
|
@@ -181,7 +178,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
181
178
|
parallel = !batch.some(item => item.parallel === false);
|
|
182
179
|
}
|
|
183
180
|
if (!parallel) {
|
|
184
|
-
outResult
|
|
181
|
+
outResult ||= new Array(length);
|
|
185
182
|
}
|
|
186
183
|
const caching = this.hasCache("postgres", sessionKey);
|
|
187
184
|
const tasks = new Array(length);
|
|
@@ -240,7 +237,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
240
237
|
item.transactionState &= ~64;
|
|
241
238
|
return client;
|
|
242
239
|
};
|
|
243
|
-
if (
|
|
240
|
+
if (this.host?.username) {
|
|
244
241
|
this.applyCommand(...batch);
|
|
245
242
|
}
|
|
246
243
|
for (let i = 0; i < length; ++i) {
|
|
@@ -264,7 +261,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
264
261
|
continue;
|
|
265
262
|
}
|
|
266
263
|
item.transactionState = 1;
|
|
267
|
-
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres", 'options',
|
|
264
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres", 'options', credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
|
|
268
265
|
const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
|
|
269
266
|
let queryString = '';
|
|
270
267
|
if (caching && ignoreCache !== true) {
|
|
@@ -322,7 +319,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
322
319
|
}
|
|
323
320
|
rows.push(row);
|
|
324
321
|
})
|
|
325
|
-
.on('error', err => error
|
|
322
|
+
.on('error', err => error ||= err)
|
|
326
323
|
.on('close', () => {
|
|
327
324
|
if (error && this.handleFail(error, item, { errorQuery, commandType })) {
|
|
328
325
|
reject(error);
|
|
@@ -361,8 +358,16 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
361
358
|
}
|
|
362
359
|
return this.processRows(batch, tasks, {
|
|
363
360
|
disconnect: () => {
|
|
364
|
-
clients.forEach(item =>
|
|
365
|
-
|
|
361
|
+
clients.forEach(item => {
|
|
362
|
+
item.end(err => {
|
|
363
|
+
if (err) {
|
|
364
|
+
this.writeFail(["Unable to close connnection", "postgres"], err, { type: 65536, fatal: false });
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
pools.forEach(item => {
|
|
369
|
+
item.release();
|
|
370
|
+
});
|
|
366
371
|
},
|
|
367
372
|
parallel
|
|
368
373
|
}, outResult);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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,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.
|
|
24
|
+
"@e-mc/db": "^0.9.2",
|
|
25
|
+
"@e-mc/types": "^0.9.2",
|
|
26
26
|
"pg": "^8.11.5",
|
|
27
27
|
"pg-connection-string": "^2.6.4",
|
|
28
28
|
"pg-query-stream": "^4.5.5"
|
package/types/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
import type { PoolConfig, QueryArrayConfig } from 'pg';
|
|
7
7
|
|
|
8
|
-
export interface PostgresDataSource<T = unknown[]> extends DbDataSource<string | QueryArrayConfig, unknown, unknown,
|
|
8
|
+
export interface PostgresDataSource<T = unknown[]> extends DbDataSource<string | QueryArrayConfig, unknown, unknown, PostgresCredential, string>, ExecuteAction<T> {
|
|
9
9
|
source: "postgres";
|
|
10
10
|
}
|
|
11
11
|
|