Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Commit

Permalink
To ensure that configurtion can be pulled identically from config fil…
Browse files Browse the repository at this point in the history
…es, the ENV, and

command-line arguments, the db module no longer relies on pulling config objects
with config-store (which only works for file-based config).
  • Loading branch information
stenington committed Dec 12, 2013
1 parent 25fd473 commit f0b7824
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
18 changes: 16 additions & 2 deletions app/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ const fs = require('fs');
const streamsql = require('streamsql');
const config = require('./config');

module.exports = function getDB (key) {
return streamsql.connect(config(key));
function getDbConfig (prefix) {
prefix += '_';
return {
driver: config(prefix+'DRIVER', 'mysql'),
host: config(prefix+'HOST', 'localhost'),
user: config(prefix+'USER'),
password: config(prefix+'PASSWORD'),
database: config(prefix+'DATABASE')
}
}

function getDb (prefix) {
return streamsql.connect(getDbConfig(prefix));
};

module.exports.getDb = getDb;
module.exports.getDbConfig = getDbConfig;
4 changes: 2 additions & 2 deletions app/models/badge.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var getDB = require('../lib/db');
var getDb = require('../lib/db').getDb;

module.exports = function getBadgeModel (key) {
var db = getDB(key);
var db = getDb(key);
try {
return db.table('badge');
}
Expand Down
4 changes: 2 additions & 2 deletions bin/db-migrate
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var migrate = require('db-migrate');
var log = require('db-migrate/lib/log');
var mysql = require('mysql');
var migrations = require('../lib/migrations');
var config = require('../app/lib/config');
var db = require('../app/lib/db');

process.on('uncaughtException', function(err) {
log.error(err.stack);
Expand Down Expand Up @@ -76,7 +76,7 @@ function run() {
break;
case 'up':
case 'down':
argv.config = config('DATABASE');
argv.config = db.getDbConfig('DATABASE');
if(verbose) {
log.info("Using settings:", argv.config);
}
Expand Down
5 changes: 3 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var async = require('async');
var migrations = require('../lib/migrations');
var config = require('../app/lib/config');
var connection = require('../app/lib/db')("TEST_DATABASE");
const DB_CONFIG = config("TEST_DATABASE");
var db = require('../app/lib/db');
var connection = db.getDb("TEST_DATABASE");
const DB_CONFIG = db.getDbConfig("TEST_DATABASE");

exports.up = function up(options) {
options = options || {};
Expand Down

0 comments on commit f0b7824

Please sign in to comment.