title: How to use bookshelf for filtering the data based on parameters passed in query string in API url?
body: <p>I have created the REST API in Express using Bookshelf which fetches the list of all contributors as JSON</p>
  
  <p>Say API : </p>
  
  <pre><code>GET example.com/contributors
  </code></pre>
  
  <p>I need to make the filter and pagination for the API, 
  The filter options can be any </p>
  
  <pre><code>GET example.com/contributors?status=1&amp;type=pro
  </code></pre>
  
  <p>So it will return all the data whose status =1 and type = 'pro', </p>
  
  <p>I am able to get this filtered data using <code>where</code> clause </p>
  
  <pre><code>Model.Contributors.forge()
  .query({where: {status: query_params.status, type: query_params.type}})
  .fetch()
  </code></pre>
  
  <p><strong>What I am looking ?</strong></p>
  
  <p>But here the 'where' key values have to be specified manually. 
  As these filters are not always fixed but depends upon the condition client want to put based on the field the data need to be filtered, how can this structure be implemented in express node api using bookshelf?</p>
  
  <p>or</p>
  
  <p>Is this right approach for REST API to get the filter data or some other way it need to be implemented as per REST  API best practices?</p>
  
  <p><strong>Code</strong> </p>
  
  <p><strong>routes/contributors.js</strong></p>
  
  <pre><code>var Contributor_Service = require('./../services/contributor-service');
  
  var express = require('express');
  var router = express.Router();
  
      /* GET all the contributors listing. */
      router.get('/', function (req, res, next) {
          Contributor_Service.listAllContributors(req.query, function (err, data) {
              if (err) {
                  res.status(500).json({error: true, data: {message: err.message}});
              } else {
                  res.json({error: false, data: data.toJSON()});
              }
          });
  
      });
  
  module.exports = router;
  </code></pre>
  
  <p><strong>services/contributor-service.js</strong></p>
  
  <pre><code>var Model = require('./../models/Contributor');
  var express = require('express');
  
  var listAllContributors = function ( query_params, callback) {
  
      // for (var x in query_params) {
         // console.log(query_params[x]);
         // console.log(x);
      //}
      Model.Contributors
          .forge()
           .query({where: {status: query_params.status, type: query_params.type}})
          .fetch()
          .then(function (collection) {
              return callback(null, collection);
          })
          .catch(function (err) {
              return callback(err, null);
          });
  };
  </code></pre>
  
  <p><strong>models/Contributor.js</strong></p>
  
  <pre><code>var Bookshelf = require('../../dbconfig').bookshelf;
  
  var Contributor = Bookshelf.Model.extend({
      tableName: 'users'
  });
  var Contributors = Bookshelf.Collection.extend({
      model: Contributor
  });
  
  module.exports = {
      Contributor: Contributor,
      Contributors: Contributors
  };
  </code></pre>