forked from snyk-labs/nodejs-goof
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mongoose-db.js
58 lines (47 loc) · 1.37 KB
/
mongoose-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
49
50
51
52
53
54
55
56
57
58
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
const DOCKER = process.env.DOCKER
if (DOCKER === '1') {
var mongoUri = 'mongodb://goof-mongo/express-todo';
} else {
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;
} else if (process.env.MONGODB_URI) {
// Generic (plus Heroku) env var support
mongoUri = process.env.MONGODB_URI;
}
console.log("Using Mongo URI " + mongoUri);
mongoose.connect(mongoUri);
User = mongoose.model('User');
User.find({ username: '[email protected]' }).exec(function (err, users) {
console.log(users);
if (users.length === 0) {
console.log('no admin');
new User({ username: '[email protected]', password: 'SuperSecretPassword' }).save(function (err, user, count) {
if (err) {
console.log('error saving admin user');
}
});
}
});