@pi-r/oracle 0.7.2 → 0.8.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.
package/client/index.d.ts CHANGED
@@ -8,6 +8,7 @@ import type { InitialiseOptions } from 'oracledb';
8
8
 
9
9
  declare const Oracle: IDbSourceClient<OracleDataSource> & {
10
10
  initOracleClient(this: IModule, credential: InitialiseOptions, type?: LogType): void;
11
+ getConfigObject(value: Undef<string>): string;
11
12
  };
12
13
 
13
14
  export = Oracle;
package/client/index.js CHANGED
@@ -1,70 +1,19 @@
1
1
  "use strict";
2
- exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = exports.initOracleClient = void 0;
2
+ exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = void 0;
3
+ exports.getConfigObject = getConfigObject;
4
+ exports.initOracleClient = initOracleClient;
5
+ exports.setCredential = setCredential;
6
+ exports.executeQuery = executeQuery;
7
+ exports.executeBatchQuery = executeBatchQuery;
8
+ exports.checkTimeout = checkTimeout;
3
9
  const oracledb = require("oracledb");
10
+ const Db = require("@e-mc/db");
4
11
  const util_1 = require("@e-mc/db/util");
5
12
  const types_1 = require("@e-mc/types");
6
- const Db = require("@e-mc/db");
7
- const DbPool = require('@e-mc/db/pool');
8
- class OraclePool extends DbPool {
9
- async getConnection() {
10
- return this.client.getConnection();
11
- }
12
- async close() {
13
- return this.client.close();
14
- }
15
- isEmpty() {
16
- if (this.closed) {
17
- return true;
18
- }
19
- try {
20
- const data = this.client.getStatistics();
21
- if (data) {
22
- const { connectionsInUse, currentQueueLength } = data;
23
- return connectionsInUse === 0 && currentQueueLength === 0;
24
- }
25
- return true;
26
- }
27
- catch {
28
- return false;
29
- }
30
- }
31
- get closeable() {
32
- return super.closeable || this.client.status === 6001;
33
- }
34
- get closed() {
35
- return this.client.status === 6002;
36
- }
37
- }
38
- const DRIVER_MODE = process.env.NODE_ORACLEDB_DRIVER_MODE;
13
+ const DbPool = require("@pi-r/oracle/client/pool");
39
14
  const POOL_STATE = {};
40
- const POOL_UNUSED = ['connectTimeout', 'transportConnectTimeout', 'expireTime', 'poolMax', 'poolMin', 'poolIncrement', 'poolTimeout', 'poolPingInterval', 'poolMaxPerShard', 'queueMax', 'queueRequests', 'queueTimeout', 'enableStatistics', 'sodaMetaDataCache', 'stmtCacheSize'];
41
- const POOL_ACTIVE = new WeakSet();
42
- function removePoolProperties(credential) {
43
- if (!credential.uuidKey && POOL_ACTIVE.has(credential)) {
44
- let result;
45
- for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
46
- const attr = POOL_UNUSED[i];
47
- if (attr in credential) {
48
- result ||= { ...credential };
49
- delete result[attr];
50
- }
51
- }
52
- if (result) {
53
- return result;
54
- }
55
- }
56
- return credential;
57
- }
58
- function getPoolKey(credential) {
59
- if (credential.connectString && (credential.user && credential.password || credential.externalAuth) || credential.poolAlias) {
60
- if ('uuidKey' in credential) {
61
- credential = { ...credential };
62
- delete credential.uuidKey;
63
- }
64
- return JSON.stringify(credential);
65
- }
66
- }
67
- const useLibDir = () => process.platform === 'win32' || process.platform === 'darwin' && process.arch === 'x64';
15
+ const DRIVER_MODE = process.env.NODE_ORACLEDB_DRIVER_MODE;
16
+ const useLibDir = () => Db.PLATFORM_WIN32 || process.platform === 'darwin' && process.arch === 'x64';
68
17
  if (DRIVER_MODE === 'thick' && oracledb.thin) {
69
18
  let libDir;
70
19
  if (!useLibDir() || (0, types_1.isString)(libDir = process.env.NODE_ORACLEDB_CLIENT_LIB_DIR || process.env.ORACLE_HOME)) {
@@ -76,6 +25,9 @@ if (DRIVER_MODE === 'thick' && oracledb.thin) {
76
25
  }
77
26
  }
78
27
  }
