Skip to content

Commit 280cc84

Browse files
committed
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
1 parent f7568a3 commit 280cc84

File tree

8 files changed

+142
-25
lines changed

8 files changed

+142
-25
lines changed

Diff for: app/index.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ Generator.prototype.askForModules = function askForModules() {
160160
}.bind(this));
161161
};
162162

163+
Generator.prototype.askForMongo = function askForMongo() {
164+
var cb = this.async();
165+
166+
this.prompt([{
167+
type: 'confirm',
168+
name: 'mongo',
169+
message: 'Would you like to include MongoDB with Mongoose?',
170+
default: false
171+
}], function (props) {
172+
this.mongo = props.mongo;
173+
174+
cb();
175+
}.bind(this));
176+
};
177+
163178
Generator.prototype.readIndex = function readIndex() {
164179
this.indexFile = this.engine(this.read('../../templates/common/index.html'), this);
165180
};
@@ -261,5 +276,15 @@ Generator.prototype.packageFiles = function () {
261276

262277
Generator.prototype.serverFiles = function () {
263278
this.template('../../templates/express/server.js', 'server.js');
264-
this.template('../../templates/express/lib/routes/api.js', 'lib/routes/api.js');
279+
this.template('../../templates/express/api.js', 'lib/controllers/api.js');
265280
};
281+
282+
Generator.prototype.mongoFiles = function () {
283+
if (!this.mongo) {
284+
return; // Skip if disabled.
285+
}
286+
287+
this.template('../../templates/express/mongo/mongo.js', 'lib/db/mongo.js');
288+
this.template('../../templates/express/mongo/dummydata.js', 'lib/db/dummydata.js');
289+
this.template('../../templates/express/mongo/thing.js', 'lib/models/thing.js');
290+
};

Diff for: templates/common/_package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "<%= _.slugify(appname) %>",
33
"version": "0.0.0",
44
"dependencies": {
5-
"express": "~3.4.3"
5+
"express": "~3.4.3"<% if (mongo) { %>,
6+
"mongoose": "~3.5.5",
7+
"async": "~0.2.9"<% } %>
68
},
79
"devDependencies": {
810
"grunt": "~0.4.1",

Diff for: templates/express/api.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
<% if (!mongo) { %>
3+
exports.awesomeThings = function(req, res) {
4+
res.json([
5+
'HTML5 Boilerplate',
6+
'AngularJS',
7+
'Karma',
8+
'Express'
9+
]);
10+
};
11+
<% } %><% if (mongo) { %>
12+
var mongoose = require('mongoose'),
13+
Thing = mongoose.model('Thing'),
14+
async = require('async');
15+
16+
// Return a list of thing 'names'
17+
exports.awesomeThings = function(req, res) {
18+
return Thing.find(function (err, things) {
19+
if (!err) {
20+
var thingNames = [];
21+
22+
async.each(things, function (thing, cb) {
23+
thingNames.push(thing.name);
24+
cb();
25+
}, function (err) {
26+
return res.send(thingNames);
27+
});
28+
} else {
29+
return res.send(err);
30+
}
31+
});
32+
};
33+
<% } %>

Diff for: templates/express/lib/routes/api.js

-10
This file was deleted.

Diff for: templates/express/mongo/dummydata.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var mongoose = require('mongoose'),
4+
Thing = mongoose.model('Thing');
5+
6+
// Create all of the dummy things if Thing is empty
7+
Thing.find(function (err, things) {
8+
if (things.length === 0) {
9+
console.log('populating database');
10+
Thing.create(
11+
{ name : 'HTML5 Boilerplate', awesomeness: 10},
12+
{ name : 'AngularJS', awesomeness: 10},
13+
{ name : 'Karma', awesomeness: 10},
14+
{ name : 'Express', awesomeness: 10},
15+
{ name : 'Mongoose', awesomeness: 10}, function(err) {
16+
console.log('finished populating');
17+
}
18+
);
19+
}
20+
});

Diff for: templates/express/mongo/mongo.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var mongoose = require('mongoose');
4+
5+
exports.mongoose = mongoose;
6+
7+
// Configure for possible deployment
8+
var uristring =
9+
process.env.MONGOLAB_URI ||
10+
process.env.MONGOHQ_URL ||
11+
'mongodb://localhost/test';
12+
13+
var mongoOptions = { db: { safe: true } };
14+
15+
// Connect to Database
16+
mongoose.connect(uristring, mongoOptions, function (err, res) {
17+
if (err) {
18+
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
19+
} else {
20+
console.log ('Successfully connected to: ' + uristring);
21+
}
22+
});

Diff for: templates/express/mongo/thing.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var mongoose = require('mongoose'),
4+
Schema = mongoose.Schema;
5+
6+
// Schema
7+
var ThingSchema = new Schema({
8+
name: String,
9+
awesomeness: Number
10+
});
11+
12+
// Validations
13+
ThingSchema.path('awesomeness').validate(function (num) {
14+
return num >= 1 && num <= 10;
15+
}, 'Awesomeness must be between 1 and 10');
16+
17+
mongoose.model('Thing', ThingSchema);

Diff for: templates/express/server.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
'use strict';
22

3-
/**
4-
* Module dependencies.
5-
*/
6-
3+
// Module dependencies.
74
var express = require('express'),
8-
http = require('http'),
9-
path = require('path'),
10-
api = require('./lib/routes/api');
5+
path = require('path')<% if (mongo) { %>,
6+
fs = require('fs')<% } %>;
117

128
var app = express();
9+
<% if (mongo) { %>
10+
// Connect to database
11+
var db = require('./lib/db/mongo');
12+
13+
// Bootstrap models
14+
var modelsPath = path.join(__dirname, 'lib/models');
15+
fs.readdirSync(modelsPath).forEach(function (file) {
16+
require(modelsPath + '/' + file);
17+
});
1318

14-
// Configuration
19+
// Populate empty DB with dummy data
20+
require('./lib/db/dummydata');
21+
<% } %>
22+
// Controllers
23+
var api = require('./lib/controllers/api');
1524

25+
// Express Configuration
1626
app.configure(function(){
1727
app.use(express.logger('dev'));
1828
app.use(express.bodyParser());
@@ -24,20 +34,18 @@ app.configure('development', function(){
2434
app.use(express.static(path.join(__dirname, '.tmp')));
2535
app.use(express.static(path.join(__dirname, 'app')));
2636
app.use(express.errorHandler());
27-
}
37+
});
2838

2939
app.configure('production', function(){
3040
app.use(express.favicon(path.join(__dirname, 'public/favicon.ico')));
3141
app.use(express.static(path.join(__dirname, 'public')));
32-
}
42+
});
3343

3444
// Routes
35-
3645
app.get('/api/awesomeThings', api.awesomeThings);
3746

3847
// Start server
39-
4048
var port = process.env.PORT || 3000;
4149
app.listen(port, function () {
42-
console.log('Express server listening on port %d in %s mode', app.address().port, app.get('env'));
50+
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
4351
});

0 commit comments

Comments
 (0)