diff --git a/README.md b/README.md index b082b33..213576b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,48 @@ # bateeq-legacy-db-module -Module to retrieve data from Bateeq legacy database +Module to retrieve data from Bateeq legacy database. + +# Usage example : +Normal select queries : +```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); +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. diff --git a/index.js b/index.js new file mode 100644 index 0000000..48337c8 --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +module.exports = { + SqlConnection : require('./sqlConnect') +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..6c9cec4 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "bateeq-legacy-db-module", + "version": "1.0.5", + "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": { + "node-mssql": "0.0.1" + } +} 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 diff --git a/sqlConnect.js b/sqlConnect.js new file mode 100644 index 0000000..fc64d16 --- /dev/null +++ b/sqlConnect.js @@ -0,0 +1,101 @@ +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.options.encrypt + } + }; +} + +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 + 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.'); + }) +} + +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;