From 1eea7b28b8fa9cbdc90038e961f8674a1ae32d22 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 13:41:33 +0700 Subject: [PATCH 01/13] Add files via upload --- index.js | 3 +++ package.json | 14 ++++++++++++++ sqlConnect.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 index.js create mode 100644 package.json create mode 100644 sqlConnect.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..4bffcbf --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +module.exports = { + SqlConnection = require('./sqlConnect') +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..c05dcbd --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "sql-node-test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "dependencies": { + "node-mssql": "0.0.1" + } +} diff --git a/sqlConnect.js b/sqlConnect.js new file mode 100644 index 0000000..ef004af --- /dev/null +++ b/sqlConnect.js @@ -0,0 +1,34 @@ +var sql = require('mssql'); + +function SqlConnect(config){ + configuration = { + user: config.user, + password: config.password, + server: config.server, + port: config.port, + database: config.database, + options: { + encrypt: config.encrypt + } + }; +} + +SqlConnect.prototype.query = function getQueryResult(sqlString){ + + return new Promise(function(fulfill, reject){ + var connection = new sql.Connection(configuration, function(err){ + if (err) { + reject(err); + } + else { + var request = new sql.Request(connection); + request.query(sqlString).then(result => fulfill(result)).catch( error => reject(error)); + } + }); + }); +} + + +module.exports = SqlConnect; + + From c8dc8243acdc826046c0b868a78352bf14d77248 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 13:42:35 +0700 Subject: [PATCH 02/13] Delete pull.txt --- pull.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 pull.txt diff --git a/pull.txt b/pull.txt deleted file mode 100644 index b54c66f..0000000 --- a/pull.txt +++ /dev/null @@ -1 +0,0 @@ -test pull From 7041fae43758e5705f8f53fde4692e85609144b6 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 14:37:12 +0700 Subject: [PATCH 03/13] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4bffcbf..48337c8 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ module.exports = { - SqlConnection = require('./sqlConnect') -}; \ No newline at end of file + SqlConnection : require('./sqlConnect') +}; From 0c054f52332b288ff1233bf1c421a1da84950494 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 14:44:29 +0700 Subject: [PATCH 04/13] Update package.json --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c05dcbd..ac71f3e 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { - "name": "sql-node-test", + "name": "bateeq-legacy-db-module", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, + "repository": { + "type": "git", + "url": "git+https://github.com/tr4nquility/bateeq-legacy-db-module.git" + }, "author": "", "license": "MIT", "dependencies": { From 2dc47c7ba7551c0d3bdbfd224daed4f7b7dffd22 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 15:02:25 +0700 Subject: [PATCH 05/13] Update README.md --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b082b33..43a0208 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # bateeq-legacy-db-module -Module to retrieve data from Bateeq legacy database +Module to retrieve data from Bateeq legacy database. + +# Usage example : +var db = require('bateeq-legacy-db-module'); +var db_config = { + user: '', + password: '', + server: '', // You can use 'localhost\\instance' to connect to named instance + port: , + database: '', + debug: false, + options: { + encrypt: false // Use this if you're on Windows Azure + } +} +var conn = new db.SqlConnection(db_config); +conn.query('SELECT TOP 1 * FROM wfdocument order by createdate desc').then(res => console.log(res)).catch(err => console.log(err)); From 64f2d53cb4268dfc461b70227b8e2448365408be Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 15:10:08 +0700 Subject: [PATCH 06/13] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 43a0208..fe597cd 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Module to retrieve data from Bateeq legacy database. # Usage example : +```javascript var db = require('bateeq-legacy-db-module'); var db_config = { user: '', @@ -16,3 +17,4 @@ var db_config = { } var conn = new db.SqlConnection(db_config); conn.query('SELECT TOP 1 * FROM wfdocument order by createdate desc').then(res => console.log(res)).catch(err => console.log(err)); +``` From 5fcb75d97fa7b283b5322c56b6700a9d7994adff Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Mon, 6 Jun 2016 18:33:47 +0700 Subject: [PATCH 07/13] Update sqlConnect.js --- sqlConnect.js | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/sqlConnect.js b/sqlConnect.js index ef004af..65a7433 100644 --- a/sqlConnect.js +++ b/sqlConnect.js @@ -1,34 +1,42 @@ -var sql = require('mssql'); +var sql = require('mssql'); -function SqlConnect(config){ - configuration = { +function SqlConnect(config) { + var configuration = { user: config.user, password: config.password, server: config.server, port: config.port, database: config.database, options: { - encrypt: config.encrypt + encrypt: config.options.encrypt } }; + connection = new sql.Connection(configuration); } -SqlConnect.prototype.query = function getQueryResult(sqlString){ - - return new Promise(function(fulfill, reject){ - var connection = new sql.Connection(configuration, function(err){ - if (err) { - reject(err); - } - else { - var request = new sql.Request(connection); - request.query(sqlString).then(result => fulfill(result)).catch( error => reject(error)); - } - }); - }); +SqlConnect.prototype.query = function getQueryResult(sqlString) { + return new Promise(function (fulfill, reject) { + if (connection) { + // try to connect using specified connection + connection.connect().then(function () { + // create a new request + var request = new sql.Request(connection); + request.query(sqlString, function (err, recordset) { + if (err) reject({ + code: err.code, + message: err.message + }); + else fulfill({ query: sqlString, result: recordset }); + }); + }).catch(err => reject({ + code: err.code, + message: err.message + })); + } + else + reject('SQL Connection is not available.'); + }) } - - module.exports = SqlConnect; From 7c096a30359e26083b7d0aa65c8fc2d02e15a5b6 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Tue, 7 Jun 2016 08:54:32 +0700 Subject: [PATCH 08/13] Update sqlConnect.js --- sqlConnect.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/sqlConnect.js b/sqlConnect.js index 65a7433..6e2e7e8 100644 --- a/sqlConnect.js +++ b/sqlConnect.js @@ -1,7 +1,7 @@ var sql = require('mssql'); function SqlConnect(config) { - var configuration = { + configuration = { user: config.user, password: config.password, server: config.server, @@ -11,10 +11,13 @@ function SqlConnect(config) { encrypt: config.options.encrypt } }; - connection = new sql.Connection(configuration); } -SqlConnect.prototype.query = function getQueryResult(sqlString) { +SqlConnect.prototype.getConnection = function getConnection() { + return new sql.Connection(configuration); +} + +SqlConnect.prototype.query = function getQueryResult(sqlString, connection) { return new Promise(function (fulfill, reject) { if (connection) { // try to connect using specified connection @@ -22,11 +25,16 @@ SqlConnect.prototype.query = function getQueryResult(sqlString) { // create a new request var request = new sql.Request(connection); request.query(sqlString, function (err, recordset) { - if (err) reject({ - code: err.code, - message: err.message - }); - else fulfill({ query: sqlString, result: recordset }); + if (err) + reject({ + code: err.code, + message: err.message + }); + else + fulfill({ + query: sqlString, + result: recordset + }); }); }).catch(err => reject({ code: err.code, @@ -36,7 +44,6 @@ SqlConnect.prototype.query = function getQueryResult(sqlString) { else reject('SQL Connection is not available.'); }) + } module.exports = SqlConnect; - - From 1135b3878296ea04a0012cce30a115c6d0b53996 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Tue, 7 Jun 2016 08:59:52 +0700 Subject: [PATCH 09/13] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe597cd..7fb20a3 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ var db_config = { database: '', debug: false, options: { - encrypt: false // Use this if you're on Windows Azure + encrypt: false // Set this to if you're on Windows Azure } } var conn = new db.SqlConnection(db_config); -conn.query('SELECT TOP 1 * FROM wfdocument order by createdate desc').then(res => console.log(res)).catch(err => console.log(err)); +conn.query('SELECT TOP 1 * FROM ').then(result => console.log(result)).catch(error => console.log(error)); ``` From e0f926241b435c13a4545e8fa53721c5f57c1078 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Tue, 7 Jun 2016 09:10:09 +0700 Subject: [PATCH 10/13] Update sqlConnect.js --- sqlConnect.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sqlConnect.js b/sqlConnect.js index 6e2e7e8..8104ed8 100644 --- a/sqlConnect.js +++ b/sqlConnect.js @@ -13,11 +13,8 @@ function SqlConnect(config) { }; } -SqlConnect.prototype.getConnection = function getConnection() { - return new sql.Connection(configuration); -} - -SqlConnect.prototype.query = function getQueryResult(sqlString, connection) { +SqlConnect.prototype.query = function getQueryResult(sqlString) { + var connection = new sql.Connection(configuration); return new Promise(function (fulfill, reject) { if (connection) { // try to connect using specified connection @@ -44,6 +41,6 @@ SqlConnect.prototype.query = function getQueryResult(sqlString, connection) { else reject('SQL Connection is not available.'); }) - } + module.exports = SqlConnect; From 8092865fcb187c192151625525b07eff9ff84c87 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Tue, 7 Jun 2016 09:10:50 +0700 Subject: [PATCH 11/13] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ac71f3e..6c9cec4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bateeq-legacy-db-module", - "version": "1.0.0", + "version": "1.0.5", "description": "", "main": "index.js", "scripts": { From 2f5ea84df1a32646a292d9df95a953040e31e936 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Tue, 7 Jun 2016 15:59:16 +0700 Subject: [PATCH 12/13] Update sqlConnect.js --- sqlConnect.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/sqlConnect.js b/sqlConnect.js index 8104ed8..fc64d16 100644 --- a/sqlConnect.js +++ b/sqlConnect.js @@ -43,4 +43,59 @@ SqlConnect.prototype.query = function getQueryResult(sqlString) { }) } +SqlConnect.prototype.execSP = function getResult(options) { + var connection = new sql.Connection(configuration); + return new Promise(function (fulfill, reject) { + if (connection) { + connection.connect().then(function () { + if (!options.hasOwnProperty('sp_name')) + reject('sp_name is missing.'); + + // create a new request + var request = new sql.Request(connection); + request.multiple = false; + if (options.hasOwnProperty('params')) { + Object.keys(options.params).forEach(function (key) { + var value = options.params[key]; + switch (Object.prototype.toString.call(value)) { + case "[object String]": + request.input(key, sql.NVarChar, value); + break; + case "[object Number]": + request.input(key, sql.Int, value); + break; + case "[object Date]": + request.input(key, sql.DateTime, value); + break; + default: + request.input(key, sql.NVarChar, value); + break; + } + }) + } + + request.execute(options.sp_name, function (err, recordset, returnValue, affected) { + if (err) + reject({ + code: err.code, + message: err.message + }) + else { + fulfill({ + retVal: returnValue, + affected: affected, + data: recordset[0] + }) + } + }); + }).catch(err => reject({ + code: err.code, + message: err.message + })); + } + else + reject('SQL Connection is not available'); + }); +} + module.exports = SqlConnect; From 71e7a419da38a0ada7782522dc0b8267906f9494 Mon Sep 17 00:00:00 2001 From: tr4nquility Date: Tue, 7 Jun 2016 16:05:40 +0700 Subject: [PATCH 13/13] Update README.md --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 7fb20a3..213576b 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Module to retrieve data from Bateeq legacy database. # Usage example : +Normal select queries : ```javascript var db = require('bateeq-legacy-db-module'); var db_config = { @@ -18,3 +19,30 @@ var db_config = { var conn = new db.SqlConnection(db_config); conn.query('SELECT TOP 1 * FROM ').then(result => console.log(result)).catch(error => console.log(error)); ``` +Executing stored procedure : +```javascript +var db = require('bateeq-legacy-db-module'); +var db_config = { + user: '', + password: '', + server: '', // You can use 'localhost\\instance' to connect to named instance + port: , + database: '', + debug: false, + options: { + encrypt: false // Set this to if you're on Windows Azure + } +} + +var conn = new db.SqlConnection(db_config); +var options = { + sp_name: '', + params:{ + dateFrom: '', + dateTo: '', + branch: '' + } +} +conn.execSP(options).then(res => console.log(res.data)).catch(err => console.log(err)); +``` +You can use Stored Procedure if you want to query cross databases.