Skip to content

Instantly share code, notes, and snippets.

@jmav
Created April 6, 2014 11:08
Show Gist options
  • Save jmav/10004569 to your computer and use it in GitHub Desktop.
Save jmav/10004569 to your computer and use it in GitHub Desktop.
Add fetcher to db module
/**
* 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