Skip to content

Commit

Permalink
db adapter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ctjong committed Mar 7, 2018
1 parent 6692979 commit e206e60
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 71 deletions.
17 changes: 4 additions & 13 deletions adapters/db/mssqldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ module.exports =
// PUBLIC
//----------------------------------------------

/**
* Initialize the adapter
* @param {any} config Site configuration
*/
function initialize(config)
{
// initialization should happen on the first request
}

/**
* Quick find a record based on the given condition
* @param {any} ctx Request context
Expand Down Expand Up @@ -260,7 +251,7 @@ module.exports =
*/
function execute(ctx, query, successCb, completeCb)
{
ensurePoolInitialized(function()
ensurePoolInitialized(ctx, function()
{
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
Expand Down Expand Up @@ -312,17 +303,18 @@ module.exports =

/**
* Ensure the connection pool is initialized
* @param {any} ctx Request context
* @param {any} callback Callback function
*/
function ensurePoolInitialized(callback)
function ensurePoolInitialized(ctx, callback)
{
if(!!pool)
{
callback();
return;
}
var sql = require("mssql");
pool = new sql.ConnectionPool(config.database.connectionString, function(err)
pool = new sql.ConnectionPool(ctx.config.database.connectionString, function(err)
{
if (err)
{
Expand Down Expand Up @@ -466,7 +458,6 @@ module.exports =
}
}

this.initialize = initialize;
this.quickFind = quickFind;
this.select = select;
this.findRecordById = findRecordById;
Expand Down
119 changes: 63 additions & 56 deletions adapters/db/mysqldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,6 @@ module.exports =
// PUBLIC
//----------------------------------------------

/**
* Initialize the adapter
* @param {any} config Site configuration
*/
function initialize(config)
{
if(!!pool ||!config.database.connectionString)
return;
var sql = require("mysql");

// there is an issue with creating mysql connection based on connection string.
// so we have to convert the string into a connection properties object.
var connString = config.database.connectionString;
var connStringParts = connString.split(";");
var connProps = {};
for (var i = 0; i < connStringParts.length; i++)
{
var connPropTokens = connStringParts[i].split('=');
connProps[connPropTokens[0]] = connPropTokens[1];
}
pool = sql.createPool(
{
host: connProps.Server,
user: connProps.Uid,
password: connProps.Pwd,
database: connProps.Database,
multipleStatements: true
});
pool.sql = sql;
}

/**
* Quick find a record based on the given condition
* @param {any} ctx Request context
Expand Down Expand Up @@ -282,36 +251,75 @@ module.exports =
*/
function execute(ctx, query, successCb, completeCb)
{
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);

pool.query(queryString, queryParams, function (error, results, fields)
ensurePoolInitialized(ctx, function()
{
if (error)
{
if (!!completeCb)
_this.exec.safeExecute(ctx, completeCb);
console.log(error);
throw new _this.error.Error("a07f", 500, "error while sending query to database");
}
else
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);

pool.query(queryString, queryParams, function (error, results, fields)
{
_this.exec.safeExecute(ctx, function ()
if (error)
{
successCb(results);
});
if (!!completeCb)
if (!!completeCb)
_this.exec.safeExecute(ctx, completeCb);
console.log(error);
throw new _this.error.Error("a07f", 500, "error while sending query to database");
}
else
{
_this.exec.safeExecute(ctx, completeCb);
_this.exec.safeExecute(ctx, function ()
{
successCb(results);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, completeCb);
}
}
}
});
console.log("-------------------------------------------------");
});
console.log("-------------------------------------------------");
}

/**
* Ensure the connection pool is initialized
* @param {any} ctx Request context
* @param {any} callback Callback function
*/
function ensurePoolInitialized(ctx, callback)
{
if(!!pool)
{
callback();
return;
}
var sql = require("mysql");

// there is an issue with creating mysql connection based on connection string.
// so we have to convert the string into a connection properties object.
var connString = ctx.config.database.connectionString;
var connStringParts = connString.split(";");
var connProps = {};
for (var i = 0; i < connStringParts.length; i++)
{
var connPropTokens = connStringParts[i].split('=');
connProps[connPropTokens[0]] = connPropTokens[1];
}
pool = sql.createPool(
{
host: connProps.Server,
user: connProps.Uid,
password: connProps.Pwd,
database: connProps.Database,
multipleStatements: true
});
pool.sql = sql;
callback();
}

/**
Expand Down Expand Up @@ -435,7 +443,6 @@ module.exports =
}
}

this.initialize = initialize;
this.quickFind = quickFind;
this.select = select;
this.findRecordById = findRecordById;
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ module.exports = function(config)
modules.addDef("db", './adapters/db/mysqldb');
else
throw "Unsupported database management system " + config.dbms;
modules.get("db").initialize(config);

// storage system
if (!!config.storage)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orion-api",
"version": "1.2.7",
"version": "1.2.8",
"description": "REST API server application",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit e206e60

Please sign in to comment.