-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gen): include MongoDB as an option
When selected, sets up database with Mongoose. Replaces the static data returned from awesomeThings route with data from a query. Closes #2
- Loading branch information
Showing
8 changed files
with
142 additions
and
25 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
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
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,33 @@ | ||
'use strict'; | ||
<% if (!mongo) { %> | ||
exports.awesomeThings = function(req, res) { | ||
res.json([ | ||
'HTML5 Boilerplate', | ||
'AngularJS', | ||
'Karma', | ||
'Express' | ||
]); | ||
}; | ||
<% } %><% if (mongo) { %> | ||
var mongoose = require('mongoose'), | ||
Thing = mongoose.model('Thing'), | ||
async = require('async'); | ||
|
||
// Return a list of thing 'names' | ||
exports.awesomeThings = function(req, res) { | ||
return Thing.find(function (err, things) { | ||
if (!err) { | ||
var thingNames = []; | ||
|
||
async.each(things, function (thing, cb) { | ||
thingNames.push(thing.name); | ||
cb(); | ||
}, function (err) { | ||
return res.send(thingNames); | ||
}); | ||
} else { | ||
return res.send(err); | ||
} | ||
}); | ||
}; | ||
<% } %> |
This file was deleted.
Oops, something went wrong.
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,20 @@ | ||
'use strict'; | ||
|
||
var mongoose = require('mongoose'), | ||
Thing = mongoose.model('Thing'); | ||
|
||
// Create all of the dummy things if Thing is empty | ||
Thing.find(function (err, things) { | ||
if (things.length === 0) { | ||
console.log('populating database'); | ||
Thing.create( | ||
{ name : 'HTML5 Boilerplate', awesomeness: 10}, | ||
{ name : 'AngularJS', awesomeness: 10}, | ||
{ name : 'Karma', awesomeness: 10}, | ||
{ name : 'Express', awesomeness: 10}, | ||
{ name : 'Mongoose', awesomeness: 10}, function(err) { | ||
console.log('finished populating'); | ||
} | ||
); | ||
} | ||
}); |
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,22 @@ | ||
'use strict'; | ||
|
||
var mongoose = require('mongoose'); | ||
|
||
exports.mongoose = mongoose; | ||
|
||
// Configure for possible deployment | ||
var uristring = | ||
process.env.MONGOLAB_URI || | ||
process.env.MONGOHQ_URL || | ||
'mongodb://localhost/test'; | ||
|
||
var mongoOptions = { db: { safe: true } }; | ||
|
||
// Connect to Database | ||
mongoose.connect(uristring, mongoOptions, function (err, res) { | ||
if (err) { | ||
console.log ('ERROR connecting to: ' + uristring + '. ' + err); | ||
} else { | ||
console.log ('Successfully connected to: ' + uristring); | ||
} | ||
}); |
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,17 @@ | ||
'use strict'; | ||
|
||
var mongoose = require('mongoose'), | ||
Schema = mongoose.Schema; | ||
|
||
// Schema | ||
var ThingSchema = new Schema({ | ||
name: String, | ||
awesomeness: Number | ||
}); | ||
|
||
// Validations | ||
ThingSchema.path('awesomeness').validate(function (num) { | ||
return num >= 1 && num <= 10; | ||
}, 'Awesomeness must be between 1 and 10'); | ||
|
||
mongoose.model('Thing', ThingSchema); |
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