Skip to content

Commit

Permalink
more refactoring and renaming to make storage generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Oct 16, 2016
1 parent 23c0fcb commit 6e48e62
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
18 changes: 9 additions & 9 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function config ( ) {
setSSL();
setAPISecret();
setVersion();
setMongo();
setStorage();
updateSettings();

return env;
Expand Down Expand Up @@ -90,12 +90,12 @@ function setVersion() {
env.name = software.name;
}

function setMongo() {
env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI');
env.mongo_collection = readENV('MONGO_COLLECTION', 'entries');
function setStorage() {
env.storageURI = readENV('STORAGE_URI') || readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI');
env.entries_collection = readENV('ENTRIES_COLLECTION') || readENV('MONGO_COLLECTION', 'entries');
env.MQTT_MONITOR = readENV('MQTT_MONITOR', null);
if (env.MQTT_MONITOR) {
var hostDbCollection = [env.mongo.split('mongodb://').pop().split('@').pop(), env.mongo_collection].join('/');
var hostDbCollection = [env.storageURI.split('mongodb://').pop().split('@').pop(), env.entries_collection].join('/');
var mongoHash = crypto.createHash('sha1');
mongoHash.update(hostDbCollection);
//some MQTT servers only allow the client id to be 23 chars
Expand All @@ -117,10 +117,10 @@ function setMongo() {
// Some people prefer to use a json configuration file instead.
// This allows a provided json config to override environment variables
var DB = require('./database_configuration.json'),
DB_URL = DB.url ? DB.url : env.mongo,
DB_COLLECTION = DB.collection ? DB.collection : env.mongo_collection;
env.mongo = DB_URL;
env.mongo_collection = DB_COLLECTION;
DB_URL = DB.url ? DB.url : env.storageURI,
DB_COLLECTION = DB.collection ? DB.collection : env.entries_collection;
env.storageURI = DB_URL;
env.entries_collection = DB_COLLECTION;
}

function updateSettings() {
Expand Down
6 changes: 3 additions & 3 deletions lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function boot (env) {
return ctx.bootErrors && ctx.bootErrors.length > 0;
}

function setupMongo (ctx, next) {
function setupStorage (ctx, next) {

if (hasBootErrors(ctx)) {
return next();
}

try {
require('./storage')(env, function ready ( err, store ) {
require('./storage/mongo-storage')(env, function ready ( err, store ) {
// FIXME, error is always null, if there is an error, the storage.js will throw an exception
console.log('Storage system ready');
ctx.store = store;
Expand Down Expand Up @@ -170,7 +170,7 @@ function boot (env) {

return require('bootevent')( )
.acquire(checkEnv)
.acquire(setupMongo)
.acquire(setupStorage)
.acquire(setupAuthorization)
.acquire(setupInternals)
.acquire(ensureIndexes)
Expand Down
2 changes: 1 addition & 1 deletion lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function storage(env, ctx) {
function api ( ) {
// obtain handle usable for querying the collection associated
// with these records
return ctx.store.db.collection(env.mongo_collection);
return ctx.store.db.collection(env.entries_collection);
}

// Expose all the useful functions
Expand Down
4 changes: 2 additions & 2 deletions lib/storage.js → lib/storage/mongo-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function init (env, cb, forceNewConnection) {
cb(null, mongo);
}
} else {
if (!env.mongo) {
if (!env.storageURI) {
throw new Error('MongoDB connection string is missing');
}

Expand All @@ -28,7 +28,7 @@ function init (env, cb, forceNewConnection) {
var options = { replset: { socketOptions: { connectTimeoutMS : timeout, socketTimeoutMS : timeout }}};

var connect_with_retry = function(i) {
return MongoClient.connect(env.mongo, options, function connected(err, db) {
return MongoClient.connect(env.storageURI, options, function connected(err, db) {
if (err) {
if (i>20) {
// Abort after retrying for more than 10 minutes
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function init (env, ctx, server) {
// TODO: this would be better to have somehow integrated/improved
var supportedCollections = {
'treatments' : env.treatments_collection,
'entries': env.mongo_collection,
'entries': env.entries_collection,
'devicestatus': env.devicestatus_collection,
'profile': env.profile_collection,
'food': env.food_collection
Expand Down
4 changes: 2 additions & 2 deletions testing/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ main();

function main() {
var MongoClient = mongodb.MongoClient;
MongoClient.connect(env.mongo, function connected(err, db) {
MongoClient.connect(env.storageURI, function connected(err, db) {

console.log('Connecting to mongo...');
if (err) {
Expand All @@ -21,7 +21,7 @@ function main() {
}

function populate_collection(db) {
var cgm_collection = db.collection(env.mongo_collection);
var cgm_collection = db.collection(env.entries_collection);
var new_cgm_record = util.get_cgm_record();

cgm_collection.insert(new_cgm_record, function (err) {
Expand Down
12 changes: 6 additions & 6 deletions tests/storage.test.js → tests/mongo-storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ describe('STORAGE', function () {
});

it('The storage class should be OK.', function (done) {
should.exist(require('../lib/storage'));
should.exist(require('../lib/storage/mongo-storage'));
done();
});

it('After initializing the storage class it should re-use the open connection', function (done) {
var store = require('../lib/storage');
var store = require('../lib/storage/mongo-storage');
store(env, function (err1, db1) {
should.not.exist(err1);

Expand All @@ -31,11 +31,11 @@ describe('STORAGE', function () {
});

it('When no connection-string is given the storage-class should throw an error.', function (done) {
delete env.mongo;
should.not.exist(env.mongo);
delete env.storageURI;
should.not.exist(env.storageURI);

(function () {
return require('../lib/storage')(env, false, true);
return require('../lib/storage/mongo-storage')(env, false, true);
}).should.throw('MongoDB connection string is missing');

done();
Expand All @@ -45,7 +45,7 @@ describe('STORAGE', function () {
env.mongo = 'This is not a MongoDB connection-string';

(function () {
return require('../lib/storage')(env, false, true);
return require('../lib/storage/mongo-storage')(env, false, true);
}).should.throw(Error);

done();
Expand Down
4 changes: 2 additions & 2 deletions tests/mqtt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('mqtt', function ( ) {

before(function () {
process.env.MQTT_MONITOR = 'mqtt://user:password@localhost:12345';
process.env.MONGO='mongodb://localhost/test_db';
process.env.MONGO_COLLECTION='test_sgvs';
process.env.STORAGE_URI='mongodb://localhost/test_db';
process.env.ENTRIES_COLLECTION='test_sgvs';
self.env = require('../env')();
self.es = require('event-stream');
self.results = self.es.through(function (ch) { this.push(ch); });
Expand Down

0 comments on commit 6e48e62

Please sign in to comment.