drupal-jsonapi-params icon indicating copy to clipboard operation
drupal-jsonapi-params copied to clipboard

Allow for parameterized queries.

Open gabesullice opened this issue 4 years ago • 1 comments
trafficstars

I think it would be a useful feature to create query objects which can be executed with replaced placeholders. This would allow developers to hardcode a query into their app, but allow users to make selections. For example, I might like to show a list of events filters by a date greater than the current date while allowing users to further restrict the list of events by a taxonomy term.

gabesullice avatar May 20 '21 16:05 gabesullice

Update: Even though not a good DX, one way this is possible right now is using the FilterFunc in qs library (read more about it here -> https://www.npmjs.com/package/qs).

Example usage:

  let api = new DrupalJsonApiParams();
  api.addFilter('changed', ['0', '@current_date'], 'BETWEEN');

  // ... somewhere else at some other time in code...
  let qsParam = api.getQsOption();
  const now = (Date.now() / 1000).toFixed();
  qsParam = {
    ...qsParam,
    filter: (key: string, value: any) => {
      // Following is just an example on how to replace if the key is known.
      // You can also replace after checking for some placeholders in the value as well.
      switch (key) {
        case 'filter[changed]':
          return now;
        default:
          return value;
      }
    },
  };
  const query = api.getQueryString(qsParam);

d34dman avatar Jan 06 '23 01:01 d34dman