Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/kysely_postgrs_js_dialect/mod.ts>kysely.InsertQueryBuilder#returning

Kysely dialect for PostgreSQL using the Postgres.js client.
Latest
method kysely.InsertQueryBuilder.prototype.returning
Re-export
import { kysely } from "https://deno.land/x/kysely_postgrs_js_dialect@v0.27.4/mod.ts";
const { InsertQueryBuilder } = kysely;

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

Parameters

selections: ReadonlyArray<SE>

Returns

InsertQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>

Parameters

callback: CB

Returns

InsertQueryBuilder<DB, TB, ReturningCallbackRow<DB, TB, O, CB>>

Parameters

selection: SE

Returns

InsertQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>