@pi-r/postgres 0.2.2 → 0.5.0

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.
Files changed (2) hide show
  1. package/client/index.js +27 -9
  2. package/package.json +4 -4
package/client/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
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
+ const postgres = require("pg");
4
5
  const util_1 = require("@e-mc/db/util");
5
6
  const types_1 = require("@e-mc/types");
6
7
  const Db = require("@e-mc/db");
@@ -20,11 +21,27 @@ class PostgresPool extends DbPool {
20
21
  }
21
22
  }
22
23
  const POOL_STATE = {};
23
- function removePoolProperties(options) {
24
- if (!options.uuidKey && ('ssl' in options || 'max' in options || 'min' in options || 'idleTimeoutMillis' in options || 'log' in options || 'allowExitOnIdle' in options || 'maxUses' in options)) {
25
- return { ...options, ssl: (0, types_1.isObject)(options.ssl) ? true : undefined, max: undefined, min: undefined, idleTimeoutMillis: undefined, log: undefined, allowExitOnIdle: undefined, maxUses: undefined };
24
+ const POOL_UNUSED = ['ssl', 'keepAlive', 'statement_timeout', 'query_timeout', 'keepAliveInitialDelayMillis', 'idle_in_transaction_session_timeout', 'connectionTimeoutMillis', 'max', 'min', 'idleTimeoutMillis', 'log', 'allowExitOnIdle', 'maxUses'];
25
+ function removePoolProperties(credential) {
26
+ if (!credential.uuidKey) {
27
+ let result;
28
+ for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
29
+ const attr = POOL_UNUSED[i];
30
+ if (attr in credential) {
31
+ result || (result = { ...credential });
32
+ if (attr === 'ssl' && (0, types_1.isObject)(result[attr])) {
33
+ result[attr] = true;
34
+ }
35
+ else {
36
+ delete result[attr];
37
+ }
38
+ }
39
+ }
40
+ if (result) {
41
+ return result;
42
+ }
26
43
  }
27
- return options;
44
+ return credential;
28
45
  }
29
46
  const getPoolKey = (credential) => (credential.host || '') + '_' + (credential.port || '5432') + (credential.user || '') + '_' + (credential.password || '') + '_' + (credential.database || '') + '_' + (credential.connectionString || '');
30
47
  function setAuthentication(credential) {
@@ -69,10 +86,9 @@ function setCredential(item) {
69
86
  }
70
87
  const poolKey = getPoolKey(credential);
71
88
  if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
72
- const { Pool } = require("pg" /* STRINGS.PACKAGE_NAME */);
73
89
  const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
74
90
  if (config) {
75
- const { min, max, idle } = config;
91
+ const { min, max, idle, timeout } = config;
76
92
  if (min >= 0) {
77
93
  credential.min ?? (credential.min = min);
78
94
  }
@@ -82,8 +98,11 @@ function setCredential(item) {
82
98
  if (idle > 0) {
83
99
  credential.idleTimeoutMillis ?? (credential.idleTimeoutMillis = idle);
84
100
  }
101
+ if (timeout > 0) {
102
+ credential.connectionTimeoutMillis ?? (credential.connectionTimeoutMillis = timeout);
103
+ }
85
104
  }
86
- new PostgresPool(new Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
105
+ new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
87
106
  }
88
107
  else {
89
108
  pool.add(item);
@@ -103,7 +122,6 @@ async function executeBatchQuery(batch, options = '', outResult) {
103
122
  if (length === 0) {
104
123
  return [];
105
124
  }
106
- const db = require("pg" /* STRINGS.PACKAGE_NAME */);
107
125
  let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
108
126
  if ((0, types_1.isPlainObject)(options)) {
109
127
  ({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
@@ -167,7 +185,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
167
185
  }
168
186
  }
169
187
  if (!client) {
170
- clients.push(client = new db.Client((postgresCredential || credential)));
188
+ clients.push(client = new postgres.Client((postgresCredential || credential)));
171
189
  await client.connect();
172
190
  }
173
191
  if (connectOnce) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/postgres",
3
- "version": "0.2.2",
3
+ "version": "0.5.0",
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.5.3",
25
- "@e-mc/types": "^0.5.3",
26
- "pg": "^8.11.0"
24
+ "@e-mc/db": "^0.7.0",
25
+ "@e-mc/types": "^0.7.0",
26
+ "pg": "^8.11.3"
27
27
  }
28
28
  }