Skip to content

Commit

Permalink
fix(initDB): initDB was not set up to run from the command line
Browse files Browse the repository at this point in the history
Move the functionality into a lib file to aid testing.
  • Loading branch information
petebacondarwin committed Sep 3, 2013
1 parent 903d393 commit 535a942
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 79 deletions.
80 changes: 3 additions & 77 deletions server/initDB.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,5 @@
// code for initializing the DB w/ an admin user

var config = require('./config.js');
var rest = require('request');

var initDB = {
adminUser: { email: '[email protected]', password: 'changeme', admin: true, firstName: 'Admin', lastName: 'User' },

initialize: function(cfg) {
initDB.apiKey = cfg.mongo.apiKey
initDB.baseUrl = cfg.mongo.dbUrl + '/databases/' + cfg.security.dbName + '/collections/';
initDB.usersCollection = cfg.security.usersCollection;
},

checkDocument: function(collection, query, done) {
var url = initDB.baseUrl + collection + '/';
console.log("rest.get - " + url);
var params = { apiKey:initDB.apiKey, q: JSON.stringify(query) };
var request = rest.get(url, { qs: params, json: {} }, function(err, response, data) {
if ( err ) {
console.log('There was an error checking the documents', err);
}
done(err, data);
});
},

createDocument: function(collection, doc, done) {
var url = initDB.baseUrl + collection + '/';
console.log("rest.post - " + url);
var request = rest.post(url, { qs: { apiKey:initDB.apiKey }, json: doc }, function(err, response, data) {
if ( !err ) {
console.log('Document created', data);
}
done(err, data);
});
},

deleteDocument: function(collection, docId, done) {
var url = initDB.baseUrl + collection + '/' + docId;
console.log("rest.del - " + url);
var request = rest.del(url, { qs: { apiKey:initDB.apiKey }, json: {} }, function(err, response, data) {
if ( !err ) {
console.log('Document deleted', data);
}
done(err, data);
});
},

addAdminUser: function(done) {
console.log('*** Admin user properties:', initDB.adminUser);
console.log('Checking that admin user does not exist...');
initDB.checkDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
if ( !err && data.length === 0 ) {
console.log('Creating new admin user...', err, data);
initDB.createDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
console.log('Created new admin user...');
console.log(err);
console.log(data);
done(err, data);
});
} else {
if (data.message) {
console.log('Error: ' + data.message);
} else {
console.log('User already created.');
}
done(err, data);
}
});
}
};

module.exports = initDB;

/*
var config = require('./config');
var initDB = require('./lib/initDB');
console.log('*** Configuration: \n', config);
initDB.initialize(config);
initDB.addAdminUser();
*/
initDB.addAdminUser(function() {});
72 changes: 72 additions & 0 deletions server/lib/initDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// code for initializing the DB w/ an admin user
var rest = require('request');

var initDB = {
adminUser: { email: '[email protected]', password: 'changeme', admin: true, firstName: 'Admin', lastName: 'User' },

initialize: function(cfg) {
initDB.apiKey = cfg.mongo.apiKey;
initDB.baseUrl = cfg.mongo.dbUrl + '/databases/' + cfg.security.dbName + '/collections/';
initDB.usersCollection = cfg.security.usersCollection;
},

checkDocument: function(collection, query, done) {
var url = initDB.baseUrl + collection + '/';
console.log("rest.get - " + url);
var params = { apiKey:initDB.apiKey, q: JSON.stringify(query) };
var request = rest.get(url, { qs: params, json: {} }, function(err, response, data) {
if ( err ) {
console.log('There was an error checking the documents', err);
}
done(err, data);
});
},

createDocument: function(collection, doc, done) {
var url = initDB.baseUrl + collection + '/';
console.log("rest.post - " + url);
var request = rest.post(url, { qs: { apiKey:initDB.apiKey }, json: doc }, function(err, response, data) {
if ( !err ) {
console.log('Document created', data);
}
done(err, data);
});
},

deleteDocument: function(collection, docId, done) {
var url = initDB.baseUrl + collection + '/' + docId;
console.log("rest.del - " + url);
var request = rest.del(url, { qs: { apiKey:initDB.apiKey }, json: {} }, function(err, response, data) {
if ( !err ) {
console.log('Document deleted', data);
}
done(err, data);
});
},

addAdminUser: function(done) {
console.log('*** Admin user properties:', initDB.adminUser);
console.log('Checking that admin user does not exist...');
initDB.checkDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
if ( !err && data.length === 0 ) {
console.log('Creating new admin user...', err, data);
initDB.createDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
console.log('Created new admin user...');
console.log(err);
console.log(data);
done(err, data);
});
} else {
if (data.message) {
console.log('Error: ' + data.message);
} else {
console.log('User already created.');
}
done(err, data);
}
});
}
};

module.exports = initDB;

4 changes: 2 additions & 2 deletions server/test/mongo-initdb.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// NodeUnit tests for the initDB.js module that initializes the DB

var rewire = require('rewire');
var initDB = require('../initDB.js');
var config = require('../config.js');
var initDB = require('../lib/initDB');
var config = require('../config');

module.exports = {
setUp: function (callback) {
Expand Down

0 comments on commit 535a942

Please sign in to comment.