forked from vmware-archive/cf-workshop-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
56 lines (50 loc) · 2.37 KB
/
mongo.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
var mongoose = require('mongoose');
// The attendee object schema
var attendeeSchema = new mongoose.Schema({
firstname: String,
lastname: String,
address: String,
city: String,
state: String,
zipcode: String,
email: String,
sessions: [{
name: String,
date: String,
completed: Boolean
}]
});
var attendee = mongoose.model('Attendee', attendeeSchema);
var vcap_services = JSON.parse(process.env.VCAP_SERVICES || '{}');
//Change this to your service type name
var serviceTypeName = 'p-mongodb';
var uri = vcap_services != undefined && vcap_services[serviceTypeName] != undefined ? vcap_services[serviceTypeName][0].credentials.uri : 'localhost:27017/cf-workshop-node';
mongoose.connect(uri, function(err, res) {
if (err) {
console.log('Error connecting to URI: ' + uri + ". " + err);
} else {
console.log('Connected successfully to URI: ' + uri);
}
});
mongoose.connection.on('open', function() {
console.log("Connected successfully; testing to see if data needs to be seeded...");
attendee.find(function(e, attendees, count) {
if (!attendees || attendees.length < 1) {
console.log("No data. Seeding sample data...");
var newAttendee = new attendee({"firstname": "Brian", "lastname": "Jimerson", "address": "123 Main St.", "city": "Akron", "state": "OH", "zipcode": "44313", "email": "[email protected]", "sessions": [ { "name": "Session 1", "date": "2014-09-01", "completed": true }, { "name": "Session 2", "date": "2014-08-06", "completed": false}]});
newAttendee.save(function(err) {
if (err) console.log("Error saving attendee 1: " + err);
});
newAttendee = new attendee({"firstname": "Sally", "lastname": "Struthers", "address": "456 Oak St.", "city": "Akron", "state": "OH", "zipcode": "44313", "email": "[email protected]", "sessions": [ ]});
newAttendee.save(function(err) {
if (err) console.log("Error saving attendee 1: " + err);
});
newAttendee = new attendee({"firstname": "John", "lastname": "Doe", "address": "1111 Peach St.", "city": "Akron", "state": "OH", "zipcode": "44313", "email": "[email protected]", "sessions": [ ]});
newAttendee.save(function(err) {
if (err) console.log("Error saving attendee 1: " + err);
});
} else {
console.log("Existing data found. Not seeding sample data...");
}
});
});