dchester / epilogue

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

Url filters AND OR BETWEEN #199

Open wcodba opened 7 years ago

wcodba commented 7 years ago

Hi,

I understood if I request /resource/?name=["rob","henry"] i will get : where name in ("rob","henry")

what about chaining AND, OR, and BETWEEN ? I mean I've a field date, how to query thisdate between DATE1 and DATE2 ?

I there a full documentation on query parameters available ?

Thanks

sdebionne commented 7 years ago

I mean I've a field date, how to query thisdate between DATE1 and DATE2 ?

I have the exact same use case and after reading the documentation, it does not seem to be possible out of the box.

@dchester If you could point me in the right direction, I am willing to prepare a PR.

sdebionne commented 7 years ago

It ends up that emulating the $between operator is easy. I didn't realized that multiple search parameters could target the same attributes:

Given a model:

sequelize.define('timeserie', {
    day: {type: DataTypes.DATE},
    value: {type: DataTypes.REAL}
  });

And a resource:

resources.timeserie = epilogue.resource({
  model: models.timeserie,
  endpoints: ['/timeserie'],
  actions: ['list', 'read'],
  search: [
    {operator: '$gte', param: 'begin', attributes: [ 'day' ]},
    {operator: '$lte', param: 'end', attributes: [ 'day' ]}
  ] 
});

One can use /timeserie?begin=2017-01-01&end=2017-01-05 to get value BETWEEN end and begin.

wcodba commented 7 years ago

Yes ok but how to avoid to specify attributes: [ 'day' ] ? Goal is to deliver this resource for all endpoint and all fields, is it possible ?

Cheers.

zlatinejc commented 7 years ago

It is. It's called dynamic coding.

You should try it.