Module

x/kysely_postgrs_js_dialect/deps.ts>kysely.ReturningInterface

Kysely dialect for PostgreSQL using the Postgres.js client.
interface kysely.ReturningInterface
import { type kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";
const { ReturningInterface } = kysely;

Type Parameters

DB
TB extends keyof DB
O

Methods

returning<SE extends SelectExpression<DB, TB>>(selections: ReadonlyArray<SE>): ReturningInterface<DB, TB, ReturningRow<DB, TB, O, SE>>

Allows you to return data from modified rows.

On supported databases like PostgreSQL, this method can be chained to insert, update and delete queries to return data.

Note that on SQLite you need to give aliases for the expressions to avoid this bug in SQLite. For example .returning('id as id').

Also see the {@link returningAll} method.

Examples

Return one column:

const { id } = await db
  .insertInto('person')
  .values({
    first_name: 'Jennifer',
    last_name: 'Aniston'
  })
  .returning('id')
  .executeTakeFirst()

Return multiple columns:

const { id, first_name } = await db
  .insertInto('person')
  .values({
    first_name: 'Jennifer',
    last_name: 'Aniston'
  })
  .returning(['id', 'last_name'])
  .executeTakeFirst()

Return arbitrary expressions:

import { sql } from 'kysely'

const { id, full_name, first_pet_id } = await db
  .insertInto('person')
  .values({
    first_name: 'Jennifer',
    last_name: 'Aniston'
  })
  .returning((eb) => [
    'id as id',
    sql<string>`concat(first_name, ' ', last_name)`.as('full_name'),
    eb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
  ])
  .executeTakeFirst()
returning<CB extends SelectCallback<DB, TB>>(callback: CB): ReturningInterface<DB, TB, ReturningCallbackRow<DB, TB, O, CB>>
returning<SE extends SelectExpression<DB, TB>>(selection: SE): ReturningInterface<DB, TB, ReturningRow<DB, TB, O, SE>>
returningAll(): ReturningInterface<DB, TB, Selectable<DB[TB]>>

Adds a returning * to an insert/update/delete query on databases that support returning such as PostgreSQL.

import kysely.ReturningInterface
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";
const { ReturningInterface } = kysely;