Dear monks,

I would like to hear your thoughts on how to approach complex filtering queries in a REST API.

I've built in the past an internal company REST API in Catalyst using Catalyst::Controller::REST and DBIx::Class for database access. At the time there was little need for filtering on the request except for some trivial cases like GET http://../collection/?name=some_value or GET http://../collection/?date_from=20200101&date_to=20201231. Those cases I've built with a naive approach to translate the parameters towards valid search queries like $c->model(..)->search([shoot_date => ['-and', {'>=', $shoot_date_from},{'<=', $shoot_date_to} ]]);

Now the question has been asked to foresee more complex cases of filtering where several criteria can be combined. So, I was wondering what is a good strategy to build a more flexible way of encoding a filter query in the GET request and translate it into the appropriate SQL statements.

I've been searching a bit and i find some stuff on building flexible sql queries in DBIx::Class with SQL::Abstract. And if find some strategies to encode queries into an uri using for example Apache Lucene syntax, for exeample GET http://../collection?q=name:(value1 value2 value3) and date:[20200101 TO 20201231], and validate and translate the query by using Lucene::QueryParser. But I don't find anything that connects the two sides.

So, are there any good approaches to encode a flexible query into an uri, easily decode the query and encode it into a SQL statement usable to retrieve the records?

Kind regards


In reply to Complex filter queries in a REST API by martell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.