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

Allow update and delete to specify a condition #135

Open
ajusa opened this issue Jan 30, 2022 · 2 comments
Open

Allow update and delete to specify a condition #135

ajusa opened this issue Jan 30, 2022 · 2 comments

Comments

@ajusa
Copy link

ajusa commented Jan 30, 2022

Use case here is pretty common, let's say you've got a comment:

type Comment = ref object of Model
  text: string
  user: string

I'd like to be able to do something like

dbConn.update(comment, "user = ?", signedInUser)

It makes it much easier to ensure that updates are only happening to an item that a user owns/created. Right now I'm doing the checking manually, which results in additional select queries. Something similar already exists for count and sum, as they take additional conditions.

@PhilippMDoerner
Copy link
Collaborator

I'm not the project maintainer in any capacity, I'm just throwing this in from a typical webdevs perspective.

Architecturally speaking, validating whether an incoming HTTP Request has the necessary permission to do X is typically done in middlewares of the application before the controller of a given endpoint is even touched. Thus my first instinct would be to warn against granting this ability.

I assume count and sum (and other aggregatation procs I'd wager, if there are further ones)have the ability to add conditions solely so that you can filter precisely which entries you count and build sums from, for update that isn't specifically necessary as there's one specific entry you want to update with a specific set of values.

Feel free to counter me here, I'm interested whether there's a usecase I might be missing for this.

@ajusa
Copy link
Author

ajusa commented Mar 12, 2022

To clarify further:
This is just an example use case, I'm sure there are others that may want to update based on a condition. The reason I'm trying to avoid using middleware to check for permissions in this case is that it will result in two database queries being sent - one for the permissions check, and then one more to actually update the item. When dealing with a networked database (say postgres) where you may not have great latency (ie not in the same datacenter), those ms can add up. By avoiding the extra db call, in most cases latency can be cut in half.

@moigagoo moigagoo added this to Norm Jul 22, 2022
@moigagoo moigagoo moved this to To do in Norm Jul 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Candidates
Development

No branches or pull requests

2 participants