28
+ function getConfigObject(value) {
29
+ return (0, types_1.isString)(value) && /^config-(?:ociobject|azure):\/\//i.test(value) ? value : '';
30
+ }
79
31
  function initOracleClient(credential, type) {
80
32
  if (credential.libDir && oracledb.thin) {
81
33
  if (DRIVER_MODE === 'thick') {
@@ -101,7 +53,6 @@ function initOracleClient(credential, type) {
101
53
  delete credential.libDir;
102
54
  }
103
55
  }
104
- exports.initOracleClient = initOracleClient;
105
56
  async function setCredential(item) {
106
57
  let credential = this.getCredential(item), hostname, port, username, database;
107
58
  if (credential) {
@@ -129,13 +80,19 @@ async function setCredential(item) {
129
80
  if (hostname) {
130
81
  credential.connectString ||= hostname + (port ? ':' + port : '') + (database ? '/' + database : '');
131
82
  }
132
- if (credential.externalAuth) {
83
+ const configObject = getConfigObject(credential.connectString);
84
+ if (credential.externalAuth || configObject) {
133
85
  delete credential.user;
134
86
  delete credential.password;
135
87
  }
136
- if (!credential.poolAlias) {
88
+ if (!credential.poolAlias && !configObject) {
137
89
  const errors = [];
90
+ const env = Db.enabled("process.env.apply");
138
91
  if (!credential.externalAuth) {
92
+ if (env && !credential.user && !credential.password) {
93
+ credential.user = process.env.NODE_ORACLEDB_USER;
94
+ credential.password = process.env.NODE_ORACLEDB_PASSWORD;
95
+ }
139
96
  if (!credential.user) {
140
97
  errors.push('user');
141
98
  }
@@ -143,10 +100,10 @@ async function setCredential(item) {
143
100
  errors.push('password');
144
101
  }
145
102
  }
146
- if (!credential.connectString) {
103
+ if (!credential.connectString && (!env || !(credential.connectString = process.env.NODE_ORACLEDB_CONNECTIONSTRING))) {
147
104
  errors.push('connectString');
148
105
  }
149
- if (errors.length) {
106
+ if (errors.length > 0) {
150
107
  throw (0, types_1.errorMessage)("oracle", 'Not defined - ' + errors.join(' | '));
151
108
  }
152
109
  }
@@ -158,7 +115,7 @@ async function setCredential(item) {
158
115
  const poolAlias = credential.poolAlias;
159
116
  let password, pool;
160
117
  if ((0, types_1.isString)(usePool)) {
161
- if (username = credential.user || poolAlias) {
118
+ if (username = credential.user || poolAlias || configObject) {
162
119
  [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
163
120
  if (pool) {
164
121
  pool.add(item, password);
@@ -205,18 +162,19 @@ async function setCredential(item) {
205
162
  credential.connectTimeout ??= idle / 1000;
206
163
  }
207
164
  }
208
- const poolKey = getPoolKey(credential);
165
+ const poolKey = DbPool.asString(credential);
209
166
  if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
210
167
  pool.add(item);
211
168
  return;
212
169
  }
213
- new OraclePool(await oracledb.createPool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
170
+ const client = await oracledb.createPool(credential);
171
+ new DbPool(client, poolKey, username && password ? { username, password } : configObject ? { username: configObject, password: configObject } : undefined)
172
+ .add(item)
173
+ .parent = POOL_STATE;
214
174
  }
215
- exports.setCredential = setCredential;
216
175
  async function executeQuery(item, options) {
217
176
  return (await executeBatchQuery.call(this, [item], options))[0] || [];
218
177
  }
219
- exports.executeQuery = executeQuery;
220
178
  async function executeBatchQuery(batch, options = '', outResult) {
221
179
  const length = batch.length;
222
180
  if (length === 0) {
@@ -250,20 +208,18 @@ async function executeBatchQuery(batch, options = '', outResult) {
250
208
  item.transactionState = 64;
251
209
  let client;
252
210
  if (oraclePool) {
253
- clients.push(client = await oraclePool.getConnection());
211
+ clients.push(client = await oraclePool.getConnection(credential));
254
212
  item.transactionState &= ~64;
255
- POOL_ACTIVE.add(credential);
256
213
  return client;
257
214
  }
258
- const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
215
+ const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, DbPool.asString(credential), ...connectOnce ? [item, batch[0]] : [item]);
259
216
  if (pool) {
260
217
  try {
261
- client = await pool.getConnection();
218
+ client = await pool.getConnection(credential);
262
219
  if (connectOnce) {
263
220
  oraclePool = pool;
264
221
  }
265
222
  pool.connected = true;
266
- POOL_ACTIVE.add(credential);
267
223
  }
268
224
  catch (err) {
269
225
  let close;
@@ -324,7 +280,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
324
280
  if (caching && ignoreCache !== true && !streamRow) {
325
281
  queryString = Db.asString(query, true) + '_' + Db.asString(params, true) + Db.asString(clientOptions, true);
326
282
  if (ignoreCache !== 1) {
327
- const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
283
+ const result = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue);
328
284
  if (result) {
329
285
  if (parallel) {
330
286
  tasks[i] = Promise.resolve(result);
@@ -361,7 +317,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
361
317
  commandType = this.commandType.SELECT;
362
318
  if (streamRow) {
363
319
  const rows = [];
364
- const processRow = typeof streamRow === 'function' && streamRow;
320
+ const processRow = typeof streamRow === 'function' ? streamRow : null;
365
321
  const stream = client.queryStream(query, params || [], executeOptions);
366
322
  stream
367
323
  .on('data', row => {
@@ -384,7 +340,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
384
340
  }
385
341
  else {
386
342
  this.add(item, 4);
387
- resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows);
343
+ resolve(!error ? this.setQueryResult(source, DbPool.sanitize(credential), queryString, rows, cacheValue) : rows);
388
344
  }
389
345
  })
390
346
  .on('end', () => stream.destroy());
@@ -392,7 +348,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
392
348
  else {
393
349
  const { rows } = await client.execute(query, params || [], executeOptions);
394
350
  this.add(item, 4);
395
- resolve(rows ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
351
+ resolve(rows ? this.setQueryResult(source, DbPool.sanitize(credential), queryString, rows, cacheValue) : null);
396
352
  }
397
353
  }
398
354
  catch (err) {
@@ -415,20 +371,20 @@ async function executeBatchQuery(batch, options = '', outResult) {
415
371
  }
416
372
  }
417
373
  return this.processRows(batch, tasks, {
418
- disconnect: () => clients.forEach(item => {
419
- item.close(err => {
420
- if (err) {
421
- this.writeFail(["Unable to close connnection", "oracle"], err, { type: 65536, fatal: false });
422
- }
423
- });
424
- }),
425
- parallel
374
+ parallel,
375
+ disconnect: () => {
376
+ for (const item of clients) {
377
+ item.close(err => {
378
+ if (err) {
379
+ this.writeFail(["Unable to close connnection", "oracle"], err, { type: 65536, fatal: false });
380
+ }
381
+ });
382
+ }
383
+ }
426
384
  }, outResult);
427
385
  }
428
- exports.executeBatchQuery = executeBatchQuery;
429
386
  async function checkTimeout(value, limit = 0) {
430
387
  return DbPool.checkTimeout(POOL_STATE, value, limit);
431
388
  }
432
- exports.checkTimeout = checkTimeout;
433
389
  exports.DB_SOURCE_CLIENT = true;
434
390
  exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
@@ -0,0 +1,9 @@
1
+ import type { DbPoolConstructor } from '@e-mc/types/lib/db';
2
+
3
+ import type { DbPoolCredential, OracleDataSource } from '../types';
4
+
5
+ import type { Pool } from 'oracledb';
6
+
7
+ declare const OraclePool: DbPoolConstructor<OracleDataSource, Pool, unknown, DbPoolCredential>;
8
+
9
+ export = OraclePool;
package/client/pool.js ADDED
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ const DbPool = require('@e-mc/db/pool');
3
+ const POOL_ACTIVE = new WeakSet();
4
+ class OraclePool extends DbPool {
5
+ static CACHE_UNUSED = ['connectTimeout', 'transportConnectTimeout', 'expireTime', 'poolMax', 'poolMin', 'poolIncrement', 'poolTimeout', 'poolPingInterval', 'poolMaxPerShard', 'queueMax', 'queueRequests', 'queueTimeout', 'enableStatistics', 'sodaMetaDataCache', 'stmtCacheSize'];
6
+ static asString(credential) {
7
+ if (credential.connectString && (credential.user && credential.password || credential.externalAuth) || credential.poolAlias) {
8
+ return JSON.stringify(this.removeUUIDKey(credential));
9
+ }
10
+ return super.asString(credential);
11
+ }
12
+ static sanitize(credential) {
13
+ if (!POOL_ACTIVE.has(credential) || credential.uuidKey) {
14
+ return credential;
15
+ }
16
+ return super.sanitize(credential);
17
+ }
18
+ get closeable() {
19
+ return super.closeable || this.client.status === 6001;
20
+ }
21
+ async getConnection(credential) {
22
+ if (credential) {
23
+ POOL_ACTIVE.add(credential);
24
+ }
25
+ return this.client.getConnection();
26
+ }
27
+ async close() {
28
+ return this.client.close();
29
+ }
30
+ isEmpty() {
31
+ if (this.closed) {
32
+ return true;
33
+ }
34
+ try {
35
+ const data = this.client.getStatistics();
36
+ if (data) {
37
+ const { connectionsInUse, currentQueueLength } = data;
38
+ return connectionsInUse === 0 && currentQueueLength === 0;
39
+ }
40
+ return true;
41
+ }
42
+ catch {
43
+ return false;
44
+ }
45
+ }
46
+ get closed() {
47
+ return this.client.status === 6002;
48
+ }
49
+ }
50
+ module.exports = OraclePool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/oracle",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "description": "Oracle 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.9.4",
25
- "@e-mc/types": "^0.9.4",
26
- "oracledb": "^6.5.1"
24
+ "@e-mc/db": "^0.10.0",
25
+ "@e-mc/types": "^0.10.0",
26
+ "oracledb": "^6.6.0"
27
27
  }
28
28
  }
package/types/index.d.ts CHANGED
@@ -1,12 +1,21 @@
1
1
  import type { DbDataSource } from '@e-mc/types/lib/squared';
2
2
 
3
+ import type { IdentifierAction } from '@e-mc/types/lib/core';
3
4
  import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
4
5
 
5
6
  // @ts-ignore
6
7
  import type { BindParameters, ConnectionAttributes, ExecuteOptions, InitialiseOptions, PoolAttributes } from 'oracledb';
7
8
 
9
+ export const enum POOL_STATUS {
10
+ CLOSED = 6002,
11
+ DRAINING = 6001,
12
+ OPEN = 6000,
13
+ RECONFIGURING = 6003
14
+ }
15
+
8
16
  export interface OracleDataSource extends DbDataSource<string, ExecuteOptions, unknown, OracleCredential, string>, ExecuteAction<BindParameters> {
9
17
  source: "oracle";
10
18
  }
11
19
 
12
- export interface OracleCredential extends ServerAuth, ConnectionAttributes, PoolAttributes, InitialiseOptions {}
20
+ export type OracleCredential = ConnectionAttributes & PoolAttributes & InitialiseOptions & ServerAuth;
21
+ export type DbPoolCredential = PoolAttributes & IdentifierAction;