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
Add when to query builder #277
Comments
Is it like a chain of where methods? |
yeah it can be chained. Its just if the first parameter is truthy then run the callback in the second parameter. Would look something like this: # QueryBuilder
def when(conditional, callback):
if conditional:
callback(self)
return self |
A doubt, the attribute "age" in "when" method... is it evaluate from conditional from outside model or every line that checks every line from User model where age it's >= 18 and in case truthy then run callback method? |
No its just a normal conditional. Maybe a more obvious use case is: def show(self, request: Request):
User.when(self.request.input('age') >= 18, lambda q: q.where('age_restricted', 1)).get() Its just true or false, removing the variables will just look like this: User.when(True, lambda q: q.where('active', 1)).get()
# SELECT * FROM `users` WHERE `active` = '1'
User.when(False, lambda q: q.where('active', 1)).get()
# SELECT * FROM `users` |
We should be able to specify a
when
method that receives a conditional and then optionally calls the lambda or function callback:Example:
The text was updated successfully, but these errors were encountered: