Skip to main content

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 IN with more values fails, e.g. with bind message has 4464 parameter formats but 0 parameters for 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), like whereIsIn. A missing array (null, undefined) is read as an empty one, not as SQL NULL: in a negated group (NOT (…)) it matches every row, where NOT (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 does innerJoinUnnest.

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 as LOWER(u.email), COALESCE(a."x", b."x") or u.id::text is used as is.
  • array?: readonly unknown[]: Values to compare with. Readonly arrays and as const constants 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 like varchar(36) and []); anything else throws a TypeError, 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), like whereIsNotIn.
  • A NULL in the array, or a NULL column, makes the condition NULL (row excluded), the same as NOT IN.
// WHERE "o"."status" <> ALL(CAST($1 AS order_status[]))
query.whereIsNotAny('o.status', ['cancelled', 'refunded'], 'order_status[]')