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.
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 sql.fn, sql.where and sql which interpret strings as values and not column names.
Col works similarly to sql.identifier, but "*" has special meaning, for backwards compatibility.
⚠️ We recommend using sql.identifier, or sql.attribute instead.
The name of the column
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 sql.attribute (recommended), sql.identifier, or sql.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.
The SQL function you want to call
All further arguments will be passed as arguments to the function
Used to represent a value that will either be escaped to a literal, or a bind parameter. Unlike sql.attribute and sql.col, this identifier will be escaped as-is, without mapping to a column name or any other transformation.
This method supports strings, table structures, model classes (in which case the identifiers will be the model schema & table name), and model definitions (same behavior as model classes)
The identifiers to escape. Automatically joins them with a period (.
).
A version of Array.join, but for SQL expressions. Using Array.join directly would not work, because the end result would be a string, not a SQL expression.
The parts to join. Each part can be a SQL expression, or a value to escape.
A raw SQL string, or a SQL expression to separate each pair of adjacent elements of the array.
A SQL expression representing the concatenation of all parts, interspersed with the separator.
Use this to access nested properties in a JSON column. You can also use the dot notation with sql.attribute, but this works with any values, not just attributes.
The expression to access the property on.
The path to the property. If a number is used, it will be treated as an array index, otherwise as a key.
sql`${jsonPath('data', ['name'])} = '"John"'`
will produce
-- postgres
"data"->'name' = '"John"'
-- sqlite, mysql, mariadb
JSON_EXTRACT("data", '$.name') = '"John"'
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 sql.fn, sql.col, sql.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.
The left operand
The POJO containing the operators and the right operands
This version of where
is used to opt back into the POJO syntax. Useful in combination with sql.
The left operand
The operator to use (one of the different values available in the Op object)
The right operand
The template tag function used to easily create sql.literal.