4

I have a simple model object with startDate and endDate properties.

I can not seem to query with both 'and/or' and 'between' using the REST api. I would like to query 'between' a startDate range 'or' an endDate range. I would have thought that the following query would have worked:

?filter[where][or][0][event.startDate][between][0]=2017-01-15&filter[where][or][0][event.startDate][between][1]=2017-01-22&filter[where][or][1][event.endDate][between][0]=2017-01-15&filter[where][or][1][event.endDate][between][1]=2017-01-22

Ideally I would have liked to query between event.startDate and event.endDate but loopback does not like the following query:

?filter[where][event.startDate][between][0]=2017-01-15&filter[where][event.endDate][between][1]=2017-01-22

The queries work independently just fine:

?filter[where][event.startDate][between][0]=2017-01-15&filter[where][event.startDate][between][1]=2017-01-22

?filter[where][event.endDate][between][0]=2017-01-15&filter[where][event.endDate][between][1]=2017-01-22

Here is my model definition:


models/event.json

{
  "name": "event",
  "plural": "events",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": false
    },
    "startDate": {
      "type": "date",
      "required": false
    },
    "endDate": {
      "type": "date",
      "required": false
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}
Andy Hale
  • 41
  • 3

1 Answers1

3

I could not make the REST query work in that case either.

Ultimately, we had to resort to building the query via stringified JSON, see the docs

So, something like this

let filter = {
  where: {
    or: [
      {
        'event.startDate': {
          between: ['2017-01-15', '2017-01-22']
        }
      },
      {
        'event.endDate': {
          between: ['2017-01-15', '2017-01-22']
        }
      }
    ]
  }
};

let url = `/api?filter=${JSON.stringify(filter)}`;

It also made query building a lot simpler, actually.

Valentin Klinghammer
  • 1,319
  • 1
  • 13
  • 19