@pi-r/postgres 0.6.3 → 0.6.5

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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
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
@@ -1,7 +1,10 @@
1
- ### @pi-r/postgres
1
+ # @pi-r/postgres
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/db/postgres.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/db/postgres.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
package/client/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { IDb } from '@e-mc/types/lib';
2
- import type { PostgresCredential, PostgresDataSource } from '@e-mc/types/lib/db';
3
2
 
4
3
  import type { IDbSourceClient } from '@e-mc/db/types';
5
4
 
5
+ import type { PostgresCredential, PostgresDataSource } from '../types';
6
+
6
7
  declare const Postgres: IDbSourceClient<PostgresDataSource> & {
7
8
  setAuthentication(this: IDb, credential: PostgresCredential): void;
8
9
  };
package/client/index.js CHANGED
@@ -24,8 +24,9 @@ class PostgresPool extends DbPool {
24
24
  }
25
25
  const POOL_STATE = {};
26
26
  const POOL_UNUSED = ['ssl', 'keepAlive', 'statement_timeout', 'query_timeout', 'keepAliveInitialDelayMillis', 'idle_in_transaction_session_timeout', 'connectionTimeoutMillis', 'max', 'min', 'idleTimeoutMillis', 'log', 'allowExitOnIdle', 'maxUses'];
27
+ const POOL_ACTIVE = new WeakSet();
27
28
  function removePoolProperties(credential) {
28
- if (!credential.uuidKey) {
29
+ if (!credential.uuidKey && POOL_ACTIVE.has(credential)) {
29
30
  let result;
30
31
  for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
31
32
  const attr = POOL_UNUSED[i];
@@ -103,7 +104,7 @@ function setCredential(item) {
103
104
  errors.push('user');
104
105
  }
105
106
  if (errors.length) {
106
- throw (0, types_1.errorMessage)("postgres" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
107
+ throw (0, types_1.errorMessage)("postgres", 'Not defined - ' + errors.join(' | '));
107
108
  }
108
109
  }
109
110
  const usePool = item.usePool;
@@ -127,7 +128,7 @@ function setCredential(item) {
127
128
  return;
128
129
  }
129
130
  }
130
- const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
131
+ const config = this.getPoolConfig("postgres", password);
131
132
  if (config) {
132
133
  const { min, max, idle, timeout } = config;
133
134
  if (min >= 0) {
@@ -180,17 +181,18 @@ async function executeBatchQuery(batch, options = '', outResult) {
180
181
  if (!parallel) {
181
182
  outResult || (outResult = new Array(length));
182
183
  }
183
- const caching = this.hasCache("postgres" /* STRINGS.MODULE_NAME */, sessionKey);
184
+ const caching = this.hasCache("postgres", sessionKey);
184
185
  const tasks = new Array(length);
185
186
  const clients = [];
186
187
  const pools = [];
187
188
  let postgresPool, postgresClient, postgresCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
188
189
  const getConnection = async (item, credential) => {
189
- item.transactionState = 64 /* DB_TRANSACTION.AUTH */;
190
+ item.transactionState = 64;
190
191
  let client;
191
192
  if (postgresPool) {
192
193
  pools.push(client = await postgresPool.getConnection());
193
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
194
+ item.transactionState &= ~64;
195
+ POOL_ACTIVE.add(credential);
194
196
  return client;
195
197
  }
196
198
  const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
@@ -201,6 +203,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
201
203
  postgresPool = pool;
202
204
  }
203
205
  pool.connected = true;
206
+ POOL_ACTIVE.add(credential);
204
207
  }
205
208
  catch (err) {
206
209
  let close;
@@ -232,7 +235,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
232
235
  }
233
236
  postgresCredential = credential;
234
237
  }
235
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
238
+ item.transactionState &= ~64;
236
239
  return client;
237
240
  };
238
241
  if (this.host?.username) {
@@ -242,7 +245,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
242
245
  const item = batch[i];
243
246
  const { source, query, params, ignoreCache } = item;
244
247
  let credential = postgresCredential || onceCredential, error;
245
- if (!credential && !(0, types_1.isPlainObject)(credential = item.credential) && (error = (0, types_1.errorMessage)(source, "Invalid credentials" /* ERR_DB.CREDENTIALS */)) || !query && (error = (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */))) {
248
+ 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"))) {
246
249
  if (this.handleFail(error, item, { errorQuery })) {
247
250
  if (!parallel) {
248
251
  tasks.length = 0;
@@ -258,8 +261,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
258
261
  }
259
262
  continue;
260
263
  }
261
- item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
262
- const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres" /* STRINGS.MODULE_NAME */, 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
264
+ item.transactionState = 1;
265
+ const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("postgres", 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
263
266
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
264
267
  let queryString = '';
265
268
  if (caching && ignoreCache !== true) {
@@ -273,7 +276,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
273
276
  else {
274
277
  outResult[i] = result;
275
278
  }
276
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */ | 128 /* DB_TRANSACTION.CACHE */);
279
+ this.add(item, 4 | 128);
277
280
  continue;
278
281
  }
279
282
  if (!ignoreCache && outCacheMiss) {
@@ -323,7 +326,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
323
326
  reject(error);
324
327
  }
325
328
  else {
326
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
329
+ this.add(item, 4);
327
330
  resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows);
328
331
  }
329
332
  })
@@ -331,7 +334,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
331
334
  }
332
335
  else {
333
336
  const { rows, command } = await client.query(query, params);
334
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
337
+ this.add(item, 4);
335
338
  resolve(rows.length || command === 'SELECT' ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
336
339
  }
337
340
  }
@@ -356,7 +359,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
356
359
  }
357
360
  return this.processRows(batch, tasks, {
358
361
  disconnect: () => {
359
- clients.forEach(item => item.end(err => err && this.writeFail(["Unable to close connnection" /* ERR_DB.CONNECTION_CLOSE */, "postgres" /* STRINGS.MODULE_NAME */], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false })));
362
+ clients.forEach(item => item.end(err => err && this.writeFail(["Unable to close connnection", "postgres"], err, { type: 65536, fatal: false })));
360
363
  pools.forEach(item => item.release());
361
364
  },
362
365
  parallel
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/postgres",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
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.8.3",
25
- "@e-mc/types": "^0.8.3",
24
+ "@e-mc/db": "^0.8.6",
25
+ "@e-mc/types": "^0.8.6",
26
26
  "pg": "^8.11.3",
27
27
  "pg-connection-string": "^2.6.2",
28
28
  "pg-query-stream": "^4.5.3"
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> {