forked from aarlaud-snyk/goof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
48 lines (39 loc) · 1.13 KB
/
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
41
42
43
44
45
46
47
48
var mongoose = require('mongoose');
var cfenv = require("cfenv");
var Schema = mongoose.Schema;
var Todo = new Schema({
content: Buffer,
updated_at: Date,
});
mongoose.model('Todo', Todo);
var User = new Schema({
username: String,
password: String,
});
mongoose.model('User', User);
// CloudFoundry env vars
var mongoCFUri = cfenv.getAppEnv().getServiceURL('goof-mongo');
console.log(JSON.stringify(cfenv.getAppEnv()));
// Default Mongo URI is local
var mongoUri = 'mongodb://localhost/express-todo';
// CloudFoundry Mongo URI
if (mongoCFUri) {
mongoUri = mongoCFUri;
} else 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: 'SuperSecretPassword' }).save(function (err, user, count) {
if (err) {
console.log('error saving admin user');
}
});
}
});