diff --git a/endpoint/templates/index.js b/endpoint/templates/index.js index 3f8c592a6..60198d142 100644 --- a/endpoint/templates/index.js +++ b/endpoint/templates/index.js @@ -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); diff --git a/endpoint/templates/index.spec.js b/endpoint/templates/index.spec.js index ccd15ec7e..291a04662 100644 --- a/endpoint/templates/index.spec.js +++ b/endpoint/templates/index.spec.js @@ -3,7 +3,7 @@ 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', @@ -11,7 +11,7 @@ var <%= cameledName %>CtrlStub = { }; var routerStub = { - get: sinon.spy()<% if(filters.mongoose) { %>, + get: sinon.spy()<% if(filters.models) { %>, put: sinon.spy(), patch: sinon.spy(), post: sinon.spy(), @@ -42,7 +42,7 @@ describe('<%= classedName %> API Router:', function() { .should.have.been.calledOnce; }); - });<% if(filters.mongoose) { %> + });<% if(filters.models) { %> describe('GET <%= route %>/:id', function() { diff --git a/endpoint/templates/name.controller.js b/endpoint/templates/name.controller.js index 7d03a42ee..abcc5f34b 100644 --- a/endpoint/templates/name.controller.js +++ b/endpoint/templates/name.controller.js @@ -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; @@ -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); } }; } @@ -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; }); }; @@ -42,7 +55,8 @@ 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(); }); @@ -50,44 +64,61 @@ function removeEntity(res) { }; }<% } %> -// 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)); diff --git a/endpoint/templates/name.integration.js b/endpoint/templates/name.integration.js index a377456a4..160cdedfa 100644 --- a/endpoint/templates/name.integration.js +++ b/endpoint/templates/name.integration.js @@ -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 %>;<% } %> @@ -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) { @@ -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) diff --git a/endpoint/templates/name.model(mongoose).js b/endpoint/templates/name.model(mongooseModels).js similarity index 100% rename from endpoint/templates/name.model(mongoose).js rename to endpoint/templates/name.model(mongooseModels).js diff --git a/endpoint/templates/name.model(sequelize).js b/endpoint/templates/name.model(sequelizeModels).js similarity index 100% rename from endpoint/templates/name.model(sequelize).js rename to endpoint/templates/name.model(sequelizeModels).js