Created
April 6, 2014 11:08
-
-
Save jmav/10004569 to your computer and use it in GitHub Desktop.
Add fetcher to db module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Make DB connection & prepare pool of connections | |
*/ | |
'use strict'; | |
// Modules | |
var dbSrc = require('node-mysql-q'), | |
_ = require('underscore'); | |
var env = process.env.NODE_ENV || 'development', | |
config = require('./../config/app-config.json')[env], | |
DEBUG = config.verbose; | |
// Set pool for external usage | |
exports.dbPool = new dbSrc(config.mysql); | |
var fetch = exports.fetch = {}; | |
var get = exports.get = {}; | |
fetch.languages = function(cache) { | |
var query = 'SELECT lang, title, currency '+ | |
'FROM languages ' + | |
'WHERE enabled = "True" AND visible = "True" '; | |
// Make query | |
if ( _.isEmpty(get.languages) || cache ) { | |
module.languagesQ = exports.dbPool.queryDb(query) | |
.then(function(data){ | |
DEBUG && console.log('\n Updating available languages ...'); | |
DEBUG && console.log('-----------------------------'); | |
get.languages = {}; | |
_.each(data, function(attrs, lang){ | |
get.languages[attrs.lang] = attrs; | |
}); | |
return get.languages; | |
// @todo add error handling - don't return just value | |
}); | |
return module.languagesQ; | |
} else { | |
DEBUG && console.log('\n Using cached languages ...'); | |
return module.languagesQ; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment