-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.js
40 lines (33 loc) · 961 Bytes
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var mongoose = require('mongoose');
var cfenv = require("cfenv");
var Schema = mongoose.Schema;
var User = new Schema({
username: String,
password: String,
});
mongoose.model('User', User);
// Default Mongo URI is local
const DOCKER = process.env.DOCKER
if ( DOCKER === '1') {
var mongoUri = 'mongodb://goof-mongo/express-todo';
} else {
var mongoUri = 'mongodb://localhost/express-todo';
}
if (process.env.MONGOLAB_URI) {
// Generic (plus Heroku) env var support
mongoUri = process.env.MONGOLAB_URI;
}
console.log("Using Mongo URI " + mongoUri);
mongoose.connect(mongoUri);
User = mongoose.model('User');
User.find({ username: 'admin' }).exec(function (err, users) {
console.log(users);
if (users.length === 0) {
console.log('no admin');
new User({ username: 'admin', password: Math.random() }).save(function (err, user, count) {
if (err) {
console.log('error saving admin user');
}
});
}
});