Module

x/kysely_postgrs_js_dialect/mod.ts>kysely.WheneableMergeQueryBuilder#$if

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

Call func(this) if condition is true.

This method is especially handy with optional selects. Any returning or returningAll method calls add columns as optional fields to the output type when called inside the func callback. This is because we can't know if those selections were actually made before running the code.

You can also call any other methods inside the callback.

Examples

async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
  return await db
    .updateTable('person')
    .set(updates)
    .where('id', '=', id)
    .returning(['id', 'first_name'])
    .$if(returnLastName, (qb) => qb.returning('last_name'))
    .executeTakeFirstOrThrow()
}

Any selections added inside the if callback will be added as optional fields to the output type since we can't know if the selections were actually made before running the code. In the example above the return type of the updatePerson function is:

{
  id: number
  first_name: string
  last_name?: string
}

Parameters

condition: boolean
func: (qb: this) => WheneableMergeQueryBuilder<any, any, any, O2>

Returns

O2 extends MergeResult ? WheneableMergeQueryBuilder<DB, TT, ST, MergeResult> : O2 extends O & infer E ? WheneableMergeQueryBuilder<DB, TT, ST, O & Partial<E>> : WheneableMergeQueryBuilder<DB, TT, ST, Partial<O2>>