Properties

attribute: ((attributeName) => Cast | JsonPath | AssociationPath | Attribute | DialectAwareFn)

Type declaration

    • (attributeName): Cast | JsonPath | AssociationPath | Attribute | DialectAwareFn
    • Used to represent the attribute of a model. You should use the attribute name, which will be mapped to the correct column name. This attribute name follows the same rules as the attribute names in POJO where options. As such, you can use dot notation to access nested JSON properties, and you can reference included associations.

      If you want to use a database name, without mapping, you can use Identifier.

      Parameters

      • attributeName: string

      Returns Cast | JsonPath | AssociationPath | Attribute | DialectAwareFn

      Example

      Let's say the class User has an attribute firstName, which maps to the column first_name.

      User.findAll({
      where: sql`${attribute('firstName')} = 'John'`
      });

      Will generate:

      SELECT * FROM users WHERE first_name = 'John'
      

      Example

      Let's say the class User has an attribute data, which is a JSON column.

      User.findAll({
      where: sql`${attribute('data.registered')} = 'true'`
      });

      Will generate (assuming the dialect supports JSON operators):

      SELECT * FROM users WHERE data->'registered' = 'true'
      
cast: ((val, type) => Cast)

Type declaration

    • (val, type): Cast
    • Creates an object representing a call to the cast function.

      Parameters

      • val: unknown

        The value to cast

      • type: DataType

        The type to cast it to

      Returns Cast

col: ((...identifiers) => Col)

Type declaration

    • (...identifiers): Col
    • Creates an object which represents a column in the DB, this allows referencing another column in your query. This is often useful in conjunction with fn, where and sql which interpret strings as values and not column names.

      Col works similarly to Identifier, but "*" has special meaning, for backwards compatibility.

      ⚠️ We recommend using Identifier, or Attribute instead.

      Parameters

      • Rest ...identifiers: string[]

        The name of the column

      Returns Col

fn: ((fnName, ...args) => Fn)

Type declaration

    • (fnName, ...args): Fn
    • Creates an object representing a database function. This can be used in search queries, both in where and order parts, and as default values in column definitions. If you want to refer to columns in your function, you should use Attribute (recommended), Identifier, or col (discouraged) otherwise the value will be interpreted as a string.

      ℹ️ This method is usually verbose and we recommend using the sql template string tag instead.

      Parameters

      • fnName: string

        The SQL function you want to call

      • Rest ...args: readonly unknown[]

        All further arguments will be passed as arguments to the function

      Returns Fn

      Example: Convert a user's username to upper case

      instance.update({
      username: fn('upper', col('username'))
      });
identifier: ((value) => Identifier)

Type declaration

    • (value): Identifier
    • Used to represent a value that will either be escaped to a literal, or a bind parameter. Unlike attribute and col, this identifier will be escaped as-is, without mapping to a column name or any other transformation.

      Parameters

      • value: string

      Returns Identifier

      Example

      sequelize.query(sql`SELECT * FROM users WHERE ${identifier('firstName')} = 'John'`);
      

      Will generate (identifier quoting depending on the dialect):

      SELECT * FROM users WHERE "firstName" = 'John'
      
jsonPath: ((expression, path) => JsonPath)

Type declaration

    • (expression, path): JsonPath
    • Use this to access nested properties in a JSON column. You can also use the dot notation with Attribute, but this works with any values, not just attributes.

      Parameters

      • expression: unknown

        The expression to access the property on.

      • path: readonly (string | number)[]

        The path to the property. If a number is used, it will be treated as an array index, otherwise as a key.

      Returns JsonPath

      Example

      sql`${jsonPath('data', ['name'])} = '"John"'`
      

      will produce

      -- postgres
      "data"->'name' = '"John"'
      -- sqlite, mysql, mariadb
      JSON_EXTRACT("data", '$.name') = '"John"'

      Example

      // notice here that 0 is a number, not a string. It will be treated as an array index.
      sql`${jsonPath('array', [0])}`

      will produce

      -- postgres
      "array"->0
      -- sqlite, mysql, mariadb
      JSON_EXTRACT(`array`, '$[0]')

      Example

      // notice here that 0 is a string, not a number. It will be treated as an object key.
      sql`${jsonPath('object', ['0'])}`

      will produce

      -- postgres
      "object"->'0'
      -- sqlite, mysql, mariadb
      JSON_EXTRACT(`object`, '$.0')
list: ((values) => List)

Type declaration

    • (values): List
    • Used to represent an SQL list of values, e.g. WHERE id IN (1, 2, 3). This ensure that the array is interpreted as an SQL list, and not as an SQL Array.

      Parameters

      • values: unknown[]

        The members of the list.

      Returns List

      Example

      sequelize.query(sql`SELECT * FROM users WHERE id IN ${list([1, 2, 3])}`);
      

      Will generate:

      SELECT * FROM users WHERE id IN (1, 2, 3)
      
literal: ((val) => Literal)

Type declaration

    • (val): Literal
    • Creates an object representing a literal, i.e. something that will not be escaped. We recommend using sql for a better DX.

      Parameters

      Returns Literal

unquote: ((...args) => DialectAwareFn)

Type declaration

uuidV1: SqlUuidV1
uuidV4: SqlUuidV4
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