From c753630a29d5b02e5192dae5e5e627477ebc7934 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 29 May 2017 19:38:04 -0600 Subject: [PATCH] initialize db only once --- lib/dbwrap.js | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/dbwrap.js b/lib/dbwrap.js index a594867..5cecf42 100644 --- a/lib/dbwrap.js +++ b/lib/dbwrap.js @@ -12,15 +12,6 @@ function wrap(db, dir, dbsMap) { dbsMap = {}; } - db.escape = function (str) { - // TODO? literals for true,false,null - // error on undefined? - if (undefined === str) { - str = ''; - } - return String(str).replace(/'/g, "''"); - }; - function lowerFirst(str) { return str.charAt(0).toLowerCase() + str.slice(1); } @@ -157,19 +148,6 @@ function wrap(db, dir, dbsMap) { dir.indices.forEach(normalizeColumn); DB._indices = dir.indices; - db = PromiseA.promisifyAll(db); - - if (dir && dir.verbose || db.verbose) { - console.log('Getting Verbose up in here'); - db.on('trace', function (str) { - console.log('SQL:', str); - }); - - db.on('profile', function (sql, ms) { - console.log('Profile:', ms); - }); - } - function simpleParse(row) { if (!row) { return null; @@ -581,6 +559,32 @@ function wrap(db, dir, dbsMap) { }); } + if (!db.__masterquest_init) { + db.__masterquest_init = true; + db = PromiseA.promisifyAll(db); + db.__masterquest_init = true; + db.escape = function (str) { + // TODO? literals for true,false,null + // error on undefined? + if (undefined === str) { + str = ''; + } + return String(str).replace(/'/g, "''"); + }; + + if (dir && dir.verbose || db.verbose) { + console.log('Getting Verbose up in here'); + db.on('trace', function (str) { + console.log('SQL:', str); + }); + + db.on('profile', function (sql, ms) { + console.log('Profile:', ms); + }); + } + } + dbsMap.sql = db; + dir.forEach(function (dir) { // TODO if directive is the same as existing dbsMap, skip it promises.push(createTable(dir).then(function (dbw) { @@ -591,8 +595,6 @@ function wrap(db, dir, dbsMap) { })); }); - dbsMap.sql = db; - return PromiseA.all(promises).then(function (/*dbs*/) { return dbsMap; });