Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(adapter-commons): Clarify adapter query filtering #2607

Merged
merged 2 commits into from
Apr 27, 2022

Conversation

daffl
Copy link
Member

@daffl daffl commented Apr 24, 2022

This pull request tries to address some inconsistencies and flexibility issues when implementing and using database adapter.

The problem

There are several issues that mention a lack of clarity when using the previous filterQuery method. What is a filter and what isn't? How are they customized? Another recurring problem is that you may not want strict query filtering when using a database adapter on the server.

Improvements

Remove filterQuery and add sanitizeQuery and sanitizeData

This pull request removes the filterQuery adapter base service method and adds a sanitizeQuery and sanitizeData method instead. sanitizeQuery applies the previous query filters but then returns them as a single query object and it is up to the adapter implementation what to do with that. sanitizeData does nothing by default but could be used to e.g. filter out protected fields (or things like $push for MongoDB).

Add $ prefixed unsafe internal service methods

It also changes the semantics of the _find, _get, _patch, _update and _remove internal (hook-less) service methods to sanitize the query and data and check if multi updates are allowed. It has been moved from the service methods so that it is easier to extend from a database adapter and implement the service methods you need instead of being limited by a type interface (also see #2605).

For better flexibility, it additionally introduces the $find, $get, $patch, $update and $remove unsafe internal service methods. This is what a database adapter now implements and it shouldn't do any query or data sanitization. Calling these methods directly lets you do any query the database adapter supports without having to explicitly declare allowed operators, filters or multi settings.

Clarify filters and operators

Finally all parts of this PR follow a more strict definition of filters and operators:

  • A filter is a special top level query property starting with a $. This can be used for pagination ($limit, $skip), modifying the result set (e.g. { $populate: true } etc.
  • An operator is a special property starting with a $ that can be used to query properties, e.g. { age: { $gte: 21 } }

This definition has also been reflected in the Querying API documentation.

The adapter settings have been updated and can now be used like this:

service({
  filters: {
    $populate: true,
    $ignoreCase: value => value === 'true' || value === true ? true : false
  },
  operators: [ '$eq' ] // allows `{ name: { $eq: 'David' } }`
});

Ideally I'd like to see a lot of this responsibility moving to using a query schema and resolvers instead which can handle all those complexities (type coercion, setting dynamic properties etc.) much more cleanly and securely but this is hopefully a step in a better direction.

…lters

BREAKING CHANGE: filterQuery will now more stricly separate between top
level filters and query property level operators.
@matiaslopezd
Copy link

matiaslopezd commented Apr 25, 2022

ref #1161

@daffl daffl changed the title fix(adapter-commons): Clean up semantics for adapter operators and filters fix(adapter-commons): Clarify adapter query filtering Apr 26, 2022
@daffl daffl merged commit 2dac771 into dove Apr 27, 2022
@daffl daffl deleted the filter-operator-semantics branch April 27, 2022 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment