From e206e6044bc49d9ca0aea59fc08990b8d04d23df Mon Sep 17 00:00:00 2001 From: ctjong Date: Tue, 6 Mar 2018 23:14:51 -0800 Subject: [PATCH] db adapter fixes --- adapters/db/mssqldb.js | 17 ++---- adapters/db/mysqldb.js | 119 ++++++++++++++++++++++------------------- index.js | 1 - package.json | 2 +- 4 files changed, 68 insertions(+), 71 deletions(-) diff --git a/adapters/db/mssqldb.js b/adapters/db/mssqldb.js index 2c201d9..b7077fe 100644 --- a/adapters/db/mssqldb.js +++ b/adapters/db/mssqldb.js @@ -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 @@ -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(); @@ -312,9 +303,10 @@ 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) { @@ -322,7 +314,7 @@ module.exports = 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) { @@ -466,7 +458,6 @@ module.exports = } } - this.initialize = initialize; this.quickFind = quickFind; this.select = select; this.findRecordById = findRecordById; diff --git a/adapters/db/mysqldb.js b/adapters/db/mysqldb.js index 351502b..f175109 100644 --- a/adapters/db/mysqldb.js +++ b/adapters/db/mysqldb.js @@ -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 @@ -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(); } /** @@ -435,7 +443,6 @@ module.exports = } } - this.initialize = initialize; this.quickFind = quickFind; this.select = select; this.findRecordById = findRecordById; diff --git a/index.js b/index.js index cf647b9..455f5a0 100644 --- a/index.js +++ b/index.js @@ -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) diff --git a/package.json b/package.json index 3fa19a0..b6a4d6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "orion-api", - "version": "1.2.7", + "version": "1.2.8", "description": "REST API server application", "main": "index.js", "repository": {