@pi-r/oracle 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.
package/client/index.d.ts CHANGED
@@ -1,7 +1,13 @@
1
- import type { OracleDataSource } from '@e-mc/types/lib/db';
2
-
1
+ import type { IModule } from '@e-mc/types/lib';
2
+ import type { LogType } from '@e-mc/types/lib/logger';
3
3
  import type { IDbSourceClient } from '@e-mc/db/types';
4
4
 
5
- declare const Oracle: IDbSourceClient<OracleDataSource>;
5
+ import type { OracleDataSource } from '../types';
6
+
7
+ import type { InitialiseOptions } from 'oracledb';
8
+
9
+ declare const Oracle: IDbSourceClient<OracleDataSource> & {
10
+ initOracleClient(this: IModule, credential: InitialiseOptions, type?: LogType): void;
11
+ };
6
12
 
7
13
  export = Oracle;
package/client/index.js CHANGED
@@ -1,21 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
3
+ exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = exports.initOracleClient = void 0;
4
4
  const oracledb = require("oracledb");
5
5
  const util_1 = require("@e-mc/db/util");
6
6
  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 OraclePool extends DbPool {
10
- getConnection() {
10
+ async getConnection() {
11
11
  return this.client.getConnection();
12
12
  }
13
- close() {
13
+ async close() {
14
14
  return this.client.close();
15
15
  }
16
16
  isEmpty() {
17
- const { connectionsInUse, currentQueueLength } = this.client.getStatistics();
18
- return this.closed || connectionsInUse === 0 && currentQueueLength === 0;
17
+ if (this.closed) {
18
+ return true;
19
+ }
20
+ try {
21
+ const data = this.client.getStatistics();
22
+ if (data) {
23
+ const { connectionsInUse, currentQueueLength } = data;
24
+ return connectionsInUse === 0 && currentQueueLength === 0;
25
+ }
26
+ return true;
27
+ }
28
+ catch {
29
+ return false;
30
+ }
19
31
  }
20
32
  get closeable() {
21
33
  return super.closeable || this.client.status === 6001 /* POOL_STATUS.DRAINING */;
@@ -24,6 +36,7 @@ class OraclePool extends DbPool {
24
36
  return this.client.status === 6002 /* POOL_STATUS.CLOSED */;
25
37
  }
26
38
  }
39
+ const DRIVER_MODE = process.env.NODE_ORACLEDB_DRIVER_MODE;
27
40
  const POOL_STATE = {};
28
41
  const POOL_UNUSED = ['poolMax', 'poolMin', 'poolIncrement', 'poolTimeout', 'poolPingInterval', 'poolMaxPerShard', 'connectTimeout', 'queueMax', 'queueRequests', 'queueTimeout'];
29
42
  function removePoolProperties(credential) {
@@ -44,9 +57,47 @@ function removePoolProperties(credential) {
44
57
  }
45
58
  function getPoolKey(credential) {
46
59
  if (credential.connectString && (credential.user && credential.password || credential.externalAuth)) {
47
- return credential.connectString + '_' + (credential.externalAuth ? 'external' : credential.user + '_' + credential.password) + '_' + (credential.edition || '') + '_' + (credential.tag ? credential.tag + '#' + (credential.matchAny ? '1' : '0') : '') + '_' + (credential.privilege ?? '') + '_' + Db.asString(credential.shardingKey) + '_' + Db.asString(credential.superShardingKey);
60
+ return credential.connectString + '_' + (credential.externalAuth ? 'external' : credential.user + '_' + credential.password) + '_' + (credential.edition || '') + '_' + (credential.tag ? credential.tag + '#' + (credential.matchAny ? '1' : '0') : '') + '_' + (credential.privilege ?? '') + '_' + (credential.configDir || '') + '_' + (credential.walletLocation || '') + '_' + (credential.sourceRoute || '') + '_' + Db.asString(credential.shardingKey, true) + '_' + Db.asString(credential.superShardingKey, true);
61
+ }
62
+ }
63
+ const useLibDir = () => process.platform === 'win32' || process.platform === 'darwin' && process.arch === 'x64';
64
+ if (DRIVER_MODE === 'thick' && oracledb.thin) {
65
+ let libDir;
66
+ if (!useLibDir() || (0, types_1.isString)(libDir = process.env.NODE_ORACLEDB_CLIENT_LIB_DIR || process.env.ORACLE_HOME)) {
67
+ try {
68
+ oracledb.initOracleClient({ libDir, configDir: process.env.NODE_ORACLEDB_CLIENT_CONFIG_DIR, driverName: process.env.NODE_ORACLEDB_CLIENT_DRIVER_NAME, errorUrl: process.env.NODE_ORACLEDB_CLIENT_ERROR_URL });
69
+ }
70
+ catch (err) {
71
+ Db.writeFail(["Unable to configure client" /* ERR_DB.CLIENT */, 'oracledb'], err);
72
+ }
48
73
  }
49
74
  }
75
+ function initOracleClient(credential, type) {
76
+ if (credential.libDir && oracledb.thin) {
77
+ if (DRIVER_MODE === 'thick') {
78
+ try {
79
+ oracledb.initOracleClient({ libDir: useLibDir() ? credential.libDir : undefined, driverName: credential.driverName, errorUrl: credential.errorUrl });
80
+ }
81
+ catch (err) {
82
+ if (err instanceof Error) {
83
+ switch (err.code) {
84
+ case 'NJS-090':
85
+ this.addLog(this.statusType.WARN, err, 'oracledb', credential.libDir);
86
+ break;
87
+ default:
88
+ this.writeFail(["Unable to configure client" /* ERR_DB.CLIENT */, 'oracledb'], err, { type: type ?? 65536 /* LOG_TYPE.DB */, fatal: false });
89
+ break;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ else {
95
+ this.writeFail(["Unable to configure client" /* ERR_DB.CLIENT */, 'oracledb'], (0, types_1.errorMessage)('oracledb', 'Environment variable "NODE_ORACLEDB_DRIVER_MODE" was not set', 'thick'), { type: type ?? 65536 /* LOG_TYPE.DB */, fatal: false });
96
+ }
97
+ delete credential.libDir;
98
+ }
99
+ }
100
+ exports.initOracleClient = initOracleClient;
50
101
  async function setCredential(item) {
51
102
  let credential = this.getCredential(item), hostname, port, username, database;
52
103
  if (credential) {
@@ -99,65 +150,65 @@ async function setCredential(item) {
99
150
  throw (0, types_1.errorMessage)("oracle" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
100
151
  }
101
152
  }
153
+ initOracleClient.call(this, credential);
102
154
  const usePool = item.usePool;
103
- if (usePool) {
104
- const poolAlias = credential.poolAlias;
105
- let password, pool;
106
- if (typeof usePool === 'string' && (username = credential.user || poolAlias)) {
107
- [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
108
- if (pool) {
109
- pool.add(item, password);
110
- return;
111
- }
155
+ if (!usePool) {
156
+ return;
157
+ }
158
+ const poolAlias = credential.poolAlias;
159
+ let password, pool;
160
+ if (typeof usePool === 'string' && (username = credential.user || poolAlias)) {
161
+ [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
162
+ if (pool) {
163
+ pool.add(item, password);
164
+ return;
112
165
  }
113
- if (poolAlias) {
114
- for (const poolKey in POOL_STATE) {
115
- const current = POOL_STATE[poolKey];
116
- if (current.client.poolAlias === poolAlias) {
117
- if (!current.closed) {
118
- current.add(item);
119
- return;
120
- }
121
- current.remove();
122
- break;
166
+ }
167
+ if (poolAlias) {
168
+ for (const poolKey in POOL_STATE) {
169
+ const current = POOL_STATE[poolKey];
170
+ if (current.client.poolAlias === poolAlias) {
171
+ if (!current.closed) {
172
+ current.add(item);
173
+ return;
123
174
  }
175
+ current.remove();
176
+ break;
124
177
  }
125
178
  }
126
- const poolKey = getPoolKey(credential);
127
- if (poolKey) {
128
- if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
129
- const config = this.getPoolConfig("oracle" /* STRINGS.MODULE_NAME */, password);
130
- if (config) {
131
- const { min, max, idle, queue_max, queue_idle, timeout } = config;
132
- if (min >= 0) {
133
- credential.poolMin ?? (credential.poolMin = min);
134
- }
135
- if (max > 0) {
136
- credential.poolMax ?? (credential.poolMax = max);
137
- }
138
- if (idle >= 0) {
139
- credential.poolTimeout ?? (credential.poolTimeout = idle / 1000);
140
- }
141
- if (queue_max >= 0) {
142
- credential.queueMax ?? (credential.queueMax = queue_max);
143
- }
144
- if (queue_idle >= 0) {
145
- credential.queueTimeout ?? (credential.queueTimeout = queue_idle);
146
- }
147
- if (timeout >= 0) {
148
- credential.connectTimeout ?? (credential.connectTimeout = idle / 1000);
149
- }
150
- }
151
- new OraclePool(await oracledb.createPool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
152
- }
153
- else {
154
- pool.add(item);
155
- }
179
+ }
180
+ const poolKey = getPoolKey(credential);
181
+ if (!poolKey) {
182
+ item.usePool = false;
183
+ return;
184
+ }
185
+ if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
186
+ pool.add(item);
187
+ return;
188
+ }
189
+ const config = this.getPoolConfig("oracle" /* STRINGS.MODULE_NAME */, password);
190
+ if (config) {
191
+ const { min, max, idle, queue_max, queue_idle, timeout } = config;
192
+ if (min >= 0) {
193
+ credential.poolMin ?? (credential.poolMin = min);
156
194
  }
157
- else {
158
- item.usePool = false;
195
+ if (max > 0) {
196
+ credential.poolMax ?? (credential.poolMax = max);
197
+ }
198
+ if (idle >= 0) {
199
+ credential.poolTimeout ?? (credential.poolTimeout = idle / 1000);
200
+ }
201
+ if (queue_max >= 0) {
202
+ credential.queueMax ?? (credential.queueMax = queue_max);
203
+ }
204
+ if (queue_idle >= 0) {
205
+ credential.queueTimeout ?? (credential.queueTimeout = queue_idle);
206
+ }
207
+ if (timeout >= 0) {
208
+ credential.connectTimeout ?? (credential.connectTimeout = idle / 1000);
159
209
  }
160
210
  }
211
+ new OraclePool(await oracledb.createPool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
161
212
  }
162
213
  exports.setCredential = setCredential;
163
214
  async function executeQuery(item, options) {
@@ -239,6 +290,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
239
290
  item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
240
291
  return client;
241
292
  };
293
+ if (this.host?.username) {
294
+ this.applyCommand(...batch);
295
+ }
242
296
  for (let i = 0; i < length; ++i) {
243
297
  const item = batch[i];
244
298
  const { source, query, params, ignoreCache, options: clientOptions } = item;
@@ -363,7 +417,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
363
417
  }, outResult);
364
418
  }
365
419
  exports.executeBatchQuery = executeBatchQuery;
366
- function checkTimeout(value, limit = 0) {
420
+ async function checkTimeout(value, limit = 0) {
367
421
  return DbPool.checkTimeout(POOL_STATE, value, limit);
368
422
  }
369
423
  exports.checkTimeout = checkTimeout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/oracle",
3
- "version": "0.5.0",
3
+ "version": "0.6.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.7.0",
25
- "@e-mc/types": "^0.7.0",
26
- "oracledb": "^6.2.0"
24
+ "@e-mc/db": "^0.8.0",
25
+ "@e-mc/types": "^0.8.0",
26
+ "oracledb": "^6.3.0"
27
27
  }
28
28
  }
package/types/index.d.ts CHANGED
@@ -2,10 +2,10 @@ 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
- import type { BindParameters, ConnectionAttributes, ExecuteOptions, PoolAttributes } from 'oracledb';
5
+ import type { BindParameters, ConnectionAttributes, ExecuteOptions, PoolAttributes, InitialiseOptions } from 'oracledb';
6
6
 
7
7
  export interface OracleDataSource extends DbDataSource<string, ExecuteOptions, unknown, string | OracleCredential, string>, ExecuteAction<BindParameters> {
8
8
  source: "oracle";
9
9
  }
10
10
 
11
- export interface OracleCredential extends ServerAuth, ConnectionAttributes, PoolAttributes {}
11
+ export interface OracleCredential extends ServerAuth, ConnectionAttributes, PoolAttributes, InitialiseOptions {}