Module

x/kysely_postgrs_js_dialect/mod.ts>kysely.AggregateFunctionBuilder#filterWhere

Kysely dialect for PostgreSQL using the Postgres.js client.
method kysely.AggregateFunctionBuilder.prototype.filterWhere
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/mod.ts";
const { AggregateFunctionBuilder } = kysely;

Adds a filter clause with a nested where clause after the function.

Similar to WhereInterface's where method.

Also see {@link filterWhereRef}.

Examples

Count by gender:

const result = await db
  .selectFrom('person')
  .select((eb) => [
    eb.fn
      .count<number>('id')
      .filterWhere('gender', '=', 'female')
      .as('female_count'),
    eb.fn
      .count<number>('id')
      .filterWhere('gender', '=', 'male')
      .as('male_count'),
    eb.fn
      .count<number>('id')
      .filterWhere('gender', '=', 'other')
      .as('other_count'),
  ])
  .executeTakeFirstOrThrow()

The generated SQL (PostgreSQL):

select
  count("id") filter(where "gender" = $1) as "female_count",
  count("id") filter(where "gender" = $2) as "male_count",
  count("id") filter(where "gender" = $3) as "other_count"
from "person"