simple-oracledb
Advanced tools
Comparing version 1.1.55 to 1.1.56
| Date | Version | Description | | ||
| ----------- | ------- | ----------- | | ||
| 2017-01-14 | v1.1.56 | pool.run actions now can return a promise instead of using a callback | | ||
| 2017-01-13 | v1.1.55 | Maintenance | | ||
@@ -4,0 +5,0 @@ | 2016-12-28 | v1.1.50 | Added pool.parallelQuery which enables parallel queries using multiple connections | |
@@ -188,3 +188,4 @@ 'use strict'; | ||
* The pool will ensure the connection is released properly and only afterwards will call the provided callback with the action error/result.<br> | ||
* This function basically will remove the need of caller code to get and release a connection and focus on the actual database operation logic. | ||
* This function basically will remove the need of caller code to get and release a connection and focus on the actual database operation logic.<br> | ||
* For extended promise support, the action provided can return a promise instead of calling the provided callback (see examples). | ||
* | ||
@@ -230,3 +231,3 @@ * @function | ||
* pool.run(function (connection, callback) { | ||
* //run some query and the output will be available in the 'run' callback | ||
* //run some query and the output will be available in the 'run' promise 'then' | ||
* connection.query('SELECT department_id, department_name FROM departments WHERE manager_id < :id', [110], callback); | ||
@@ -236,2 +237,10 @@ * }).then(function onActionDone(result) { | ||
* }); | ||
* | ||
* //extended promise support (action is returning a promise instead of using the callback) | ||
* pool.run(function (connection) { | ||
* //run some query and the output will be available in the 'run' promise 'then' | ||
* return connection.query('SELECT department_id, department_name FROM departments WHERE manager_id < :id', [110]); //no need for a callback, instead return a promise | ||
* }).then(function onActionDone(result) { | ||
* //do something with the result | ||
* }); | ||
* ``` | ||
@@ -261,3 +270,3 @@ */ | ||
try { | ||
action(connection, function onActionDone(actionAsyncError, result) { | ||
var onActionDone = function (actionAsyncError, result) { | ||
connection.release(releaseOptions, function onConnectionRelease(releaseError) { | ||
@@ -272,3 +281,13 @@ if (actionAsyncError) { | ||
}); | ||
}); | ||
}; | ||
onActionDone = singleAsyncCallback(onActionDone); | ||
var promise = action(connection, onActionDone); | ||
if (promiseHelper.isPromise(promise)) { | ||
promise.then(function onActionResult(result) { | ||
onActionDone(null, result); | ||
}).catch(onActionDone); | ||
} | ||
} catch (actionSyncError) { | ||
@@ -275,0 +294,0 @@ connection.release(releaseOptions, function onConnectionRelease() { |
@@ -30,2 +30,20 @@ 'use strict'; | ||
/** | ||
* Returns true if the provided object supports the basic promise capabilities. | ||
* | ||
* @function | ||
* @memberof! PromiseHelper | ||
* @private | ||
* @param {Object} promise - The promise object to validate | ||
* @returns {Boolean} True if the provided object supports the basic promise capabilities | ||
*/ | ||
PromiseHelper.prototype.isPromise = function (promise) { | ||
var valid = false; | ||
if (promise && promise.then && promise.catch && (typeof promise.then === 'function') && (typeof promise.catch === 'function')) { | ||
valid = true; | ||
} | ||
return valid; | ||
}; | ||
/** | ||
* Calls the provided function with a callback. | ||
@@ -32,0 +50,0 @@ * |
{ | ||
"name": "simple-oracledb", | ||
"version": "1.1.55", | ||
"version": "1.1.56", | ||
"description": "Extend capabilities of oracledb with simplified API for quicker development.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -294,3 +294,4 @@ # simple-oracledb | ||
The pool will ensure the connection is released properly and only afterwards will call the provided callback with the action error/result.<br> | ||
This function basically will remove the need of caller code to get and release a connection and focus on the actual database operation logic. | ||
This function basically will remove the need of caller code to get and release a connection and focus on the actual database operation logic.<br> | ||
For extended promise support, the action provided can return a promise instead of calling the provided callback (see examples). | ||
@@ -326,3 +327,3 @@ **Example** | ||
pool.run(function (connection, callback) { | ||
//run some query and the output will be available in the 'run' callback | ||
//run some query and the output will be available in the 'run' promise 'then' | ||
connection.query('SELECT department_id, department_name FROM departments WHERE manager_id < :id', [110], callback); | ||
@@ -332,2 +333,10 @@ }).then(function onActionDone(result) { | ||
}); | ||
//extended promise support (action is returning a promise instead of using the callback) | ||
pool.run(function (connection) { | ||
//run some query and the output will be available in the 'run' promise 'then' | ||
return connection.query('SELECT department_id, department_name FROM departments WHERE manager_id < :id', [110]); //no need for a callback, instead return a promise | ||
}).then(function onActionDone(result) { | ||
//do something with the result | ||
}); | ||
``` | ||
@@ -1090,2 +1099,3 @@ <!-- markdownlint-enable MD009 MD031 MD036 --> | ||
| ----------- | ------- | ----------- | | ||
| 2017-01-14 | v1.1.56 | pool.run actions now can return a promise instead of using a callback | | ||
| 2017-01-13 | v1.1.55 | Maintenance | | ||
@@ -1092,0 +1102,0 @@ | 2016-12-28 | v1.1.50 | Added pool.parallelQuery which enables parallel queries using multiple connections | |
@@ -10,2 +10,3 @@ 'use strict'; | ||
var Pool = require('../../lib/pool'); | ||
var Connection = require('../../lib/connection'); | ||
var SimpleOracleDB = require('../..'); | ||
@@ -999,2 +1000,106 @@ var extensions = require('../../lib/extensions'); | ||
}); | ||
it('actions are a promise chain', function (done) { | ||
var releaseCalled = false; | ||
var pool = {}; | ||
Pool.extend(pool); | ||
pool.getConnection = function (cb) { | ||
var connection = { | ||
execute: function (sql) { | ||
var callback = arguments[arguments.length - 1]; | ||
setTimeout(function () { | ||
if (sql === 'lastSQL') { | ||
return callback(null, { | ||
metaData: [ | ||
{ | ||
name: 'COL1' | ||
}, | ||
{ | ||
name: 'COL2' | ||
}, | ||
{ | ||
name: 'COL3' | ||
}, | ||
{ | ||
name: 'COL4' | ||
} | ||
], | ||
rows: [ | ||
{ | ||
COL1: 1, | ||
COL2: 'test', | ||
COL3: 50, | ||
COL4: undefined | ||
}, | ||
{ | ||
COL1: 'a', | ||
COL2: 123456, | ||
COL3: undefined, | ||
COL4: undefined | ||
} | ||
] | ||
}); | ||
} | ||
callback(null, { | ||
metaData: [], | ||
rows: [] | ||
}); | ||
}, 5); | ||
} | ||
}; | ||
Connection.extend(connection); | ||
connection.release = function (options, callback) { | ||
releaseCalled = true; | ||
setTimeout(callback, 5); | ||
}; | ||
cb(null, connection); | ||
}; | ||
global.Promise = PromiseLib; | ||
var promise = pool.run(function (connection) { | ||
assert.isDefined(connection); | ||
global.Promise = PromiseLib; | ||
return connection.query('firstSQL').then(function () { | ||
global.Promise = PromiseLib; | ||
return connection.query('lastSQL'); | ||
}); | ||
}); | ||
assert.isDefined(promise); | ||
assert.isFunction(promise.then); | ||
assert.isFunction(promise.catch); | ||
promise.then(function (result) { | ||
assert.isTrue(releaseCalled); | ||
assert.deepEqual([ | ||
{ | ||
COL1: 1, | ||
COL2: 'test', | ||
COL3: 50, | ||
COL4: undefined | ||
}, | ||
{ | ||
COL1: 'a', | ||
COL2: 123456, | ||
COL3: undefined, | ||
COL4: undefined | ||
} | ||
], result); | ||
done(); | ||
}).catch(function () { | ||
assert.fail(); | ||
}); | ||
}); | ||
}); | ||
@@ -1001,0 +1106,0 @@ |
@@ -11,2 +11,57 @@ 'use strict'; | ||
describe('PromiseHelper Tests', function () { | ||
describe('isPromise', function () { | ||
it('undefined', function () { | ||
var output = promiseHelper.isPromise(); | ||
assert.isFalse(output); | ||
}); | ||
it('null', function () { | ||
var output = promiseHelper.isPromise(null); | ||
assert.isFalse(output); | ||
}); | ||
it('not an object', function () { | ||
var output = promiseHelper.isPromise(1); | ||
assert.isFalse(output); | ||
}); | ||
it('missing then', function () { | ||
var output = promiseHelper.isPromise({ | ||
catch: promiseHelper.noop | ||
}); | ||
assert.isFalse(output); | ||
}); | ||
it('missing catch', function () { | ||
var output = promiseHelper.isPromise({ | ||
then: promiseHelper.noop | ||
}); | ||
assert.isFalse(output); | ||
}); | ||
it('then not a function', function () { | ||
var output = promiseHelper.isPromise({ | ||
then: true, | ||
catch: promiseHelper.noop | ||
}); | ||
assert.isFalse(output); | ||
}); | ||
it('catch not a function', function () { | ||
var output = promiseHelper.isPromise({ | ||
then: promiseHelper.noop, | ||
catch: true | ||
}); | ||
assert.isFalse(output); | ||
}); | ||
it('valid', function () { | ||
var output = promiseHelper.isPromise({ | ||
then: promiseHelper.noop, | ||
catch: promiseHelper.noop | ||
}); | ||
assert.isTrue(output); | ||
}); | ||
}); | ||
describe('runPromise', function () { | ||
@@ -13,0 +68,0 @@ it('Promise not supported', function () { |
@@ -11,3 +11,3 @@ 'use strict'; | ||
describe('RowsReader Tests', function () { | ||
describe('getFlattenRowsCount tests', function () { | ||
describe('getFlattenRowsCount', function () { | ||
it('no flattenStackEveryRows value', function () { | ||
@@ -70,3 +70,3 @@ var count = RowsReader.getFlattenRowsCount(['test'], {}); | ||
describe('read tests', function () { | ||
describe('read', function () { | ||
it('empty', function (done) { | ||
@@ -574,3 +574,3 @@ RowsReader.read([], [], function (error, jsRows) { | ||
describe('readJSON tests', function () { | ||
describe('readJSON', function () { | ||
it('undefined', function () { | ||
@@ -577,0 +577,0 @@ var json = RowsReader.readJSON(); |
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
817232
16973
1170