Skip to content

Commit

Permalink
fix(endpoint): fully support sequelize models
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcody committed Jul 5, 2015
1 parent f4f0b5a commit df82d17
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion endpoint/templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var controller = require('./<%= name %>.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 endpoint/templates/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 <%= cameledName %>CtrlStub = {
index: '<%= name %>Ctrl.index'<% if(filters.mongoose) { %>,
index: '<%= name %>Ctrl.index'<% if(filters.models) { %>,
show: '<%= name %>Ctrl.show',
create: '<%= name %>Ctrl.create',
update: '<%= name %>Ctrl.update',
destroy: '<%= name %>Ctrl.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('<%= classedName %> API Router:', function() {
.should.have.been.calledOnce;
});

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

describe('GET <%= route %>/:id', function() {

Expand Down
71 changes: 51 additions & 20 deletions endpoint/templates/name.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
'use strict';<% if(filters.mongoose) { %>
/**
* Using Rails-like standard naming convention for endpoints.
* GET <%= route %> -> index<% if (filters.models) { %>
* POST <%= route %> -> create
* GET <%= route %>/:id -> show
* PUT <%= route %>/:id -> update
* DELETE <%= route %>/:id -> destroy<% } %>
*/

var _ = require('lodash');
var <%= classedName %> = require('./<%= name %>.model');
'use strict';<% if (filters.models) { %>

var _ = require('lodash');<% if (filters.mongooseModels) { %>
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
var sqldb = require('../../sqldb');
var <%= classedName %> = sqldb.<%= classedName %>;<% } %>

function handleError(res, statusCode) {
statusCode = statusCode || 500;
Expand All @@ -14,7 +25,7 @@ function responseWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if (entity) {
return res.status(statusCode).json(entity);
res.status(statusCode).json(entity);
}
};
}
Expand All @@ -31,9 +42,11 @@ function handleEntityNotFound(res) {

function saveUpdates(updates) {
return function(entity) {
var updated = _.merge(entity, updates);
<% if (filters.mongooseModels) { %>var updated = _.merge(entity, updates);
return updated.saveAsync()
.spread(function(updated) {
.spread(function(updated) {<% }
if (filters.sequelizeModels) { %>return entity.updateAttributes(updates)
.then(function(updated) {<% } %>
return updated;
});
};
Expand All @@ -42,52 +55,70 @@ function saveUpdates(updates) {
function removeEntity(res) {
return function(entity) {
if (entity) {
return entity.removeAsync()
<% if (filters.mongooseModels) { %>return entity.removeAsync()<% }
if (filters.sequelizeModels) { %>return entity.destroy()<% } %>
.then(function() {
res.status(204).end();
});
}
};
}<% } %>

// Get list of <%= name %>s
exports.index = function(req, res) {<% if (!filters.mongoose) { %>
res.json([]);<% } %><% if (filters.mongoose) { %>
<%= classedName %>.findAsync()
// Gets a list of <%= name %>s
exports.index = function(req, res) {<% if (!filters.models) { %>
res.json([]);<% } else { %>
<% if (filters.mongooseModels) { %><%= classedName %>.findAsync()<% }
if (filters.sequelizeModels) { %><%= classedName %>.findAll()<% } %>
.then(responseWithResult(res))
.catch(handleError(res));<% } %>
};<% if (filters.mongoose) { %>
};<% if (filters.models) { %>

// Gets a single <%= name %> from the DB.
// Gets a single <%= name %> from the DB
exports.show = function(req, res) {
<%= classedName %>.findByIdAsync(req.params.id)
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
if (filters.sequelizeModels) { %><%= classedName %>.find({
where: {
_id: req.params.id
}
})<% } %>
.then(handleEntityNotFound(res))
.then(responseWithResult(res))
.catch(handleError(res));
};

// Creates a new <%= name %> in the DB.
// Creates a new <%= name %> in the DB
exports.create = function(req, res) {
<%= classedName %>.createAsync(req.body)
<% if (filters.mongooseModels) { %><%= classedName %>.createAsync(req.body)<% }
if (filters.sequelizeModels) { %><%= classedName %>.create(req.body)<% } %>
.then(responseWithResult(res, 201))
.catch(handleError(res));
};

// Updates an existing <%= name %> in the DB.
// Updates an existing <%= name %> in the DB
exports.update = function(req, res) {
if (req.body._id) {
delete req.body._id;
}
<%= classedName %>.findByIdAsync(req.params.id)
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
if (filters.sequelizeModels) { %><%= classedName %>.find({
where: {
_id: req.params.id
}
})<% } %>
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(responseWithResult(res))
.catch(handleError(res));
};

// Deletes a <%= name %> from the DB.
// Deletes a <%= name %> from the DB
exports.destroy = function(req, res) {
<%= classedName %>.findByIdAsync(req.params.id)
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
if (filters.sequelizeModels) { %><%= classedName %>.find({
where: {
_id: req.params.id
}
})<% } %>
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
Expand Down
6 changes: 3 additions & 3 deletions endpoint/templates/name.integration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var app = require('../../app');
var request = require('supertest');<% if(filters.mongoose) { %>
var request = require('supertest');<% if(filters.models) { %>

var new<%= classedName %>;<% } %>

Expand All @@ -28,7 +28,7 @@ describe('<%= classedName %> API:', function() {
<%= cameledName %>s.should.be.instanceOf(Array);
});

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

describe('POST <%= route %>', function() {
beforeEach(function(done) {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('<%= classedName %> API:', function() {
});
});

it('should respond with 404 when <%= name %> does not exsist', function(done) {
it('should respond with 404 when <%= name %> does not exist', function(done) {
request(app)
.delete('<%= route %>/' + new<%= classedName %>._id)
.expect(404)
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit df82d17

Please sign in to comment.