@pi-r/postgres 0.5.0 → 0.6.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 +38 -35
  2. package/package.json +3 -3
package/client/index.js CHANGED
@@ -7,10 +7,10 @@ const types_1 = require("@e-mc/types");
7
7
  const Db = require("@e-mc/db");
8
8
  const DbPool = require('@e-mc/db/pool');
9
9
  class PostgresPool extends DbPool {
10
- getConnection() {
10
+ async getConnection() {
11
11
  return this.client.connect();
12
12
  }
13
- close() {
13
+ async close() {
14
14
  return this.client.end();
15
15
  }
16
16
  isEmpty() {
@@ -75,42 +75,42 @@ function setCredential(item) {
75
75
  }
76
76
  }
77
77
  const usePool = item.usePool;
78
- if (item.usePool && typeof credential.password !== 'function') {
79
- let username, password, pool;
80
- if (typeof usePool === 'string' && (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username)) {
81
- [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
82
- if (pool) {
83
- pool.add(item, password);
84
- return;
85
- }
86
- }
87
- const poolKey = getPoolKey(credential);
88
- if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
89
- const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
90
- if (config) {
91
- const { min, max, idle, timeout } = config;
92
- if (min >= 0) {
93
- credential.min ?? (credential.min = min);
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;
78
+ if (!item.usePool || typeof credential.password === 'function') {
79
+ if ('usePool' in item) {
80
+ item.usePool = false;
106
81
  }
107
- else {
108
- pool.add(item);
82
+ return;
83
+ }
84
+ let username, password, pool;
85
+ if (typeof usePool === 'string' && (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username)) {
86
+ [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
87
+ if (pool) {
88
+ pool.add(item, password);
89
+ return;
109
90
  }
110
91
  }
111
- else if ('usePool' in item) {
112
- item.usePool = false;
92
+ const poolKey = getPoolKey(credential);
93
+ if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
94
+ pool.add(item);
95
+ return;
113
96
  }
97
+ const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
98
+ if (config) {
99
+ const { min, max, idle, timeout } = config;
100
+ if (min >= 0) {
101
+ credential.min ?? (credential.min = min);
102
+ }
103
+ if (max > 0) {
104
+ credential.max ?? (credential.max = max);
105
+ }
106
+ if (idle > 0) {
107
+ credential.idleTimeoutMillis ?? (credential.idleTimeoutMillis = idle);
108
+ }
109
+ if (timeout > 0) {
110
+ credential.connectionTimeoutMillis ?? (credential.connectionTimeoutMillis = timeout);
111
+ }
112
+ }
113
+ new PostgresPool(new postgres.Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
114
114
  }
115
115
  exports.setCredential = setCredential;
116
116
  async function executeQuery(item, options) {
@@ -197,6 +197,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
197
197
  item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
198
198
  return client;
199
199
  };
200
+ if (this.host?.username) {
201
+ this.applyCommand(...batch);
202
+ }
200
203
  for (let i = 0; i < length; ++i) {
201
204
  const item = batch[i];
202
205
  const { source, query, params, ignoreCache } = item;
@@ -290,7 +293,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
290
293
  }, outResult);
291
294
  }
292
295
  exports.executeBatchQuery = executeBatchQuery;
293
- function checkTimeout(value, limit = 0) {
296
+ async function checkTimeout(value, limit = 0) {
294
297
  return DbPool.checkTimeout(POOL_STATE, value, limit);
295
298
  }
296
299
  exports.checkTimeout = checkTimeout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/postgres",
3
- "version": "0.5.0",
3
+ "version": "0.6.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.7.0",
25
- "@e-mc/types": "^0.7.0",
24
+ "@e-mc/db": "^0.8.0",
25
+ "@e-mc/types": "^0.8.0",
26
26
  "pg": "^8.11.3"
27
27
  }
28
28
  }