Socket
Socket
Sign inDemoInstall

simple-oracledb

Package Overview
Dependencies
Maintainers
1
Versions
239
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-oracledb - npm Package Compare versions

Comparing version 0.0.9 to 0.0.10

60

docs/api.md

@@ -46,2 +46,3 @@ ## Classes

* [#release([callback])](#Connection+release)
* [#queryJSON(sql, [bindParams], [options], callback)](#Connection+queryJSON)
* [#modifyParams(argumentsArray)](#Connection+modifyParams) ⇒ <code>object</code> ℗

@@ -189,2 +190,36 @@ * [#createCallback(callback, commit, [output])](#Connection+createCallback) ⇒ <code>function</code> ℗

```
<a name="Connection+queryJSON"></a>
### Connection#queryJSON(sql, [bindParams], [options], callback)
This function will invoke the provided SQL SELECT and return a results object with the returned row count and the JSONs.<br>
The json property will hold a single JSON object in case the returned row count is 1, and an array of JSONs in case the row count is higher.<br>
The query expects that only 1 column is fetched and if more are detected in the results, this function will return an error in the callback.<br>
The function arguments used to execute the 'queryJSON' are exactly as defined in the oracledb connection.execute function.
**Access:** public
| Param | Type | Description |
| --- | --- | --- |
| sql | <code>string</code> | The SQL to execute |
| [bindParams] | <code>object</code> | Optional bind parameters |
| [options] | <code>object</code> | Optional execute options |
| callback | <code>[AsyncCallback](#AsyncCallback)</code> | Invoked with an error or the query results object holding the row count and JSONs |
**Example**
```js
connection.queryJSON('SELECT JSON_DATA FROM APP_CONFIG WHERE ID > :id', [110], function onResults(error, results) {
if (error) {
//handle error...
} else if (results.rowCount === 1) { //single JSON is returned
//print the JSON
console.log(results.json);
} else if (results.rowCount > 1) { //multiple JSONs are returned
//print the JSON
results.json.forEach(function printJSON(json) {
console.log(json);
});
} else {
console.log('Did not find any results');
}
});
```
<a name="Connection+modifyParams"></a>

@@ -315,2 +350,3 @@ ### Connection#modifyParams(argumentsArray) ⇒ <code>object</code> ℗

* [#read(columnNames, row, callback)](#RecordReader+read)
* [#readJSON(jsRow, column)](#RecordReader+readJSON) ⇒ <code>object</code>

@@ -357,2 +393,14 @@ <a name="new_RecordReader_new"></a>

<a name="RecordReader+readJSON"></a>
### RecordReader#readJSON(jsRow, column) ⇒ <code>object</code>
Read a JSON record.
**Returns**: <code>object</code> - The JSON object
**Access:** public
| Param | Type | Description |
| --- | --- | --- |
| jsRow | <code>object</code> | The JS object holding the row data. |
| column | <code>string</code> | The column name |
<a name="RecordWriter"></a>

@@ -459,2 +507,3 @@ ## RecordWriter

* [#read(columnNames, rows, callback)](#RowsReader+read)
* [#readJSON(jsRows)](#RowsReader+readJSON) ⇒ <code>object</code>

@@ -477,2 +526,13 @@ <a name="new_RowsReader_new"></a>

<a name="RowsReader+readJSON"></a>
### RowsReader#readJSON(jsRows) ⇒ <code>object</code>
Read a JSON rows.
**Returns**: <code>object</code> - The JSON object
**Access:** public
| Param | Type | Description |
| --- | --- | --- |
| jsRows | <code>Array</code> | The JS objects holding the row data. |
<a name="SimpleOracleDB"></a>

@@ -479,0 +539,0 @@ ## SimpleOracleDB

@@ -235,2 +235,76 @@ 'use strict';

/**
* This function will invoke the provided SQL SELECT and return a results object with the returned row count and the JSONs.<br>
* The json property will hold a single JSON object in case the returned row count is 1, and an array of JSONs in case the row count is higher.<br>
* The query expects that only 1 column is fetched and if more are detected in the results, this function will return an error in the callback.<br>
* The function arguments used to execute the 'queryJSON' are exactly as defined in the oracledb connection.execute function.
*
* @function
* @memberof! Connection
* @public
* @param {string} sql - The SQL to execute
* @param {object} [bindParams] - Optional bind parameters
* @param {object} [options] - Optional execute options
* @param {AsyncCallback} callback - Invoked with an error or the query results object holding the row count and JSONs
* @example
* ```js
* connection.queryJSON('SELECT JSON_DATA FROM APP_CONFIG WHERE ID > :id', [110], function onResults(error, results) {
* if (error) {
* //handle error...
* } else if (results.rowCount === 1) { //single JSON is returned
* //print the JSON
* console.log(results.json);
* } else if (results.rowCount > 1) { //multiple JSONs are returned
* //print the JSON
* results.json.forEach(function printJSON(json) {
* console.log(json);
* });
* } else {
* console.log('Did not find any results');
* }
* });
* ```
*/
Connection.prototype.queryJSON = function () {
var self = this;
var argumentsArray = Array.prototype.slice.call(arguments, 0);
var callback = argumentsArray.pop();
argumentsArray.push(function onExecute(error, jsRows) {
if (error) {
callback(error);
} else if ((!jsRows) || (!jsRows.length)) {
callback(null, {
rowCount: 0,
json: []
});
} else {
var callbackCalled = false;
var json;
try {
json = rowsReader.readJSON(jsRows);
} catch (parseError) {
callbackCalled = true;
callback(parseError);
}
if (!callbackCalled) {
var output = {
rowCount: jsRows.length,
json: json
};
if (output.json.length === 1) {
output.json = output.json[0];
}
callback(null, output);
}
}
});
self.query.apply(self, argumentsArray);
};
/**
* Internal function used to modify the INSERT/UPDATE SQL arguments.<br>

@@ -237,0 +311,0 @@ * This function will add the RETURNING clause to the SQL to support LOBs modification after the INSERT/UPDATE finished.<br>

@@ -114,2 +114,27 @@ 'use strict';

/**
* Read a JSON record.
*
* @function
* @memberof! RecordReader
* @public
* @param {object} jsRow - The JS object holding the row data.
* @param {string} column - The column name
* @returns {object} The JSON object
*/
RecordReader.prototype.readJSON = function (jsRow, column) {
var json;
if (jsRow && column) {
var jsonStr = jsRow[column];
if (jsonStr) {
json = JSON.parse(jsonStr);
} else {
json = {};
}
}
return json;
};
module.exports = new RecordReader();

@@ -56,2 +56,35 @@ 'use strict';

/**
* Read a JSON rows.
*
* @function
* @memberof! RowsReader
* @public
* @param {Array} jsRows - The JS objects holding the row data.
* @returns {object} The JSON object
*/
RowsReader.prototype.readJSON = function (jsRows) {
var json;
if (jsRows) {
if (jsRows.length) {
var keys = Object.keys(jsRows[0]);
if (keys.length !== 1) {
throw new Error('Expected exactly 1 column in response, found: ' + keys.length);
}
json = [];
var column = keys[0];
var index;
for (index = 0; index < jsRows.length; index++) {
json.push(recordReader.readJSON(jsRows[index], column));
}
} else {
json = [];
}
}
return json;
};
module.exports = new RowsReader();

2

package.json
{
"name": "simple-oracledb",
"version": "0.0.9",
"version": "0.0.10",
"description": "Extend capabilities of oracledb with simplified API for quicker development.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -14,2 +14,3 @@ # simple-oracledb

* [update](#usage-update)
* [queryJSON](#usage-queryJSON)
* [release](#usage-release)

@@ -161,2 +162,27 @@ * [terminate](#usage-terminate)

<a name="usage-queryJSON"></a>
## 'connection.queryJSON(sql, [bindVariables], [options], callback)'
This function will invoke the provided SQL SELECT and return a results object with the returned row count and the JSONs.<br>
The json property will hold a single JSON object in case the returned row count is 1, and an array of JSONs in case the row count is higher.<br>
The query expects that only 1 column is fetched and if more are detected in the results, this function will return an error in the callback.<br>
The function arguments used to execute the 'queryJSON' are exactly as defined in the oracledb connection.execute function.
```js
connection.queryJSON('SELECT JSON_DATA FROM APP_CONFIG WHERE ID > :id', [110], function onResults(error, results) {
if (error) {
//handle error...
} else if (results.rowCount === 1) { //single JSON is returned
//print the JSON
console.log(results.json);
} else if (results.rowCount > 1) { //multiple JSONs are returned
//print the JSON
results.json.forEach(function printJSON(json) {
console.log(json);
});
} else {
console.log('Did not find any results');
}
});
```
<a name="usage-release"></a>

@@ -226,7 +252,8 @@ ## 'connection.release([callback])'

| ----------- | ------- | ----------- |
| 2015-10-20 | v0.0.10 | Added connection.queryJSON |
| 2015-10-19 | v0.0.9 | autoCommit support when doing INSERT/UPDATE with LOBs |
| 2015-10-19 | v0.0.7 | Added pool.terminate support |
| 2015-10-19 | v0.0.7 | Added pool.terminate |
| 2015-10-19 | v0.0.6 | Maintenance |
| 2015-10-18 | v0.0.5 | Added connection.update support |
| 2015-10-18 | v0.0.4 | Added connection.insert support |
| 2015-10-18 | v0.0.5 | Added connection.update |
| 2015-10-18 | v0.0.4 | Added connection.insert |
| 2015-10-16 | v0.0.3 | Maintenance |

@@ -233,0 +260,0 @@ | 2015-10-15 | v0.0.1 | Initial release. |

@@ -887,2 +887,265 @@ 'use strict';

});
describe('queryJSON', function () {
it('error in query', function () {
var connection = {};
Connection.extend(connection);
connection.execute = function () {
var argumentsArray = Array.prototype.slice.call(arguments, 0);
argumentsArray.pop()(new Error('test error'));
};
connection.queryJSON(1, 2, 3, function (error) {
assert.isDefined(error);
assert.equal(error.message, 'test error');
});
});
it('error in parse', function () {
var connection = {};
Connection.extend(connection);
connection.execute = function () {
var argumentsArray = Array.prototype.slice.call(arguments, 0);
argumentsArray.pop()(null, [{
data: 'not json text'
}]);
};
connection.queryJSON(1, 2, 3, function (error) {
assert.isDefined(error);
});
});
it('empty', function () {
var connection = {};
Connection.extend(connection);
connection.query = function () {
var argumentsArray = Array.prototype.slice.call(arguments, 0);
argumentsArray.pop()(null, []);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(0, results.rowCount);
assert.deepEqual([], results.json);
});
});
it('undefined', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(0, results.rowCount);
assert.deepEqual([], results.json);
});
});
it('null', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, null);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(0, results.rowCount);
assert.deepEqual([], results.json);
});
});
it('single row empty data', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [{
data: ''
}]);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(1, results.rowCount);
assert.deepEqual({}, results.json);
});
});
it('single row undefined data', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [{
data: undefined
}]);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(1, results.rowCount);
assert.deepEqual({}, results.json);
});
});
it('single row not json data', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [{
data: 'some text'
}]);
};
connection.queryJSON(function (error) {
assert.isDefined(error);
});
});
it('multiple rows empty data', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [
{
data: ''
},
{
data: ''
}
]);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(2, results.rowCount);
assert.deepEqual([{}, {}], results.json);
});
});
it('multiple rows undefined data', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [
{
data: undefined
},
{
data: undefined
}
]);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(2, results.rowCount);
assert.deepEqual([{}, {}], results.json);
});
});
it('single row', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [
{
data: JSON.stringify({
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
})
}
]);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(1, results.rowCount);
assert.deepEqual({
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
}, results.json);
});
});
it('multiple rows', function () {
var connection = {};
Connection.extend(connection);
connection.query = function (callback) {
callback(null, [
{
data: JSON.stringify({
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
})
},
{
data: JSON.stringify({
a: 2,
test: true,
array: ['a', true],
subObject: {
key1: 'value1',
arr: [true, false, {}]
}
})
}
]);
};
connection.queryJSON(function (error, results) {
assert.isNull(error);
assert.equal(2, results.rowCount);
assert.deepEqual([
{
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
},
{
a: 2,
test: true,
array: ['a', true],
subObject: {
key1: 'value1',
arr: [true, false, {}]
}
}
], results.json);
});
});
});
});

@@ -398,2 +398,90 @@ 'use strict';

});
describe('readJSON tests', function () {
it('undefined no column', function () {
var json = RecordReader.readJSON();
assert.isUndefined(json);
});
it('undefined with column', function () {
var json = RecordReader.readJSON(undefined, 'data');
assert.isUndefined(json);
});
it('not json no column', function () {
try {
RecordReader.readJSON('some text');
assert.fail();
} catch (error) {
assert.isDefined(error);
}
});
it('not json with column', function () {
try {
RecordReader.readJSON('some text', 'data');
assert.fail();
} catch (error) {
assert.isDefined(error);
}
});
it('not json in data with column', function () {
try {
RecordReader.readJSON({
data: 'some text'
}, 'data');
assert.fail();
} catch (error) {
assert.isDefined(error);
}
});
it('json no column', function () {
var output = RecordReader.readJSON({
data: JSON.stringify({
a: 1
})
});
assert.isUndefined(output);
});
it('json with column', function () {
var output = RecordReader.readJSON({
data: JSON.stringify({
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
})
}, 'data');
assert.deepEqual(output, {
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
});
});
it('json wrong column', function () {
var output = RecordReader.readJSON({
data: JSON.stringify({
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
})
}, 'wrong');
assert.deepEqual(output, {});
});
});
});

@@ -414,2 +414,74 @@ 'use strict';

});
describe('readJSON tests', function () {
it('undefined', function () {
var json = RowsReader.readJSON();
assert.isUndefined(json);
});
it('multiple keys', function () {
try {
RowsReader.readJSON([{
key1: JSON.stringify({
test: true
}),
key2: JSON.stringify({
test: true
})
}]);
assert.fail();
} catch (error) {
assert.isDefined(error);
}
});
it('no rows', function () {
var json = RowsReader.readJSON([]);
assert.deepEqual([], json);
});
it('multiple json rows', function () {
var output = RowsReader.readJSON([
{
data: JSON.stringify({
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
})
},
{
data: JSON.stringify({
a: 2,
test: false,
array: [1, 'b', 3],
subObject: {
key1: 'value1'
}
})
}
]);
assert.deepEqual(output, [
{
a: 1,
test: true,
array: [1, 2, 3],
subObject: {
key1: 'value1'
}
},
{
a: 2,
test: false,
array: [1, 'b', 3],
subObject: {
key1: 'value1'
}
}
]);
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc