Skip to content

Commit

Permalink
feat(server): add support for sequelize
Browse files Browse the repository at this point in the history
Closes #414.
  • Loading branch information
sigriston committed Nov 26, 2014
1 parent b986998 commit 943120e
Show file tree
Hide file tree
Showing 43 changed files with 974 additions and 240 deletions.
55 changes: 48 additions & 7 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,38 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
this.log('\n# Server\n');

this.prompt([{
type: 'confirm',
name: 'mongoose',
message: 'Would you like to use mongoDB with Mongoose for data modeling?'
type: 'checkbox',
name: 'odms',
message: 'What would you like to use for data modeling?',
choices: [
{
value: 'mongoose',
name: 'Mongoose (MongoDB)',
checked: true
},
{
value: 'sequelize',
name: 'Sequelize (MySQL, SQLite, MariaDB, PostgreSQL)',
checked: false
}
]
}, {
type: 'list',
name: 'models',
message: 'What would you like to use for the default models?',
choices: [ 'Mongoose', 'Sequelize' ],
filter: function( val ) {
return val.toLowerCase();
},
when: function(answers) {
return answers.odms && answers.odms.length > 1;
}
}, {
type: 'confirm',
name: 'auth',
message: 'Would you scaffold out an authentication boilerplate?',
when: function (answers) {
return answers.mongoose;
return answers.odms && answers.odms.length !== 0;
}
}, {
type: 'checkbox',
Expand Down Expand Up @@ -183,15 +206,29 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
type: 'confirm',
name: 'socketio',
message: 'Would you like to use socket.io?',
// to-do: should not be dependent on mongoose
// to-do: should not be dependent on ODMs
when: function (answers) {
return answers.mongoose;
return answers.odms && answers.odms.length !== 0;
},
default: true
}], function (answers) {
if(answers.socketio) this.filters.socketio = true;
if(answers.mongoose) this.filters.mongoose = true;
if(answers.auth) this.filters.auth = true;
if(answers.odms.length > 0) {
var models;
if(!answers.models) {
models = answers.odms[0];
} else {
models = answers.models;
}
this.filters.models = true;
this.filters[models + 'Models'] = true;
answers.odms.forEach(function(odm) {
this.filters[odm] = true;
}.bind(this));
} else {
this.filters.noModels = true;
}
if(answers.oauth) {
if(answers.oauth.length) this.filters.oauth = true;
answers.oauth.forEach(function(oauthStrategy) {
Expand Down Expand Up @@ -265,6 +302,10 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
this.config.set('registerSocketsFile', 'server/config/socketio.js');
this.config.set('socketsNeedle', '// Insert sockets below');

this.config.set('insertModels', true);
this.config.set('registerModelsFile', 'server/sqldb/index.js');
this.config.set('modelsNeedle', '// Insert models below');

this.config.set('filters', this.filters);
this.config.forceSave();
},
Expand Down
8 changes: 5 additions & 3 deletions app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
"jade": "~1.2.0",<% } %><% if (filters.html) { %>
"ejs": "~0.8.4",<% } %><% if (filters.mongoose) { %>
"mongoose": "~3.8.8",
"mongoose-bird": "~0.0.1",<% } %><% if (filters.auth) { %>
"mongoose-bird": "~0.0.1",<% } %><% if (filters.sequelize) { %>
"sequelize": "^2.0.0-rc2",
"sqlite3": "~3.0.2",<% } %><% if (filters.auth) { %>
"jsonwebtoken": "^0.3.0",
"express-jwt": "^0.1.3",
"passport": "~0.2.0",
"passport-local": "~0.1.6",<% } %><% if (filters.facebookAuth) { %>
"passport-facebook": "latest",<% } %><% if (filters.twitterAuth) { %>
"passport-twitter": "latest",<% } %><% if (filters.googleAuth) { %>
"passport-google-oauth": "latest",<% } %>
"composable-middleware": "^0.3.0",
"connect-mongo": "^0.4.1"<% if (filters.socketio) { %>,
"composable-middleware": "^0.3.0"<% if (filters.mongoose) { %>,
"connect-mongo": "^0.4.1"<% } %><% if (filters.socketio) { %>,
"socket.io": "^1.0.6",
"socket.io-client": "^1.0.6",
"socketio-jwt": "^2.0.2"<% } %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ angular.module '<%= scriptAppName %>'
.catch (err) ->
err = err.data
$scope.errors = {}

<% if (filters.mongooseModels) { %>
# Update validity of form fields that match the mongoose errors
angular.forEach err.errors, (error, field) ->
form[field].$setValidity 'mongoose', false
$scope.errors[field] = error.message
$scope.errors[field] = error.message<% }
if (filters.sequelizeModels) { %>
# Update validity of form fields that match the sequelize errors
if err.name
angular.forEach err.fields, (field) ->
form[field].$setValidity 'mongoose', false
$scope.errors[field] = err.message<% } %>
<% if (filters.oauth) {%>
$scope.loginOauth = (provider) ->
$window.location.href = '/auth/' + provider<% } %>
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ angular.module('<%= scriptAppName %>')
.catch(function(err) {
err = err.data;
$scope.errors = {};

<% if (filters.mongooseModels) { %>
// Update validity of form fields that match the mongoose errors
angular.forEach(err.errors, function(error, field) {
form[field].$setValidity('mongoose', false);
$scope.errors[field] = error.message;
});
});<% }
if (filters.sequelizeModels) { %>
// Update validity of form fields that match the sequelize errors
if (err.name) {
angular.forEach(err.fields, function(field) {
form[field].$setValidity('mongoose', false);
$scope.errors[field] = err.message;
});
}<% } %>
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ angular.module '<%= scriptAppName %>'
$http.get('/api/things').success (awesomeThings) ->
$scope.awesomeThings = awesomeThings
<% if (filters.socketio) { %>socket.syncUpdates 'thing', $scope.awesomeThings<% } %>
<% if (filters.mongoose) { %>
<% if (filters.models) { %>
$scope.addThing = ->
return if $scope.newThing is ''
$http.post '/api/things',
Expand Down
2 changes: 1 addition & 1 deletion app/templates/client/app/main/main.controller(js).js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ angular.module('<%= scriptAppName %>')
$scope.awesomeThings = awesomeThings;<% if (filters.socketio) { %>
socket.syncUpdates('thing', $scope.awesomeThings);<% } %>
});
<% if (filters.mongoose) { %>
<% if (filters.models) { %>
$scope.addThing = function() {
if ($scope.newThing === '') {
return;
Expand Down
11 changes: 7 additions & 4 deletions app/templates/e2e/account(auth)/login/login.spec(jasmine).js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = protractor.getInstance().params;
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');
var config = protractor.getInstance().params;<% if (filters.mongooseModels) { %>
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');<% } %><% if (filters.sequelizeModels) { %>
var UserModel = require(config.serverConfig.root + '/server/sqldb').User;<% } %>

describe('Login View', function() {
var page;
Expand All @@ -18,9 +19,11 @@ describe('Login View', function() {
};

beforeEach(function(done) {
UserModel.removeAsync()
<% if (filters.mongooseModels) { %>UserModel.removeAsync()<% }
if (filters.sequelizeModels) { %>UserModel.destroy()<% } %>
.then(function() {
return UserModel.createAsync(testUser);
<% if (filters.mongooseModels) { %>return UserModel.createAsync(testUser);<% }
if (filters.sequelizeModels) { %>return UserModel.create(testUser);<% } %>
})
.then(loadPage)
.finally(done);
Expand Down
14 changes: 9 additions & 5 deletions app/templates/e2e/account(auth)/login/login.spec(mocha).js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = protractor.getInstance().params;
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');
var config = protractor.getInstance().params;<% if (filters.mongooseModels) { %>
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');<% } %><% if (filters.sequelizeModels) { %>
var UserModel = require(config.serverConfig.root + '/server/sqldb').User;<% } %>

describe('Login View', function() {
var page;
Expand All @@ -19,15 +20,18 @@ describe('Login View', function() {

before(function() {
return UserModel
.removeAsync()
<% if (filters.mongooseModels) { %>.removeAsync()<% }
if (filters.sequelizeModels) { %>.destroy()<% } %>
.then(function() {
return UserModel.createAsync(testUser);
<% if (filters.mongooseModels) { %>return UserModel.createAsync(testUser);<% }
if (filters.sequelizeModels) { %>return UserModel.create(testUser);<% } %>
})
.then(loadPage);
});

after(function() {
return UserModel.removeAsync();
<% if (filters.mongooseModels) { %>return UserModel.removeAsync();<% }
if (filters.sequelizeModels) { %>return UserModel.destroy();<% } %>
});

it('should include login form with correct inputs and submit button', function() {
Expand Down
11 changes: 7 additions & 4 deletions app/templates/e2e/account(auth)/logout/logout.spec(jasmine).js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = protractor.getInstance().params;
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');
var config = protractor.getInstance().params;<% if (filters.mongooseModels) { %>
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');<% } %><% if (filters.sequelizeModels) { %>
var UserModel = require(config.serverConfig.root + '/server/sqldb').User;<% } %>

describe('Logout View', function() {
var login = function(user) {
Expand All @@ -16,9 +17,11 @@ describe('Logout View', function() {
};

beforeEach(function(done) {
UserModel.removeAsync()
<% if (filters.mongooseModels) { %>UserModel.removeAsync()<% }
if (filters.sequelizeModels) { %>UserModel.destroy()<% } %>
.then(function() {
return UserModel.createAsync(testUser);
<% if (filters.mongooseModels) { %>return UserModel.createAsync(testUser);<% }
if (filters.sequelizeModels) { %>return UserModel.create(testUser);<% } %>
})
.then(function() {
return login(testUser);
Expand Down
14 changes: 9 additions & 5 deletions app/templates/e2e/account(auth)/logout/logout.spec(mocha).js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = protractor.getInstance().params;
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');
var config = protractor.getInstance().params;<% if (filters.mongooseModels) { %>
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');<% } %><% if (filters.sequelizeModels) { %>
var UserModel = require(config.serverConfig.root + '/server/sqldb').User;<% } %>

describe('Logout View', function() {
var login = function(user) {
Expand All @@ -17,17 +18,20 @@ describe('Logout View', function() {

beforeEach(function() {
return UserModel
.removeAsync()
<% if (filters.mongooseModels) { %>.removeAsync()<% }
if (filters.sequelizeModels) { %>.destroy()<% } %>
.then(function() {
return UserModel.createAsync(testUser);
<% if (filters.mongooseModels) { %>return UserModel.createAsync(testUser);<% }
if (filters.sequelizeModels) { %>return UserModel.create(testUser);<% } %>
})
.then(function() {
return login(testUser);
});
});

after(function() {
return UserModel.removeAsync();
<% if (filters.mongooseModels) { %>return UserModel.removeAsync();<% }
if (filters.sequelizeModels) { %>return UserModel.destroy();<% } %>
})

describe('with local auth', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = protractor.getInstance().params;
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');
var config = protractor.getInstance().params;<% if (filters.mongooseModels) { %>
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');<% } %><% if (filters.sequelizeModels) { %>
var UserModel = require(config.serverConfig.root + '/server/sqldb').User;<% } %>

describe('Signup View', function() {
var page;
Expand Down Expand Up @@ -35,7 +36,8 @@ describe('Signup View', function() {
describe('with local auth', function() {

it('should signup a new user, log them in, and redirecting to "/"', function(done) {
UserModel.remove(function() {
<% if (filters.mongooseModels) { %>UserModel.remove(function() {<% }
if (filters.sequelizeModels) { %>UserModel.destroy().then(function() {<% } %>
page.signup(testUser);

var navbar = require('../../components/navbar/navbar.po');
Expand Down
11 changes: 7 additions & 4 deletions app/templates/e2e/account(auth)/signup/signup.spec(mocha).js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = protractor.getInstance().params;
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');
var config = protractor.getInstance().params;<% if (filters.mongooseModels) { %>
var UserModel = require(config.serverConfig.root + '/server/api/user/user.model');<% } %><% if (filters.sequelizeModels) { %>
var UserModel = require(config.serverConfig.root + '/server/sqldb').User;<% } %>

describe('Signup View', function() {
var page;
Expand All @@ -22,7 +23,8 @@ describe('Signup View', function() {
});

after(function() {
return UserModel.removeAsync();
<% if (filters.mongooseModels) { %>return UserModel.removeAsync();<% }
if (filters.sequelizeModels) { %>return UserModel.destroy();<% } %>
});

it('should include signup form with correct inputs and submit button', function() {
Expand All @@ -39,7 +41,8 @@ describe('Signup View', function() {
describe('with local auth', function() {

it('should signup a new user, log them in, and redirecting to "/"', function(done) {
UserModel.remove(function() {
<% if (filters.mongooseModels) { %>UserModel.remove(function() {<% }
if (filters.sequelizeModels) { %>UserModel.destroy().then(function() {<% } %>
page.signup(testUser);

var navbar = require('../../components/navbar/navbar.po');
Expand Down
4 changes: 2 additions & 2 deletions app/templates/protractor.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ var config = {
);
<% } %>
var serverConfig = config.params.serverConfig;

<% if (filters.mongoose) { %>
// Setup mongo for tests
var mongoose = require('mongoose-bird')();
mongoose.connect(serverConfig.mongo.uri, serverConfig.mongo.options); // Connect to database
mongoose.connect(serverConfig.mongo.uri, serverConfig.mongo.options); // Connect to database<% } %>
}
};

Expand Down
2 changes: 1 addition & 1 deletion app/templates/server/api/thing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var controller = require('./thing.controller');

var router = express.Router();

router.get('/', controller.index);<% if (filters.mongoose) { %>
router.get('/', controller.index);<% if (filters.models) { %>
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.update);
Expand Down
6 changes: 3 additions & 3 deletions app/templates/server/api/thing/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
var proxyquire = require('proxyquire').noPreserveCache();

var thingCtrlStub = {
index: 'thingCtrl.index'<% if (filters.mongoose) { %>,
index: 'thingCtrl.index'<% if (filters.models) { %>,
show: 'thingCtrl.show',
create: 'thingCtrl.create',
update: 'thingCtrl.update',
destroy: 'thingCtrl.destroy'<% } %>
};

var routerStub = {
get: sinon.spy()<% if (filters.mongoose) { %>,
get: sinon.spy()<% if (filters.models) { %>,
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('Thing API Router:', function() {
.should.have.been.calledOnce;
});

});<% if (filters.mongoose) { %>
});<% if (filters.models) { %>

describe('GET /api/things/:id', function() {

Expand Down
Loading

0 comments on commit 943120e

Please sign in to comment.