When / Conditionals
Last updated
Last updated
If you store the builder object in a variable, you can use if
and else
statements like you would expect.
This works, but breaks chainability. To keep chainability you can use the when
helper method.
when
The when
helper is used to allow conditional statements when defining queries without using if statements and having to store temporary variables.
You can pass a third argument to be called in the else
case.
when
callbacks are automatically scoped and grouped. That means that if a where clause is added inside the callback with an OR
combinator the clauses will automatically be grouped (have parenthesis put around them.) You can disable this feature by passing withoutScoping = true
to the when
callback.
Name
Type
Required
Default
Description
condition
boolean
true
The condition to switch on.
onTrue
Function
true
The callback to execute if the condition is true. It is passed the builder
object as the only parameter.
onFalse
Function
false
function( q ) { return q; }
The callback to execute if the conditions is false. It is passed the builder
object as the only parameter.
withoutScoping
boolean
false
false
Flag to turn off the automatic scoping of where clauses during the callback.