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

Support params.query in update() #189

Merged
merged 2 commits into from
Mar 27, 2018

Conversation

TimNZ
Copy link
Contributor

@TimNZ TimNZ commented Jan 30, 2018

I have permissions hooks that add fields to params.query for all service operations to limit records that can be updated/retrieved to a filtered set by 'owner_id'.

update() is the only method that doesn't support params.query to augment the final query.

I understand why, since it is only supposed to act on a specific record by id, which should always be unique.

If you don't accept the PR I understand.

In the meantime I've created a custom Service class extended from the sequelize Service class and overridden update()

Also want to avoid doing a hook that has to query for the record first to do the check before an update, to avoid a double query.

I may implement feathers-cache at some point, but don't know how robust that is yet in scaling and multiple server instances.

@daffl
Copy link
Member

daffl commented Jan 30, 2018

Thank you for the pull request! Earlier versions of Feathers database adapters allowed to do this and it cause many issues with people replacing their entire collections when all they wanted to do was update one field.

What is a good use case where multi update does something that the already support multi patch doesn't (adding a test might also help to clarify 😄 )?

@TimNZ
Copy link
Contributor Author

TimNZ commented Jan 30, 2018

Yeah, I did the PR and then thought "hmm, I guess they'll want a test" 😄

Adding params.query support is not for multi update like Patch, as the id field is still enforced in Update.

The mods are to make it consistent with all the other methods, and to make it easier for hooks to enforce some additional filter on what can be updated.
i.e. a permissions framework that enforces a 'owner_id = req.user.id'

Avoids the need for an additional query by a before hook to do the check.

As I said, an edge case, and understand if you choose not to merge.
Not a major real world issue.

I've got my workaround and happy to continue using that.

@daffl
Copy link
Member

daffl commented Jan 30, 2018

Ah ok, if the constraint is still enforce that makes sense. A simple test would still be nice though.

@TimNZ
Copy link
Contributor Author

TimNZ commented Jan 30, 2018

The current Update doesn't enforce the id by testing for truthy, it is passed as is to _get(), which doesn't enforce it either - should I add a truthy check in Update () and throw an error if not provided?

@TimNZ
Copy link
Contributor Author

TimNZ commented Feb 6, 2018

@daffl Bump, re last comment to get this tidied away.

"The current Update doesn't enforce the id by testing for truthy, it is passed as is to _get(), which doesn't enforce it either -
should I add a truthy check in Update () and throw an error if not provided?"

@daffl
Copy link
Member

daffl commented Feb 7, 2018

Is that necessary and/or related to this PR? Like I mentioned before, a test would still be nice.

@TimNZ
Copy link
Contributor Author

TimNZ commented Feb 7, 2018

Never mind, reread the Service docs and see update() is meant to support id = null and support params.query, it was just not implemented in this package.

https://docs.feathersjs.com/api/services.html#updateid-data-params

I'll add tests

@schettino
Copy link

schettino commented Mar 26, 2018

Is this going to be merged? I have faced the same issue when setting a permission criteria inside a hook.

@TimNZ
Copy link
Contributor Author

TimNZ commented Mar 26, 2018

Forgot.

Added tests (which maybe need a more experienced person to tidy up) and fixed a bug (tests are useful).

@daffl
Copy link
Member

daffl commented Mar 27, 2018

Awesome, thanks for adding those tests. This looks good to me so I'll go ahead and publish as a minor release.

@daffl daffl merged commit de2874e into feathersjs-ecosystem:master Mar 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants