The left operand
The POJO containing the operators and the right operands
where(attribute('id'), { [Op.eq]: 1 });
where(attribute('id'), {
[Op.or]: {
[Op.eq]: 1,
[Op.gt]: 10,
},
});
This version of where
is used to opt back into the POJO syntax. Useful in combination with sql.
sequelize.query(sql`
SELECT * FROM users WHERE ${where({ id: 1 })};
`)
produces
SELECT * FROM users WHERE "id" = 1;
The left operand
The operator to use (one of the different values available in the Op object)
The right operand
where(col('id'), Op.eq, 1)
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
// Using a SQL function on the left operand.
// Equal to: WHERE LOWER(first_name) = 'lily'
where(fn('LOWER', col('first_name')), Op.eq, 'lily');
// Using raw SQL as the left operand.
// Equal to: WHERE 'Lily' = 'Lily'
where(literal(`'Lily'`), Op.eq, 'Lily');
Generated using TypeDoc
A way of writing an SQL binary operator, or more complex where conditions.
This solution is slightly more verbose than the POJO syntax, but allows any value on the left hand side of the operator (unlike the POJO syntax which only accepts attribute names). For instance, either the left or right hand side of the operator can be fn, col, literal etc.
If your left operand is an attribute name, using the regular POJO syntax (
{ where: { attrName: value }}
) syntax is usually more convenient.⚠️ Unlike the POJO syntax, if the left operand is a string, it will be treated as a value, not an attribute name. If you wish to refer to an attribute, use attribute instead.