Mockoon how to add filter to CRUD route

157 Views Asked by At

New to Mockoon. I have a data bucket like this:

[
    {
      "id": 1,
      "parentObjectServiceType": 14,
      "parentObjectId": 4,
      "title": "Note Title Public",
    },
    {
      "id": 2,
      "parentObjectServiceType": -1,
      "parentObjectId": -1,
      "title": "Note Title Private",
    }
]

It is connected to a CRUD route (called notes) so that I can do GET, PUT, POST, and DELETE calls automatically. In addition to this, I would like to filter out the GET using query strings such as:

/notes?parentObjectServiceType=14&parentObjectId=4

This should return the first element of the array above.

I am unable to do this. Any pointers will be appreciated.

1

There are 1 best solutions below

7
255kb - Mockoon On

[Update 2024-01-01]

It is now possible to filter on CRUD routes using query parameters and operators.

You can achieve what you want by doing something like this:

/notes?parentObjectServiceType_eq=14&parentObjectId_eq=4


This is not yet possible with CRUD routes. There is an opened issue that is on the roadmap though.

The alternative is to use a combination of templating helpers on a regular HTTP route, like you can see here:

{{{ stringify (oneOf (
    filter
      (dataRaw 'notes')
      (object type=(queryParamRaw 'parentObjectServiceType') )
  ))
}}}

But it's not ideal and you loose the rest of the CRUD features.