javascriptassociationssequelize.jsepilogue

Using epilogue, is it possible to get back a resource without associations?


I have

epilogue.resource({
  model: db.Question,
  endpoints: ['/api/questions', '/api/questions/:id'],
  associations: true
});

So when I hit /api/questions, I get back all the associations with the resources. Is there something I can pass to not get the associations in certain cases? Or should I create a new endpoint:

epilogue.resource({
  model: db.Question,
  endpoints: ['/api/questions2', '/api/questions2/:id']
});

Solution

  • One way of doing is by using milestones you can define milestone for list and read behaviour in certain case, you have access to req object so you can do changes accordingly

    https://github.com/dchester/epilogue#customize-behavior

    Here is an example of list call modification

    // my-middleware.js
    module.exports = {
      list: {
        write: {
          before: function(req, res, context) {
            // modify data before writing list data
            return context.continue;
          },
          action: function(req, res, context) {
            // change behavior of actually writing the data
            return context.continue;
          },
          after: function(req, res, context) {
            // set some sort of flag after writing list data
            return context.continue;
          }
        }
      }
    };
    
    // my-app.js
    var epilogue = require('epilogue'),
        restMiddleware = require('my-middleware');
    
    epilogue.initialize({
        app: app,
        sequelize: sequelize
    });
    
    var userResource = epilogue.resource({
        model: User,
        endpoints: ['/users', '/users/:id']
    });
    
    userResource.use(restMiddleware);