where: {
    (leftOperand, whereAttributeHashValue): Where;
    (whereOptions): Where;
    (leftOperand, operator, rightOperand): Where;
}

Type declaration

    • (leftOperand, whereAttributeHashValue): Where
    • 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.

      Parameters

      • leftOperand: unknown

        The left operand

      • whereAttributeHashValue: any

        The POJO containing the operators and the right operands

      Returns Where

      Example

      where(attribute('id'), { [Op.eq]: 1 });
      where(attribute('id'), {
      [Op.or]: {
      [Op.eq]: 1,
      [Op.gt]: 10,
      },
      });
    • (whereOptions): Where
    • This version of where is used to opt back into the POJO syntax. Useful in combination with sql.

      Parameters

      Returns Where

      Example

      sequelize.query(sql`
      SELECT * FROM users WHERE ${where({ id: 1 })};
      `)

      produces

      SELECT * FROM users WHERE "id" = 1;
      
    • (leftOperand, operator, rightOperand): Where
    • Parameters

      • leftOperand: unknown

        The left operand

      • operator: keyof WhereOperators<any>

        The operator to use (one of the different values available in the Op object)

      • rightOperand: unknown

        The right operand

      Returns Where

      Example

      where(col('id'), Op.eq, 1)
      

      Example

      // Using a column name as the left operand.
      // Equal to: WHERE first_name = 'Lily'
      where(col('first_name'), Op.eq, 'Lily');

      Example

      // Using a SQL function on the left operand.
      // Equal to: WHERE LOWER(first_name) = 'lily'
      where(fn('LOWER', col('first_name')), Op.eq, 'lily');

      Example

      // Using raw SQL as the left operand.
      // Equal to: WHERE 'Lily' = 'Lily'
      where(literal(`'Lily'`), Op.eq, 'Lily');

Generated using TypeDoc