Wednesday 26 April 2017

How to handle result filtering on a REST API endpoint?

I have a Node.js REST API, working directly with a MySQL database via node-mysql.

I'm looking for a way to filter the database results on an endpoint in the most performatic way.

Currently, I have an endpoint /api/v2/matches, where my API code queries the database and outputs the results as a JSON response.

I'm planning to use filters on this endpoint. e.g. /api/v2/matches?filter[team]=TeamId

What is the best way/most performatic way to filter those database results in my code?

Should I:

  • Query the database for everything and use Array.prototype.filter() or similar on my results array to filter the data properly.

  • Assemble my query string programatically, depending on what is passed on req.query.filters object and query the database.

Please keep in mind that my current query for this endpoint (the one that returns all results) has a few INNER JOINS going on. I'm using LIMIT 50 on this query as well.



via Rafael Veiga

No comments:

Post a Comment