Skip to content

Commit

Permalink
feat(gen): include MongoDB as an option
Browse files Browse the repository at this point in the history
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
DaftMonk committed Nov 7, 2013
1 parent f7568a3 commit 280cc84
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 25 deletions.
27 changes: 26 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ Generator.prototype.askForModules = function askForModules() {
}.bind(this));
};

Generator.prototype.askForMongo = function askForMongo() {
var cb = this.async();

this.prompt([{
type: 'confirm',
name: 'mongo',
message: 'Would you like to include MongoDB with Mongoose?',
default: false
}], function (props) {
this.mongo = props.mongo;

cb();
}.bind(this));
};

Generator.prototype.readIndex = function readIndex() {
this.indexFile = this.engine(this.read('../../templates/common/index.html'), this);
};
Expand Down Expand Up @@ -261,5 +276,15 @@ Generator.prototype.packageFiles = function () {

Generator.prototype.serverFiles = function () {
this.template('../../templates/express/server.js', 'server.js');
this.template('../../templates/express/lib/routes/api.js', 'lib/routes/api.js');
this.template('../../templates/express/api.js', 'lib/controllers/api.js');
};

Generator.prototype.mongoFiles = function () {
if (!this.mongo) {
return; // Skip if disabled.
}

this.template('../../templates/express/mongo/mongo.js', 'lib/db/mongo.js');
this.template('../../templates/express/mongo/dummydata.js', 'lib/db/dummydata.js');
this.template('../../templates/express/mongo/thing.js', 'lib/models/thing.js');
};
4 changes: 3 additions & 1 deletion templates/common/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "<%= _.slugify(appname) %>",
"version": "0.0.0",
"dependencies": {
"express": "~3.4.3"
"express": "~3.4.3"<% if (mongo) { %>,
"mongoose": "~3.5.5",
"async": "~0.2.9"<% } %>
},
"devDependencies": {
"grunt": "~0.4.1",
Expand Down
33 changes: 33 additions & 0 deletions templates/express/api.js
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);
}
});
};
<% } %>
10 changes: 0 additions & 10 deletions templates/express/lib/routes/api.js

This file was deleted.

20 changes: 20 additions & 0 deletions templates/express/mongo/dummydata.js
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');
}
);
}
});
22 changes: 22 additions & 0 deletions templates/express/mongo/mongo.js
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);
}
});
17 changes: 17 additions & 0 deletions templates/express/mongo/thing.js
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);
34 changes: 21 additions & 13 deletions templates/express/server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
'use strict';

/**
* Module dependencies.
*/

// Module dependencies.
var express = require('express'),
http = require('http'),
path = require('path'),
api = require('./lib/routes/api');
path = require('path')<% if (mongo) { %>,
fs = require('fs')<% } %>;

var app = express();
<% if (mongo) { %>
// Connect to database
var db = require('./lib/db/mongo');

// Bootstrap models
var modelsPath = path.join(__dirname, 'lib/models');
fs.readdirSync(modelsPath).forEach(function (file) {
require(modelsPath + '/' + file);
});

// Configuration
// Populate empty DB with dummy data
require('./lib/db/dummydata');
<% } %>
// Controllers
var api = require('./lib/controllers/api');

// Express Configuration
app.configure(function(){
app.use(express.logger('dev'));
app.use(express.bodyParser());
Expand All @@ -24,20 +34,18 @@ app.configure('development', function(){
app.use(express.static(path.join(__dirname, '.tmp')));
app.use(express.static(path.join(__dirname, 'app')));
app.use(express.errorHandler());
}
});

app.configure('production', function(){
app.use(express.favicon(path.join(__dirname, 'public/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
}
});

// Routes

app.get('/api/awesomeThings', api.awesomeThings);

// Start server

var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Express server listening on port %d in %s mode', app.address().port, app.get('env'));
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});

0 comments on commit 280cc84

Please sign in to comment.