dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

Filtering on association field #203

Open fstn opened 7 years ago

fstn commented 7 years ago

Hi, Is there any way to filter on association column?

Sequelize equivalent:

Project.findAll({ include: [{ model: Task, where: {"name":"myTask"} }] })

this is what I want to query:

http://localhost/project?task.name=myTask

douglas-hirsh commented 7 years ago

I am interested in doing this too. I am wondering if there would be a place that we could inject the logic to handle this?

Thanks, Douglas

AkselsLedins commented 7 years ago

Something like that

  Project.use({
    list: {
      fetch: {
        before(request, response, context) {
          if (!isEmpty(request.query) && request.query.name) {
              context.include = [{
                model: Task,
                attributes: [ 'name' ],
                where: { name: request.query.name },
              }];
          }
          return context.continue;
        },
      },
    },
  });