1
\$\begingroup\$

I have a very simple Node application that routes requests to /users to the following file:

users.js

var express = require('express');
var router = express.Router();
var User = require('models/user');
var sequelizeSuccessHandler = require('lib/sequelizeSuccessHandler');
var sequelizeErrorHandler = require('lib/sequelizeErrorHandler');

router.get('/', function(request, response) {
  User.findAll()
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.post('/', function(request, response) {
  User.create(User.params(request.body))
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.get('/:id', function(request, response) {
  User.findOne({where: {id: request.params.id}})
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.patch('/:id', function(request, response) {
  User.findOne({where: {id: request.params.id}})
  .then(function(user) {
    return user.update(User.params(request.body));
  })
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.delete('/:id', function(request, response) {
  User.findOne({where: {id: request.params.id}})
  .then(function(user) {
    return user.destroy();
  })
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

module.exports = router;

lib/sequelizeSuccessHandler.js

var Sequelize = require('sequelize');

function successHandler(response) {
  return function(object) {
    response.send(JSON.stringify(object));
  };
}

module.exports = successHandler;

lib/sequelizeErrorHandler.js

var Sequelize = require('sequelize');

function errorHandler(response) {
  return function(error) {
    if (error.constructor == Sequelize.ValidationError) {
      response.status(400).send();
    } else {
      response.status(500).send();
    }
  };
}

module.exports = errorHandler;

Looking at users.js, you can see that I tried to DRY things up by creating sequelizeSuccessHandler and sequelizeErrorHandler (I hate those names), but there's still a lot of repetition. I feel like there is a better way to do this.

How can I clean this up?

\$\endgroup\$
  • \$\begingroup\$ the database queries should be done in a separate controller \$\endgroup\$ – Aaron Goldsmith May 2 '18 at 3:23
1
\$\begingroup\$

Looking at users.js, you can see that I tried to DRY things up by creating sequelizeSuccessHandler and sequelizeErrorHandler (I hate those names), but there's still a lot of repetition.

I suppose you're talking about these repeated lines in the route handlers:

  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));

I don't really know Node, but it seems to me that you could add another helper function:

function handle(query, response) {
  query
  .then(sequelizeSuccessHandler(response))
  .catch(sequelizeErrorHandler(response));
});

router.get('/', function(request, response) {
  handle(User.findAll(), response);
});

router.post('/', function(request, response) {
  handle(User.create(User.params(request.body)), response);
});

(The names "handle", and "query" might not be the best.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.