Class WheneableMergeQueryBuilder<DB, TT, ST, O>

Type Parameters

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

Implements

Constructors

Properties

#private: any

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.

    Examples

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

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

    db.updateTable('person')
    .set(values)
    .$call(log)
    .execute()

    Type Parameters

    • T

    Parameters

    • func: ((qb) => T)
        • (qb): T
        • Parameters

          • qb: this

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

    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
    }

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

  • Executes the query and returns an array of rows.

    Also see the executeTakeFirst and executeTakeFirstOrThrow methods.

    Returns Promise<SimplifyResult<O>[]>

  • Executes the query and returns the first result or undefined if the query returned no result.

    Returns Promise<SimplifySingleResult<O>>

  • Executes the query and returns the first result or throws if the query returned no result.

    By default an instance of NoResultError is thrown, but you can provide a custom error class, or callback as the only argument to throw a different error.

    Parameters

    Returns Promise<SimplifyResult<O>>

  • Adds a simple when matched clause to the query.

    For a when matched clause with an and condition, see whenMatchedAnd.

    For a simple when not matched clause, see whenNotMatched.

    For a when not matched clause with an and condition, see whenNotMatchedAnd.

    Examples

    const result = await db.mergeInto('person')
    .using('pet', 'person.id', 'pet.owner_id')
    .whenMatched()
    .thenDelete()
    .execute()

    The generated SQL (PostgreSQL):

    merge into "person"
    using "pet" on "person"."id" = "pet"."owner_id"
    when matched then
    delete

    Returns MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>

  • Adds a simple when not matched clause to the query.

    For a when not matched clause with an and condition, see whenNotMatchedAnd.

    For a simple when matched clause, see whenMatched.

    For a when matched clause with an and condition, see whenMatchedAnd.

    Examples

    const result = await db.mergeInto('person')
    .using('pet', 'person.id', 'pet.owner_id')
    .whenNotMatched()
    .thenInsertValues({
    first_name: 'John',
    last_name: 'Doe',
    })
    .execute()

    The generated SQL (PostgreSQL):

    merge into "person"
    using "pet" on "person"."id" = "pet"."owner_id"
    when not matched then
    insert ("first_name", "last_name") values ($1, $2)

    Returns NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>

  • Adds the when not matched clause to the query with an and condition.

    This method is similar to SelectQueryBuilder.where, so see the documentation for that method for more examples.

    For a simple when not matched clause (without an and condition) see whenNotMatched.

    Unlike whenMatchedAnd, you cannot reference columns from the table merged into.

    Examples

    const result = await db.mergeInto('person')
    .using('pet', 'person.id', 'pet.owner_id')
    .whenNotMatchedAnd('pet.name', '=', 'Lucky')
    .thenInsertValues({
    first_name: 'John',
    last_name: 'Doe',
    })
    .execute()

    The generated SQL (PostgreSQL):

    merge into "person"
    using "pet" on "person"."id" = "pet"."owner_id"
    when not matched and "pet"."name" = $1 then
    insert ("first_name", "last_name") values ($2, $3)

    Type Parameters

    • RE extends string | Expression<any> | DynamicReferenceBuilder<any> | SelectQueryBuilderExpression<Record<string, any>> | OperandExpressionFactory<DB, ST, any>
    • VE extends any

    Parameters

    Returns NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>

  • Type Parameters

    Parameters

    • expression: E

    Returns NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>