@pi-r/postgres 0.2.1 → 0.2.8
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/LICENSE +1 -1
- package/README.md +4 -2
- package/client/index.js +33 -17
- package/package.json +5 -5
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright 2023
|
|
1
|
+
Copyright 2023 Mile Square Park
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/client/index.js
CHANGED
|
@@ -20,11 +20,27 @@ class PostgresPool extends DbPool {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
const POOL_STATE = {};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
const POOL_UNUSED = ['ssl', 'keepAlive', 'statement_timeout', 'query_timeout', 'keepAliveInitialDelayMillis', 'idle_in_transaction_session_timeout', 'connectionTimeoutMillis', 'max', 'min', 'idleTimeoutMillis', 'log', 'allowExitOnIdle', 'maxUses'];
|
|
24
|
+
function removePoolProperties(credential) {
|
|
25
|
+
if (!credential.uuidKey) {
|
|
26
|
+
let result;
|
|
27
|
+
for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
|
|
28
|
+
const attr = POOL_UNUSED[i];
|
|
29
|
+
if (attr in credential) {
|
|
30
|
+
result || (result = { ...credential });
|
|
31
|
+
if (attr === 'ssl' && (0, types_1.isObject)(result[attr])) {
|
|
32
|
+
result[attr] = true;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
delete result[attr];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (result) {
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
26
42
|
}
|
|
27
|
-
return
|
|
43
|
+
return credential;
|
|
28
44
|
}
|
|
29
45
|
const getPoolKey = (credential) => (credential.host || '') + '_' + (credential.port || '5432') + (credential.user || '') + '_' + (credential.password || '') + '_' + (credential.database || '') + '_' + (credential.connectionString || '');
|
|
30
46
|
function setAuthentication(credential) {
|
|
@@ -54,7 +70,7 @@ function setCredential(item) {
|
|
|
54
70
|
errors.push('user');
|
|
55
71
|
}
|
|
56
72
|
if (errors.length) {
|
|
57
|
-
throw (0, types_1.errorMessage)("postgres"
|
|
73
|
+
throw (0, types_1.errorMessage)("postgres", 'Not defined - ' + errors.join(' | '));
|
|
58
74
|
}
|
|
59
75
|
}
|
|
60
76
|
const usePool = item.usePool;
|
|
@@ -69,8 +85,8 @@ function setCredential(item) {
|
|
|
69
85
|
}
|
|
70
86
|
const poolKey = getPoolKey(credential);
|
|
71
87
|
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
72
|
-
const { Pool } = require("pg"
|
|
73
|
-
const config = this.getPoolConfig("postgres"
|
|
88
|
+
const { Pool } = require("pg");
|
|
89
|
+
const config = this.getPoolConfig("postgres", password);
|
|
74
90
|
if (config) {
|
|
75
91
|
const { min, max, idle } = config;
|
|
76
92
|
if (min >= 0) {
|
|
@@ -103,7 +119,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
103
119
|
if (length === 0) {
|
|
104
120
|
return [];
|
|
105
121
|
}
|
|
106
|
-
const db = require("pg"
|
|
122
|
+
const db = require("pg");
|
|
107
123
|
let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
108
124
|
if ((0, types_1.isPlainObject)(options)) {
|
|
109
125
|
({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
@@ -124,17 +140,17 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
124
140
|
if (!parallel) {
|
|
125
141
|
outResult || (outResult = new Array(length));
|
|
126
142
|
}
|
|
127
|
-
const caching = this.hasCache("postgres"
|
|
143
|
+
const caching = this.hasCache("postgres", sessionKey);
|
|
128
144
|
const tasks = new Array(length);
|
|
129
145
|
const clients = [];
|
|
130
146
|
const pools = [];
|
|
131
147
|
let postgresPool, postgresClient, postgresCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
|
|
132
148
|
const getConnection = async (item, credential) => {
|
|
133
|
-
item.transactionState = 64
|
|
149
|
+
item.transactionState = 64;
|
|
134
150
|
let client;
|
|
135
151
|
if (postgresPool) {
|
|
136
152
|
pools.push(client = await postgresPool.getConnection());
|
|
137
|
-
item.transactionState &= ~64
|
|
153
|
+
item.transactionState &= ~64;
|
|
138
154
|
return client;
|
|
139
155
|
}
|
|
140
156
|
const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
|
|
@@ -176,14 +192,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
176
192
|
}
|
|
177
193
|
postgresCredential = credential;
|
|
178
194
|
}
|
|
179
|
-
item.transactionState &= ~64
|
|
195
|
+
item.transactionState &= ~64;
|
|
180
196
|
return client;
|
|
181
197
|
};
|
|
182
198
|
for (let i = 0; i < length; ++i) {
|
|
183
199
|
const item = batch[i];
|
|
184
200
|
const { source, query, params, ignoreCache } = item;
|
|
185
201
|
let credential = postgresCredential || onceCredential, error;
|
|
186
|
-
if (!credential && !(0, types_1.isPlainObject)(credential = item.credential) && (error = (0, types_1.errorMessage)(source, "Invalid credentials"
|
|
202
|
+
if (!credential && !(0, types_1.isPlainObject)(credential = item.credential) && (error = (0, types_1.errorMessage)(source, "Invalid credentials")) || !query && (error = (0, types_1.errorMessage)(source, "Missing database query"))) {
|
|
187
203
|
if (this.handleFail(error, item, { errorQuery })) {
|
|
188
204
|
if (!parallel) {
|
|
189
205
|
tasks.length = 0;
|
|
@@ -199,7 +215,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
199
215
|
}
|
|
200
216
|
continue;
|
|
201
217
|
}
|
|
202
|
-
item.transactionState = 1
|
|
218
|
+
item.transactionState = 1;
|
|
203
219
|
const renewCache = ignoreCache === 0;
|
|
204
220
|
const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
|
|
205
221
|
let queryString = '';
|
|
@@ -214,7 +230,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
214
230
|
else {
|
|
215
231
|
outResult[i] = result;
|
|
216
232
|
}
|
|
217
|
-
this.add(item, 4
|
|
233
|
+
this.add(item, 4 | 128);
|
|
218
234
|
continue;
|
|
219
235
|
}
|
|
220
236
|
if (!ignoreCache && outCacheMiss) {
|
|
@@ -241,7 +257,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
241
257
|
}
|
|
242
258
|
commandType = this.commandType.SELECT;
|
|
243
259
|
const { rows, command } = await client.query(query, params);
|
|
244
|
-
this.add(item, 4
|
|
260
|
+
this.add(item, 4);
|
|
245
261
|
resolve(rows.length || command === 'SELECT' ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
|
|
246
262
|
}
|
|
247
263
|
catch (err) {
|
|
@@ -265,7 +281,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
265
281
|
}
|
|
266
282
|
return this.processRows(batch, tasks, {
|
|
267
283
|
disconnect: () => {
|
|
268
|
-
clients.forEach(item => item.end(err => err && this.writeFail(["Unable to close connnection"
|
|
284
|
+
clients.forEach(item => item.end(err => err && this.writeFail(["Unable to close connnection", "postgres"], err, { type: 65536, fatal: false })));
|
|
269
285
|
pools.forEach(item => item.release());
|
|
270
286
|
},
|
|
271
287
|
parallel
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/postgres",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "PostgreSQL client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/anpham6/pi-r.git",
|
|
12
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
13
13
|
"directory": "src/db/postgres"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
@@ -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.5.
|
|
25
|
-
"@e-mc/types": "^0.5.
|
|
26
|
-
"pg": "^8.
|
|
24
|
+
"@e-mc/db": "^0.5.4",
|
|
25
|
+
"@e-mc/types": "^0.5.4",
|
|
26
|
+
"pg": "^8.11.3"
|
|
27
27
|
}
|
|
28
28
|
}
|