@pi-r/postgres 0.2.2 → 0.2.9

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 2020-24 Code Geass
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,5 +1,7 @@
1
- ### @pi-r/postgres
1
+ # @pi-r/postgres
2
2
 
3
- ### LICENSE
3
+ PEP 402 - Forever Josee + Nunnally
4
+
5
+ ## LICENSE
4
6
 
5
7
  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
@@ -20,11 +20,27 @@ class PostgresPool extends DbPool {
20
20
  }
21
21
  }
22
22
  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 };
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 options;
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" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
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" /* STRINGS.PACKAGE_NAME */);
73
- const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
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" /* STRINGS.PACKAGE_NAME */);
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" /* STRINGS.MODULE_NAME */, sessionKey);
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 /* DB_TRANSACTION.AUTH */;
149
+ item.transactionState = 64;
134
150
  let client;
135
151
  if (postgresPool) {
136
152
  pools.push(client = await postgresPool.getConnection());
137
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
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 /* DB_TRANSACTION.AUTH */;
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" /* ERR_DB.CREDENTIALS */)) || !query && (error = (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */))) {
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 /* DB_TRANSACTION.ACTIVE */;
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 /* DB_TRANSACTION.COMMIT */ | 128 /* DB_TRANSACTION.CACHE */);
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 /* DB_TRANSACTION.COMMIT */);
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" /* ERR_DB.CONNECTION_CLOSE */, "postgres" /* STRINGS.MODULE_NAME */], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false })));
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.2",
3
+ "version": "0.2.9",
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.3",
25
- "@e-mc/types": "^0.5.3",
26
- "pg": "^8.11.0"
24
+ "@e-mc/db": "^0.5.5",
25
+ "@e-mc/types": "^0.5.5",
26
+ "pg": "^8.11.5"
27
27
  }
28
28
  }
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> {