Skip to main content
Module

x/kysely_postgrs_js_dialect/deps.ts>kysely.QueryCreator#mergeInto

Kysely dialect for PostgreSQL using the Postgres.js client.
Go to Latest
method kysely.QueryCreator.prototype.mergeInto
Re-export
import { kysely } from "https://deno.land/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";
const { QueryCreator } = kysely;

Creates a merge query.

The return value of the query is a MergeResult.

See the MergeQueryBuilder.using method for examples on how to specify the other table.

Examples

const result = await db
  .mergeInto('person')
  .using('pet', 'pet.owner_id', 'person.id')
  .whenMatched((and) => and('has_pets', '!=', 'Y'))
  .thenUpdateSet({ has_pets: 'Y' })
  .whenNotMatched()
  .thenDoNothing()
  .executeTakeFirstOrThrow()

console.log(result.numChangedRows)

The generated SQL (PostgreSQL):

merge into "person"
using "pet" on "pet"."owner_id" = "person"."id"
when matched and "has_pets" != $1 then
  update set "has_pets" = $2
when not matched then
  do nothing

Type Parameters

TR extends keyof DB & string

Parameters

targetTable: TR

Type Parameters

TR extends AnyAliasedTable<DB>

Parameters

targetTable: TR

Returns

MergeQueryBuilder<DB & PickTableWithAlias<DB, TR>, ExtractTableAlias<DB & PickTableWithAlias<DB, TR>, TR>, MergeResult>