Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

WheneableMergeQueryBuilder

class WheneableMergeQueryBuilder<DB, TT extends keyof DB, ST extends keyof DB, O> implements Compilable<O>, OperationNodeSource {
constructor(props: MergeQueryBuilderProps);
$call<T>(func: (qb: this) => T): T;
$if<O2>(condition: boolean, func: (qb: this) => WheneableMergeQueryBuilder<any, any, any, O2>): 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>>;
compile(): CompiledQuery<never>;
execute(): Promise<SimplifyResult<O>[]>;
executeTakeFirst(): Promise<SimplifySingleResult<O>>;
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor | ((node: QueryNode) => Error)): Promise<SimplifyResult<O>>;
toOperationNode(): MergeQueryNode;
top(expression: number | bigint, modifiers?: "percent"): WheneableMergeQueryBuilder<DB, TT, ST, O>;
whenMatched(): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>;
whenMatchedAnd<RE extends ReferenceExpression<DB, TT | ST>, VE extends OperandValueExpressionOrList<DB, TT | ST, RE>>(
lhs: RE,
rhs: VE,
): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>;
whenMatchedAnd<E extends ExpressionOrFactory<DB, TT | ST, SqlBool>>(expression: E): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>;
whenMatchedAndRef<LRE extends ReferenceExpression<DB, TT | ST>, RRE extends ReferenceExpression<DB, TT | ST>>(
lhs: LRE,
rhs: RRE,
): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>;
whenNotMatched(): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>;
whenNotMatchedAnd<RE extends ReferenceExpression<DB, ST>, VE extends OperandValueExpressionOrList<DB, ST, RE>>(
lhs: RE,
rhs: VE,
): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>;
whenNotMatchedAnd<E extends ExpressionOrFactory<DB, ST, SqlBool>>(expression: E): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>;
whenNotMatchedAndRef<LRE extends ReferenceExpression<DB, ST>, RRE extends ReferenceExpression<DB, ST>>(
lhs: LRE,
rhs: RRE,
): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>;
whenNotMatchedBySource(): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>;
whenNotMatchedBySourceAnd<RE extends ReferenceExpression<DB, TT>, VE extends OperandValueExpressionOrList<DB, TT, RE>>(
lhs: RE,
rhs: VE,
): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>;
whenNotMatchedBySourceAnd<E extends ExpressionOrFactory<DB, TT, SqlBool>>(expression: E): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>;
whenNotMatchedBySourceAndRef<LRE extends ReferenceExpression<DB, TT>, RRE extends ReferenceExpression<DB, TT>>(
lhs: LRE,
rhs: RRE,
): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>;
}

§Type Parameters

§
TT extends keyof DB
[src]
§
ST extends keyof DB
[src]

§Implements

§Constructors

§
new WheneableMergeQueryBuilder(props: MergeQueryBuilderProps)
[src]

§Methods

§
$call<T>(func: (qb: this) => T): T
[src]

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 {@link $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()
§
$if<O2>(condition: boolean, func: (qb: this) => WheneableMergeQueryBuilder<any, any, any, O2>): 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>>
[src]

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
}
§
compile(): CompiledQuery<never>
[src]
§
execute(): Promise<SimplifyResult<O>[]>
[src]

Executes the query and returns an array of rows.

Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.

§
executeTakeFirst(): Promise<SimplifySingleResult<O>>
[src]

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

§
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor | ((node: QueryNode) => Error)): Promise<SimplifyResult<O>>
[src]

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

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

§
toOperationNode(): MergeQueryNode
[src]
§
top(expression: number | bigint, modifiers?: "percent"): WheneableMergeQueryBuilder<DB, TT, ST, O>
[src]
§
whenMatched(): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>
[src]

Adds a simple when matched clause to the query.

For a when matched clause with an and condition, see {@link whenMatchedAnd}.

For a simple when not matched clause, see {@link whenNotMatched}.

For a when not matched clause with an and condition, see {@link 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
§
whenMatchedAnd<RE extends ReferenceExpression<DB, TT | ST>, VE extends OperandValueExpressionOrList<DB, TT | ST, RE>>(lhs: RE, op: ComparisonOperatorExpression, rhs: VE): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>
[src]

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

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

For a simple when matched clause (without an and condition) see {@link whenMatched}.

Examples

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

The generated SQL (PostgreSQL):

merge into "person"
using "pet" on "person"."id" = "pet"."owner_id"
when matched and "person"."first_name" = $1 then
  delete
whenMatchedAnd<E extends ExpressionOrFactory<DB, TT | ST, SqlBool>>(expression: E): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>
[src]
§
whenMatchedAndRef<LRE extends ReferenceExpression<DB, TT | ST>, RRE extends ReferenceExpression<DB, TT | ST>>(lhs: LRE, op: ComparisonOperatorExpression, rhs: RRE): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT | ST, O>
[src]

Adds the when matched clause to the query with an and condition. But unlike {@link whenMatchedAnd}, this method accepts a column reference as the 3rd argument.

This method is similar to {@link SelectQueryBuilder.whereRef}, so see the documentation for that method for more examples.

§
whenNotMatched(): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>
[src]

Adds a simple when not matched clause to the query.

For a when not matched clause with an and condition, see {@link whenNotMatchedAnd}.

For a simple when matched clause, see {@link whenMatched}.

For a when matched clause with an and condition, see {@link 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)
§
whenNotMatchedAnd<RE extends ReferenceExpression<DB, ST>, VE extends OperandValueExpressionOrList<DB, ST, RE>>(lhs: RE, op: ComparisonOperatorExpression, rhs: VE): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>
[src]

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

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

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

Unlike {@link 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)
whenNotMatchedAnd<E extends ExpressionOrFactory<DB, ST, SqlBool>>(expression: E): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>
[src]
§
whenNotMatchedAndRef<LRE extends ReferenceExpression<DB, ST>, RRE extends ReferenceExpression<DB, ST>>(lhs: LRE, op: ComparisonOperatorExpression, rhs: RRE): NotMatchedThenableMergeQueryBuilder<DB, TT, ST, O>
[src]

Adds the when not matched clause to the query with an and condition. But unlike {@link whenNotMatchedAnd}, this method accepts a column reference as the 3rd argument.

Unlike {@link whenMatchedAndRef}, you cannot reference columns from the target table.

This method is similar to {@link SelectQueryBuilder.whereRef}, so see the documentation for that method for more examples.

§
whenNotMatchedBySource(): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>
[src]

Adds a simple when not matched by source clause to the query.

Supported in MS SQL Server.

Similar to {@link whenNotMatched}, but returns a MatchedThenableMergeQueryBuilder.

§
whenNotMatchedBySourceAnd<RE extends ReferenceExpression<DB, TT>, VE extends OperandValueExpressionOrList<DB, TT, RE>>(lhs: RE, op: ComparisonOperatorExpression, rhs: VE): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>
[src]

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

Supported in MS SQL Server.

Similar to {@link whenNotMatchedAnd}, but returns a MatchedThenableMergeQueryBuilder.

whenNotMatchedBySourceAnd<E extends ExpressionOrFactory<DB, TT, SqlBool>>(expression: E): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>
[src]
§
whenNotMatchedBySourceAndRef<LRE extends ReferenceExpression<DB, TT>, RRE extends ReferenceExpression<DB, TT>>(lhs: LRE, op: ComparisonOperatorExpression, rhs: RRE): MatchedThenableMergeQueryBuilder<DB, TT, ST, TT, O>
[src]

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

Similar to {@link whenNotMatchedAndRef}, but you can reference columns from the target table, and not from source table and returns a MatchedThenableMergeQueryBuilder.