whereIsAny
Description
whereIsAny adds a column = ANY(:array) condition. Unlike whereIsIn, which expands the array into one bind parameter per value (IN ($1, $2, …)), the whole array is bound as one array parameter.
- Works for any number of values. PostgreSQL allows at most 65535 bind parameters per query, so
INwith more values fails, e.g. withbind message has 4464 parameter formats but 0 parametersfor 70 000 values. - The SQL text is the same for any array length, which helps prepared statements and
pg_stat_statements. - An empty (or missing) array becomes
FALSE(no rows match), likewhereIsIn. A missing array (null,undefined) is read as an empty one, not as SQLNULL: in a negated group (NOT (…)) it matches every row, whereNOT (x = ANY(NULL))would match none. - One value and an object with array-index keys are read as a list, and anything else throws a
TypeError, as in whereIsIn; so doesinnerJoinUnnest.
PostgreSQL only (and compatible databases such as CockroachDB): it needs array parameters.
Available on SelectQueryBuilder, UpdateQueryBuilder, DeleteQueryBuilder and SoftDeleteQueryBuilder (softDelete(), restore()).
Signature
whereIsAny(field: string, array?: readonly unknown[], arrayType?: string): this
andWhereIsAny(field: string, array?: readonly unknown[], arrayType?: string): this
orWhereIsAny(field: string, array?: readonly unknown[], arrayType?: string): this
whereIsNotAny(field: string, array?: readonly unknown[], arrayType?: string): this
andWhereIsNotAny(field: string, array?: readonly unknown[], arrayType?: string): this
orWhereIsNotAny(field: string, array?: readonly unknown[], arrayType?: string): this
Arguments
- field: string: Column to compare, e.g.
'user.id', which is quoted as an SQL identifier:"user"."id"(already quoted parts are kept). An expression such asLOWER(u.email),COALESCE(a."x", b."x")oru.id::textis used as is. - array?: readonly unknown[]: Values to compare with. Readonly arrays and
as constconstants are accepted. - arrayType?: string: Type to cast the array parameter to, e.g.
'uuid[]','text[]','public.order_status[]':= ANY(CAST(:param AS uuid[])). PostgreSQL usually infers the type from the column, so it's optional. Only a type name is accepted (optionally schema-qualified, with a modifier likevarchar(36)and[]); anything else throws aTypeError, since it goes into the SQL as is.
Like TypeORM's where, whereIsAny replaces previously set WHERE conditions; use andWhereIsAny / orWhereIsAny to add to them.
Usage
// WHERE "user"."id" = ANY($1), $1 = '{1,2,3}'
query.whereIsAny('user.id', [1, 2, 3])
// one parameter, whatever the size of `ids`
query.andWhereIsAny('user.id', ids)
// same as `query.where('FALSE')`: no rows
query.whereIsAny('user.id', [])
For a typesafe version that takes the column from an entity property, see whereIsAnyTyped.
whereIsNotAny
whereIsNotAny is the negation: column <> ALL(:array), true when the column differs from every value of the array, like NOT IN. (<> ANY would be true as soon as the column differs from one value, i.e. almost always.)
- An empty (or missing) array becomes
TRUE(the condition doesn't filter anything), likewhereIsNotIn. - A
NULLin the array, or aNULLcolumn, makes the conditionNULL(row excluded), the same asNOT IN.
// WHERE "o"."status" <> ALL(CAST($1 AS order_status[]))
query.whereIsNotAny('o.status', ['cancelled', 'refunded'], 'order_status[]')