-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
306 lines (277 loc) · 8.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* istanbul ignore file */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-else-return */
const oracledb = require("oracledb");
oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT;
const POOL_MAX_DEFAULT = 4;
const POOL_MIN_DEFAULT = 0;
const POOL_INCREMENT_DEFAULT = 1;
let promises = {};
let pools = {};
let config = {};
/**
* OracleDB Parameter types
*/
exports.OracleDBTypes = {
STRING: oracledb.STRING,
NUMBER: oracledb.NUMBER,
DATE: oracledb.DATE
};
exports.createPool = async (poolName) => {
try {
const srcCfg = config.DATASOURCES[poolName];
if (srcCfg) {
let poolMax = POOL_MAX_DEFAULT;
let poolMin = POOL_MIN_DEFAULT;
let poolIncrement = POOL_INCREMENT_DEFAULT;
if (process.env.UV_THREADPOOL_SIZE && srcCfg.MAX_POOL && !isNaN(srcCfg.MAX_POOL)) {
// if a max pool is provided, make sure to set poolMin equal to poolMax, as well as
// the poolIncrement to zero, based on oracledb documentation to prevent any
// kind of connection storms.
// For more info, checkout https://oracle.github.io/node-oracledb/doc/api.html#-1531-connection-pool-sizing
poolMax = srcCfg.MAX_POOL;
poolMin = srcCfg.MAX_POOL;
poolIncrement = 0;
}
if (!promises[poolName]) {
promises[poolName] = oracledb.createPool({
poolAlias: poolName,
user: srcCfg.DB_USER,
password: srcCfg.DB_PASSWORD,
connectString: `${srcCfg.DB_HOST}:${srcCfg.DB_PORT}/${srcCfg.DB_DATABASE}`,
poolMax,
poolMin,
poolIncrement
});
}
pools[poolName] = await promises[poolName];
console.debug(`Oracle Adapter: Pool ${poolName} created`);
return true;
} else {
console.error(`Oracle Adapter: Missing configuration for ${poolName}`);
return false;
}
} catch (err) {
console.error("Oracle Adapter: Error while closing connection", err);
return false;
}
};
exports.connect = async (poolName) => {
try {
if (!pools[poolName]) {
await this.createPool(poolName);
}
const connection = await oracledb.getConnection(poolName);
console.debug(`Oracle Adapter: Successfully retrieved a connection from ${poolName} pool`);
return connection;
} catch (err) {
console.error("Oracle Adapter: Error while retrieving a connection", err);
throw new Error(err.message);
}
};
exports.close = async (conn) => {
if (conn) {
try {
await conn.close();
console.debug("Oracle Adapter: DB connection returned to pool");
return true;
} catch (err) {
console.error("Oracle Adapter: Error while closing connection", err);
return false;
}
} else {
return true;
}
};
exports.closePool = async (poolAlias) => {
try {
await oracledb.getPool(poolAlias).close(10);
console.debug(`Oracle Adapter: Pool ${poolAlias} closed`);
return true;
} catch (err) {
console.error("Oracle Adapter: Error while closing connection", err);
return false;
}
};
exports.init = async (cfg) => {
config = cfg;
};
exports.execute = async (srcName, query, params = {}, options = {}) => {
let result;
let conn;
try {
console.debug(query);
if (params) {
console.debug(JSON.stringify(params));
}
const start = process.hrtime();
conn = await this.connect(srcName);
console.debug(
`Oracle Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
// to get the text from columns of type CLOB
oracledb.fetchAsString = [ oracledb.CLOB ];
result = await conn.execute(query, params, options);
console.debug(
`Oracle Adapter: Query executed: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
return result;
} catch (err) {
console.error("Oracle Adapter: Error while executing query", err);
throw new Error(err.message);
} finally {
if (conn) await conn.close();
}
};
/**
*
* @param {*} srcName - Connection name to connect to DB
* @param {*} query - SQL Query to execute. Example:
* INSERT INTO TABLE1 (ID, NAME) VALUES (:id, :name)
* @param {*} binds - Array of objects whose keys match the bind variable names in the SQL statement. For Example:
* [
* {id:1,name: name1},
* {id:2,name: name2}
* ]
* @param {*} options - It is an optional parameter contains following properties:
* 1. autoCommit
* 2. batchErrors - call will stop when first error occurs
* 3. bindDefs - object defines the bind variable types, sizes and directions. Example:
* bindDefs: {
* id: { type: dataSource.OracleDBTypes.NUMBER, maxSize: 5 },
* name: { type: dataSource.OracleDBTypes.STRING, maxSize: 10 }
* }
*/
exports.executeMany = async (srcName, query, binds = [], options = {}) => {
let result;
let conn;
try {
console.debug(query);
if (binds) {
console.debug(JSON.stringify(binds));
}
const start = process.hrtime();
conn = await this.connect(srcName);
console.debug(
`Oracle Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
result = await conn.executeMany(query, binds, options);
console.debug(
`Oracle Adapter: Query executed: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
return result;
} catch (err) {
console.error("Oracle Adapter: Error while executing query", err);
throw new Error(err.message);
} finally {
if (conn) await conn.close();
}
};
exports.executeStoredProc = async (srcName, storeproc, params = {}, options = {}) => {
let result;
let conn;
let rows = [];
try {
console.debug(storeproc);
if (params) {
console.debug(JSON.stringify(params));
}
const start = process.hrtime();
conn = await this.connect(srcName);
console.debug(
`Oracle Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
result = await conn.execute(
storeproc, params
);
console.debug(
`Oracle Adapter: Query executed: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
const resultSet = result.outBinds.response;
let row;
while ((row = await resultSet.getRow())) {
rows.push(row);
}
} catch (err) {
console.error("Oracle Adapter: Error while executing query", err);
throw new Error(err.message);
} finally {
if (conn) await conn.close();
return rows;
}
};
exports.closeAllPools = async () => {
try {
const tempPools = pools;
promises = {};
pools = {};
for (const poolAlias of Object.keys(tempPools)) {
await oracledb.getPool(poolAlias).close(10);
console.debug(`Oracle Adapter: Pool ${poolAlias} closed`);
}
return true;
} catch (err) {
console.error("Oracle Adapter: Error while closing connection", err);
return false;
}
};
/**
* create a new standalone, non-pooled query and executes query using
* the datasource based on the source name provided
*
* @param {string} sourceName - provide the data source name to be used. It should be
* one match one of the datasources set in the express.config file
* @param {string} query - provide the query to be executed
* @param {object} params - provide the bindings used in the query
* @param {object} options - provide oracleDB.ExecuteOptions
*/
exports.executeStandaloneConnection = async (
sourceName,
query,
params = {},
options = {}
) => {
const srcCfg = config.DATASOURCES[sourceName];
if (!srcCfg) {
console.error(`Oracle Adapter: Missing configuration for ${sourceName}`);
return false;
}
const connection = await oracledb.getConnection({
user: srcCfg.DB_USER,
password: srcCfg.DB_PASSWORD,
connectString: `${srcCfg.DB_HOST}:${srcCfg.DB_PORT}/${srcCfg.DB_DATABASE}`
});
const start = process.hrtime();
let result;
console.debug(
`Oracle Adapter: Connection secured: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
try {
result = await connection.execute(query, params, options);
console.debug(
`Oracle Adapter: Query executed: ${process.hrtime(start)[0]}s ${
process.hrtime(start)[1] / 1000000
}ms`
);
} catch (ex) {
console.error(`Oracle Adapter: There was an error at the moment of executing oracle query using a standalone connection: ${JSON.stringify(ex)}`);
} finally {
if (connection) await connection.close();
return result;
}
};