simple-oracledb
Advanced tools
Comparing version 1.1.49 to 1.1.50
| Date | Version | Description | | ||
| ----------- | ------- | ----------- | | ||
| 2016-12-28 | v1.1.50 | Added pool.parallelQuery which enable parallel queries using multiple connections | | ||
| 2016-12-20 | v1.1.49 | Maintenance | | ||
@@ -4,0 +5,0 @@ | 2016-11-15 | v1.1.41 | Added connection.executeFile to read SQL statement from file and execute it | |
@@ -24,2 +24,3 @@ 'use strict'; | ||
'Pool+run': 'Pool+run', | ||
'Pool+parallelQuery': 'Pool+parallelQuery', | ||
'Pool+terminate': { | ||
@@ -26,0 +27,0 @@ tag: 'Pool+terminate', |
110
lib/pool.js
@@ -10,2 +10,3 @@ 'use strict'; | ||
var promiseHelper = require('./promise-helper'); | ||
var constants = require('./constants'); | ||
@@ -15,2 +16,11 @@ var poolIDCounter = 0; | ||
/** | ||
* Holds query invocation definitions. | ||
* | ||
* @typedef {Object} QuerySpec | ||
* @param {string} sql - The SQL to execute | ||
* @param {object} [bindParams] - Optional bind parameters | ||
* @param {object} [options] - Optional query options | ||
*/ | ||
/** | ||
* This events is triggered when a connection is created via pool. | ||
@@ -269,3 +279,3 @@ * | ||
} else { | ||
throw new Error('Illegal input provided.'); | ||
callback(new Error('Illegal input provided.')); | ||
} | ||
@@ -284,2 +294,100 @@ }; | ||
/** | ||
* This function invokes the requested queries in parallel (limiting it based on the amount of node.js thread pool size).<br> | ||
* In order for the queries to run in parallel, multiple connections will be used so use this with caution. | ||
* | ||
* @function | ||
* @memberof! Pool | ||
* @public | ||
* @param {QuerySpec[]} querySpec - Array of query spec objects | ||
* @param {object} [options] - Optional runtime options | ||
* @param {number} [options.limit] - The max connections to be used in parallel (if not provided, it will be calcaulated based on the current node.js thread pool size) | ||
* @param {AsyncCallback} [callback] - Invoked with an error or an array of query results | ||
* @returns {Promise} In case of no callback provided in input, this function will return a promise | ||
* @example | ||
* ```js | ||
* pool.parallelQuery([ | ||
* { | ||
* sql: 'SELECT department_id, department_name FROM departments WHERE manager_id = :id', | ||
* bindParams: [100], | ||
* options: { | ||
* //any options here | ||
* } | ||
* }, | ||
* { | ||
* sql: 'SELECT * FROM employees WHERE manager_id = :id', | ||
* bindParams: { | ||
* id: 100 | ||
* } | ||
* } | ||
* ], function onQueriesDone(error, results) { | ||
* //do something with the result/error | ||
* var query1Results = results[0]; | ||
* var query2Results = results[1]; | ||
* }); | ||
* | ||
* //another example but with promise support | ||
* pool.parallelQuery([ | ||
* { | ||
* sql: 'SELECT department_id, department_name FROM departments WHERE manager_id = :id', | ||
* bindParams: [100], | ||
* options: { | ||
* //any options here | ||
* } | ||
* }, | ||
* { | ||
* sql: 'SELECT * FROM employees WHERE manager_id = :id', | ||
* bindParams: { | ||
* id: 100 | ||
* } | ||
* } | ||
* ]).then(function onQueriesDone(results) { | ||
* //do something with the result | ||
* var query1Results = results[0]; | ||
* var query2Results = results[1]; | ||
* }); | ||
* ``` | ||
*/ | ||
Pool.prototype.parallelQuery = function (querySpec, options, callback) { | ||
var self = this; | ||
if ((!callback) && (typeof options === 'function')) { | ||
callback = options; | ||
options = null; | ||
} | ||
if ((!querySpec) || (!querySpec.length)) { | ||
callback(new Error('Query spec not provided.')); | ||
} else { | ||
//get limit | ||
var limit = constants.parallelLimit; | ||
if (options && options.limit) { | ||
limit = options.limit; | ||
} | ||
var createQuery = function (spec) { | ||
return function run(asyncCallback) { | ||
self.run(function query(connection, queryCallback) { | ||
connection.query(spec.sql, spec.bindParams, spec.options, queryCallback); | ||
}, asyncCallback); | ||
}; | ||
}; | ||
var functions = []; | ||
var index; | ||
for (index = 0; index < querySpec.length; index++) { | ||
functions.push(createQuery(querySpec[index])); | ||
} | ||
asyncLib.parallelLimit(functions, limit, callback); | ||
} | ||
}; | ||
//jscs:enable jsDoc | ||
/*eslint-enable valid-jsdoc*/ | ||
//add promise support | ||
Pool.prototype.parallelQuery = promiseHelper.promisify(Pool.prototype.parallelQuery); | ||
/*eslint-disable valid-jsdoc*/ | ||
//jscs:disable jsDoc | ||
/** | ||
* This function modifies the existing pool.terminate function by enabling the input | ||
@@ -286,0 +394,0 @@ * callback to be an optional parameter.<br> |
{ | ||
"name": "simple-oracledb", | ||
"version": "1.1.49", | ||
"version": "1.1.50", | ||
"description": "Extend capabilities of oracledb with simplified API for quicker development.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -24,2 +24,3 @@ # {"gitdown": "gitinfo", "name": "name"} | ||
* [run](#Pool+run) | ||
* [parallelQuery](#Pool+parallelQuery) | ||
* [terminate](#Pool+terminate) | ||
@@ -209,2 +210,4 @@ * [close](#Pool+terminate) | ||
<a name="Pool+parallelQuery"></a> | ||
<a name="Pool+terminate"></a> | ||
@@ -211,0 +214,0 @@ ### 'pool.terminate([callback]) ⇒ [Promise]' |
@@ -24,2 +24,3 @@ # simple-oracledb | ||
* [run](#Pool+run) | ||
* [parallelQuery](#Pool+parallelQuery) | ||
* [terminate](#Pool+terminate) | ||
@@ -333,2 +334,53 @@ * [close](#Pool+terminate) | ||
<a name="Pool+parallelQuery"></a> | ||
<!-- markdownlint-disable MD009 MD031 MD036 --> | ||
### 'pool.parallelQuery(querySpec, [options], [callback]) ⇒ [Promise]' | ||
This function invokes the requested queries in parallel (limiting it based on the amount of node.js thread pool size).<br> | ||
In order for the queries to run in parallel, multiple connections will be used so use this with caution. | ||
**Example** | ||
```js | ||
pool.parallelQuery([ | ||
{ | ||
sql: 'SELECT department_id, department_name FROM departments WHERE manager_id = :id', | ||
bindParams: [100], | ||
options: { | ||
//any options here | ||
} | ||
}, | ||
{ | ||
sql: 'SELECT * FROM employees WHERE manager_id = :id', | ||
bindParams: { | ||
id: 100 | ||
} | ||
} | ||
], function onQueriesDone(error, results) { | ||
//do something with the result/error | ||
var query1Results = results[0]; | ||
var query2Results = results[1]; | ||
}); | ||
//another example but with promise support | ||
pool.parallelQuery([ | ||
{ | ||
sql: 'SELECT department_id, department_name FROM departments WHERE manager_id = :id', | ||
bindParams: [100], | ||
options: { | ||
//any options here | ||
} | ||
}, | ||
{ | ||
sql: 'SELECT * FROM employees WHERE manager_id = :id', | ||
bindParams: { | ||
id: 100 | ||
} | ||
} | ||
]).then(function onQueriesDone(results) { | ||
//do something with the result | ||
var query1Results = results[0]; | ||
var query2Results = results[1]; | ||
}); | ||
``` | ||
<!-- markdownlint-enable MD009 MD031 MD036 --> | ||
<a name="Pool+terminate"></a> | ||
@@ -1037,2 +1089,3 @@ ### 'pool.terminate([callback]) ⇒ [Promise]' | ||
| ----------- | ------- | ----------- | | ||
| 2016-12-28 | v1.1.50 | Added pool.parallelQuery which enable parallel queries using multiple connections | | ||
| 2016-12-20 | v1.1.49 | Maintenance | | ||
@@ -1039,0 +1092,0 @@ | 2016-11-15 | v1.1.41 | Added connection.executeFile to read SQL statement from file and execute it | |
@@ -30,2 +30,6 @@ 'use strict'; | ||
TestConnection.prototype.rollback = function (callback) { | ||
callback(); | ||
}; | ||
TestConnection.prototype.release = function () { | ||
@@ -36,2 +40,6 @@ var callback = arguments[arguments.length - 1]; | ||
TestPool.prototype.modifyTestConnection = function (connection) { | ||
return connection; | ||
}; | ||
TestPool.prototype.getConnection = function (callback) { | ||
@@ -47,2 +55,4 @@ if (this.throwError) { | ||
connection = this.modifyTestConnection(connection); | ||
callback(null, connection); | ||
@@ -49,0 +59,0 @@ } |
@@ -504,25 +504,34 @@ 'use strict'; | ||
it('missing action with options', function () { | ||
it('missing action with options', function (done) { | ||
var pool = {}; | ||
Pool.extend(pool); | ||
var errorFound = false; | ||
pool.run(null, {}, function (error) { | ||
assert.isDefined(error); | ||
try { | ||
pool.run(null, {}, noop); | ||
} catch (error) { | ||
errorFound = true; | ||
} | ||
done(); | ||
}); | ||
}); | ||
assert.isTrue(errorFound); | ||
it('action not a function with callback', function (done) { | ||
var pool = {}; | ||
Pool.extend(pool); | ||
pool.run('test', {}, function (error) { | ||
assert.isDefined(error); | ||
done(); | ||
}); | ||
}); | ||
it('action not a function', function () { | ||
it('action not a function without callback', function () { | ||
var pool = {}; | ||
Pool.extend(pool); | ||
delete global.Promise; | ||
var errorFound = false; | ||
try { | ||
pool.run('test', {}, noop); | ||
pool.run('test', {}); | ||
} catch (error) { | ||
@@ -532,2 +541,4 @@ errorFound = true; | ||
global.Promise = PromiseLib; | ||
assert.isTrue(errorFound); | ||
@@ -991,2 +1002,211 @@ }); | ||
describe('parallelQuery', function () { | ||
it('callback not provided', function () { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
delete global.Promise; | ||
var errorFound = false; | ||
try { | ||
testPool.parallelQuery([ | ||
{ | ||
sql: 'test' | ||
} | ||
]); | ||
} catch (error) { | ||
errorFound = true; | ||
} | ||
global.Promise = PromiseLib; | ||
assert.isTrue(errorFound); | ||
}); | ||
it('callback not provided, with options', function () { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
delete global.Promise; | ||
var errorFound = false; | ||
try { | ||
testPool.parallelQuery([ | ||
{ | ||
sql: 'test' | ||
} | ||
], {}); | ||
} catch (error) { | ||
errorFound = true; | ||
} | ||
global.Promise = PromiseLib; | ||
assert.isTrue(errorFound); | ||
}); | ||
it('query spec not provided', function (done) { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
testPool.parallelQuery(undefined, function (error) { | ||
assert.isDefined(error); | ||
done(); | ||
}); | ||
}); | ||
it('query spec not provided, with options', function (done) { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
testPool.parallelQuery(undefined, {}, function (error) { | ||
assert.isDefined(error); | ||
done(); | ||
}); | ||
}); | ||
it('empty query spec', function (done) { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
testPool.parallelQuery([], function (error) { | ||
assert.isDefined(error); | ||
done(); | ||
}); | ||
}); | ||
it('multiple queries, no options', function (done) { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
testPool.poolAttributes.runValidationSQL = false; | ||
testPool.modifyTestConnection = function (connection) { | ||
connection.query = function (sql, bindParams, options, callback) { | ||
if (sql === 'test1') { | ||
assert.deepEqual(bindParams, [1, 2, 3]); | ||
assert.deepEqual(options, { | ||
test: '1' | ||
}); | ||
callback(null, 'good1'); | ||
} else if (sql === 'test2') { | ||
assert.deepEqual(bindParams, ['a', 'b', 'c']); | ||
assert.deepEqual(options, { | ||
test: '2' | ||
}); | ||
callback(null, 'good2'); | ||
} else { | ||
callback(new Error('invalid sql')); | ||
} | ||
}; | ||
return connection; | ||
}; | ||
testPool.parallelQuery([ | ||
{ | ||
sql: 'test1', | ||
options: { | ||
test: '1' | ||
}, | ||
bindParams: [1, 2, 3] | ||
}, | ||
{ | ||
sql: 'test2', | ||
options: { | ||
test: '2' | ||
}, | ||
bindParams: ['a', 'b', 'c'] | ||
} | ||
], function (error, results) { | ||
assert.isNull(error); | ||
assert.deepEqual(results, [ | ||
'good1', | ||
'good2' | ||
]); | ||
done(); | ||
}); | ||
}); | ||
it('multiple queries, limit option provided', function (done) { | ||
var testPool = oracledb.createPool(); | ||
testPool.extendConnection = true; | ||
Pool.extend(testPool); | ||
testPool.poolAttributes.runValidationSQL = false; | ||
testPool.modifyTestConnection = function (connection) { | ||
connection.query = function (sql, bindParams, options, callback) { | ||
if (sql === 'test1') { | ||
assert.deepEqual(bindParams, [1, 2, 3]); | ||
assert.deepEqual(options, { | ||
test: '1' | ||
}); | ||
callback(null, 'good1'); | ||
} else if (sql === 'test2') { | ||
assert.deepEqual(bindParams, ['a', 'b', 'c']); | ||
assert.deepEqual(options, { | ||
test: '2' | ||
}); | ||
callback(null, 'good2'); | ||
} else { | ||
callback(new Error('invalid sql')); | ||
} | ||
}; | ||
return connection; | ||
}; | ||
testPool.parallelQuery([ | ||
{ | ||
sql: 'test1', | ||
options: { | ||
test: '1' | ||
}, | ||
bindParams: [1, 2, 3] | ||
}, | ||
{ | ||
sql: 'test2', | ||
options: { | ||
test: '2' | ||
}, | ||
bindParams: ['a', 'b', 'c'] | ||
} | ||
], { | ||
limit: 7 | ||
}, function (error, results) { | ||
assert.isNull(error); | ||
assert.deepEqual(results, [ | ||
'good1', | ||
'good2' | ||
]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('terminate', function () { | ||
@@ -993,0 +1213,0 @@ it('callback provided', function (done) { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
808896
16807
1159