whereTyped
Description
whereTyped is a method that applies typesafe condition to a query.
Available on SelectQueryBuilder, UpdateQueryBuilder, DeleteQueryBuilder and SoftDeleteQueryBuilder (softDelete(), restore()), together with andWhereTyped and orWhereTyped. On UpdateQueryBuilder / DeleteQueryBuilder / SoftDeleteQueryBuilder the column isn't prefixed with an alias (UPDATE "users" … WHERE "firstName" = …), since UPDATE / DELETE statements have none. Properties are mapped to their database column names, so @Column({ name: 'first_name' }) firstName gives "user"."first_name". The same holds for whereIsInTyped and whereIsAnyTyped.
Signature
whereTyped(
selection: KeyProxyCallback<Entity>,
condition?: string | FindOperator<Value>,
params?: unknown,
options?: { addValue?: boolean }
): SelectQueryBuilder<Entity>
andWhereTyped(selection, condition?, params?, options?): SelectQueryBuilder<Entity>
orWhereTyped(selection, condition?, params?, options?): SelectQueryBuilder<Entity>
Arguments
- selection: KeyProxyCallback<Entity>: The callback which selects a field of entity. A callback that selects several columns (
u => [u.id, u.email]) throws aTypeError: add one condition per column, or use whereSearchTyped to search several columns. - condition?: string | FindOperator: Operator for the query, or a TypeORM find operator (see Find operators). A string may reference a key of
paramsas a placeholder (:keyor(:...keys)); then the value is not appended automatically. - params?: unknown: Parameter for the condition. This can be an object
{ someValue: 'value' }or the value itself'value'. ADate, aBufferor a typed array is a value too:whereTyped(u => u.createdAt, '>', since)bindssince. An object whose value goes after the condition holds exactly one key: with more, or none, it throws aTypeError(only the first value used to be bound). An object whose keys the condition names can have any number:whereTyped(u => u.id, 'BETWEEN :from AND :to', { from, to }). - options?: { addValue?: boolean }: Options for where logic.
- options.addValue?: boolean: The
addValueoption is controlling whether it will paste random uniq placeholder to condition (if true) or work as default requiring you to pass placeholder yourself (if false). Supports primitive values (placeholder:key), array (placeholder(:...key)) and a params object with one entry. Defaults totrue, or tofalsewhenconditionalready references a key ofparams(e.g.'LIKE :search'with{ search: '%John%' }).
Like TypeORM's where, whereTyped replaces previously set WHERE conditions; use andWhereTyped / orWhereTyped to add to them.
Find operators
The condition may be one of TypeORM's find operators instead of SQL, written as find({ where }) writes it, its values bound as parameters. It takes no params. The parameters get unique names (:op_…), not TypeORM's orm_param_0, orm_param_1… which each builder numbers from 0: a query can take the parameters of a subquery built on its own (.setParameters(sub.getParameters())) without the two overwriting each other.
import { Any, Equal, IsNull, MoreThanOrEqual, Not } from 'typeorm';
import { EqualColumn, IsDistinctFrom, IsFalse, IsIn, IsTrue } from 'typeorm-extensions';
query
.whereTyped((o) => o.deletedAt, IsNull()) // "o"."deletedAt" IS NULL
.andWhereTyped((o) => o.paidAt, Not(IsNull())) // NOT("o"."paidAt" IS NULL)
.andWhereTyped((o) => o.status, Equal(OrderStatus.Successful)) // "o"."status" = :op_k2x9q
.andWhereTyped((o) => o.status, IsIn([OrderStatus.Refunded])) // "o"."status" IN (:op_m4w1z)
.andWhereTyped((o) => o.type, Not(IsIn([OrderType.Gift]))) // NOT("o"."type" IN (:op_c8r3t))
.andWhereTyped((o) => o.createdAt, MoreThanOrEqual(since))
.andWhereTyped((o) => o.isGift, IsTrue()) // "o"."isGift" IS TRUE
.andWhereTyped((o) => o.isArchived, Not(IsTrue())) // NOT("o"."isArchived" IS TRUE): IS NOT TRUE, NULL too
.andWhereTyped((o) => o.id, Any(orderIds)) // "o"."id" = ANY(:op_p5n7h): one array parameter
.andWhereTyped((o) => o.note, IsDistinctFrom(note)) // "o"."note" IS DISTINCT FROM :isDistinctFrom_1
.andWhereTyped((o) => o.userId, EqualColumn(users.id)); // "o"."userId" = "users"."id"
- Checked values. The value of
Equal,Not,MoreThan,MoreThanOrEqual,LessThan,LessThanOrEqual,Between,Like,ILikeandAnyis checked against the column's type, withoutstrictParams:Equal('successful')on an enum column fails type-check. TypeORM typesIn(),IsNull(),Raw()and theArray…operators asFindOperator<any>, so they fit any column: useIsIn([…]), the same operator with its values typed, andNot(IsIn([…]))(IsIn(['successful'])on an enum column fails type-check). With an explicit type argument (whereTyped<Joins>(…)) TypeScript can't infer the column, and nothing is checked: set the root withtyped<Root>()instead. IsTrue()/IsFalse()test a boolean column withIS TRUE/IS FALSEand fit only boolean columns. They are never NULL, soNot(IsTrue())isIS NOT TRUEand matches a NULL column too, whileNot(Equal(true))(NOT(col = true)) skips it. A btree index on the column servesIS TRUE/IS FALSElike= true/= false. PostgreSQL, SQLite 3.23+ and MySQL.- Partial indexes and
IsTrue()/IsFalse(). PostgreSQL never matchescol IS TRUE/col IS FALSEto a partial index whoseWHEREtests the column (WHERE col,WHERE col = true,WHERE NOT col,WHERE col = false, alone or with other conditions): the planner falls back to another index or a sequential scan. In aWHEREclause a NULL column is left out byIS TRUEand by= truealike, so for a positive filter useEqual(true)/Equal(false): same rows, and the partial index applies. KeepIsTrue()/IsFalse()forNot(…), where NULL must match. Any([…]), TypeORM's, binds the list as one array parameter,= ANY(:param), whereIsInbinds a parameter per value: for long lists, past the database's limit of bound parameters. Its values are checked likeIsIn's; an empty list matches no row. PostgreSQL.IsDistinctFrom(value)isIS DISTINCT FROM: unlikeNot(Equal(…)), it matches a NULL column too, andIsDistinctFrom(null)matches the rows that aren't NULL.Not(IsDistinctFrom(value))is an=that matches NULL to NULL. The value may benullfor any column. PostgreSQL and SQLite 3.39+; on MySQL / MariaDB usewhereIsDistinctFrom, which writes<=>.EqualColumn(sql)compares the column with another column or an SQL expression, written as is;Not(EqualColumn(…))negates it.- An empty
IsIn([])/In([])matches no row (0=1), as infind(); an emptyNot(IsIn([]))matches every row. Like SQLNOT IN,Not(IsIn(…))never matches a row whose column is NULL.
Paths into JSON columns
A callback may go into a JSON column (json, jsonb or simple-json): the keys after the column become a JSON path.
// "exec"."details" ->> 'outcome' = 'executed'
query.andWhereTyped(exec => exec.details.outcome, `= 'executed'`)
// CAST("exec"."details" -> 'meta' ->> 'endTs' AS numeric) >= :param
query.andWhereTyped(exec => exec.details.meta.endTs, '>=', { endTime: endDate.getTime() })
-
Depth. Two levels (
column.key, oralias.columnon a joined entity) work with any JSON column. To go deeper, mark the property withJson<T>; without it TypeScript can't tell a JSON object from a relation, and relations stop atalias.column:import { Json } from 'typeorm-extensions';
@Column({ type: 'jsonb' })
details: Json<ExecutionDetails>; // values stay assignable both waysArrays inside the JSON are values:
e => e.details.tagscompares the whole array. -
Types of values.
->>gives text on PostgreSQL, and text compares'9' >= '10'as true. So the path is cast to the type of the value it is compared with: a number tonumeric, a boolean toboolean, aDatetotimestamptz. A string, or a condition without a value (= 'executed'), stays text. On SQLite->>gives SQL values and nothing is cast. -
Numeric text in an ordering comparison. With
<,>,<=or>=(also'>= :min'with a params object), a string that is a number ('10', as a query string gives it) is bound as that number, so it compares like10on both databases. Other operators keep a string as text:= '01234'stays a match on that text. -
Indexes. An expression index is used only for the same expression:
((details ->> 'outcome')), or((details -> 'meta' ->> 'endTs')::numeric)for the cast one. For equality on a hot path,whereJsonContainsTyped(@>, a GIN index) is the better fit. -
Where it works. In
whereTypedand the other typed where methods (whereIsInTyped,whereIsAnyTyped,whereIsDistinctFromTyped, …);whereJsonContainsTyped/whereJsonHasKeyTypeduse->, the JSON value. Select, order, distinct and group by take columns only and throw aTypeErrorfor a JSON path. PostgreSQL (asimple-jsoncolumn is read asjsonbthere) and SQLite 3.38+; other databases throw aTypeError.
Checking parameters: strictParams
By default params is unknown. Augment TypeormExtensionsConfig once in your project to check it against the type of the column the callback selects:
declare module 'typeorm-extensions' {
interface TypeormExtensionsConfig {
strictParams: true;
}
}
The expected value depends on the operator (matched case-insensitively):
| Operator | params |
|---|---|
=, <>, !=, <, <=, >, >= | the column's value, required, never null / undefined (= NULL matches no row) |
IS DISTINCT FROM, IS NOT DISTINCT FROM | the column's value or null |
IS, IS NOT | null |
IN, NOT IN | an array of the column's values |
LIKE, ILIKE, NOT LIKE, NOT ILIKE | a string |
IS NULL, IS NOT NULL, IS [NOT] TRUE, IS [NOT] FALSE, IS [NOT] UNKNOWN | none |
anything else ('BETWEEN :from AND :to', '= true') | not checked |
A date column takes a Date or a string. The value may be passed as is or in a { name: value } params object, except a plain object (a JSON column): at runtime a plain object always is a params object, so it has to be wrapped.
query.whereTyped(e => e.price, '>=', { minPrice: 10 }); // ok
query.whereTyped(e => e.price, '>=', '10'); // error: string for a number column
query.whereTyped(e => e.cityId, '=', { cityId: undefined }); // error: = NULL matches no row
query.whereTyped(e => e.id, 'IN', 'a'); // error: IN takes an array
query.whereTyped(e => e.settings, '=', { settings: value }); // ok; the bare `value` doesn't compile
Calls with an explicit type argument (whereTyped<Joins>(…)) stay unchecked: TypeScript can't infer the column type when one type argument is given. Set the root with typed<Joins>() instead, and the calls are checked.
Usage
// same as `query.where('user.name LIKE :name', { name: '%John%' })`
query
.whereTyped(
user => user.name,
'LIKE',
'%John%',
)
// same as `query.andWhere('user.name LIKE :name', { name: '%Doe%' })`
query
.andWhereTyped(
user => user.name,
'LIKE',
{ name: '%Doe%' },
)
// same as `query.orWhere('user.name LIKE :search', { search: '%John Doe%' })`
// `:search` is a key of params, so no extra placeholder is appended
query
.orWhereTyped(
user => user.name,
'LIKE :search',
{ search: '%John Doe%' },
)
// arrays get a `(:...key)` placeholder
// same as `query.where('user.id IN (:...ids)', { ids: [1, 2, 3] })`
query.whereTyped(user => user.id, 'IN', [1, 2, 3])
// columns of joined relations, including array relations (`images: ImageEntity[]`)
// same as `query.andWhere('images.url IS NOT NULL')`
query
.leftJoinTyped(user => user.images, 'images')
.andWhereTyped(user => user.images.url, 'IS NOT NULL')
// a key of a JSON column, see "Paths into JSON columns" above
// same as `query.where("user.metadata ->> 'role' = :role", { role: 'admin' })`
query.whereTyped(user => user.metadata.role, '=', 'admin')