forked from angular-app/angular-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(initDB): initDB was not set up to run from the command line
Move the functionality into a lib file to aid testing.
- Loading branch information
1 parent
903d393
commit 535a942
Showing
3 changed files
with
77 additions
and
79 deletions.
There are no files selected for viewing
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
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() {}); |
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
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; | ||
|
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