kysely
    Preparing search index...

    Class WheneableMergeQueryBuilder<DB, TT, ST, O>

    Type Parameters

    • DB
    • TT extends keyof DB
    • ST extends keyof DB
    • O

    Implements

    Index

    Constructors

    Methods

    • Simply calls the provided function passing this as the only argument. $call returns what the provided function returns.

      If you want to conditionally call a method on this, see the $if method.

      The next example uses a helper function log to log a query:

      import type { Compilable } from 'kysely'

      function log<T extends Compilable>(qb: T): T {
      console.log(qb.compile())
      return qb
      }

      await db.updateTable('person')
      .set({ first_name: 'John' })
      .$call(log)
      .execute()

      Type Parameters

      • T

      Parameters

      • func: (qb: this) => T

      Returns T

    • 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.

      import type { PersonUpdate } from 'type-editor' // imaginary module

      async function updatePerson(id: number, updates: PersonUpdate, 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:

      Promise<{
      id: number
      first_name: string
      last_name?: string
      }>

      Type Parameters

      • O2

      Parameters

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

    • This can be used to add any additional SQL to the end of the query.

      import { sql } from 'kysely'

      await db
      .mergeInto('person')
      .using('pet', 'pet.owner_id', 'person.id')
      .whenMatched()
      .thenDelete()
      .modifyEnd(sql.raw('-- this is a comment'))
      .execute()

      The generated SQL (PostgreSQL):

      merge into "person" using "pet" on "pet"."owner_id" = "person"."id" when matched then delete -- this is a comment
      

      Parameters

      Returns WheneableMergeQueryBuilder<DB, TT, ST, O>