kysely
    Preparing search index...

    Interface OutputInterface<DB, TB, O, OP>

    interface OutputInterface<
        DB,
        TB extends keyof DB,
        O,
        OP extends OutputPrefix = OutputPrefix,
    > {
        output<
            OE extends
                | AliasedExpression<any, any>
                | AliasedExpressionFactory<OutputDatabase<DB, TB, OP>, OP>
                | `deleted.${string}`
                | `inserted.${string}`
                | `deleted.${string} as ${string}`
                | `inserted.${string} as ${string}`,
        >(
            selections: readonly OE[],
        ): OutputInterface<
            DB,
            TB,
            ReturningRow<DB, TB, O, SelectExpressionFromOutputExpression<OE>>,
            OP,
        >;
        output<CB extends OutputCallback<DB, TB, OP>>(
            callback: CB,
        ): OutputInterface<
            DB,
            TB,
            ReturningRow<DB, TB, O, SelectExpressionFromOutputCallback<CB>>,
            OP,
        >;
        output<
            OE extends
                | AliasedExpression<any, any>
                | AliasedExpressionFactory<OutputDatabase<DB, TB, OP>, OP>
                | `deleted.${string}`
                | `inserted.${string}`
                | `deleted.${string} as ${string}`
                | `inserted.${string} as ${string}`,
        >(
            selection: OE,
        ): OutputInterface<
            DB,
            TB,
            ReturningRow<DB, TB, O, SelectExpressionFromOutputExpression<OE>>,
            OP,
        >;
        outputAll(
            table: OP,
        ): OutputInterface<DB, TB, ReturningAllRow<DB, TB, O>, OP>;
    }

    Type Parameters

    Implemented by

    Index

    Methods

    • Allows you to return data from modified rows.

      On supported databases like MS SQL Server (MSSQL), this method can be chained to insert, update, delete and merge queries to return data.

      Also see the outputAll method.

      Return one column:

      const { id } = await db
      .insertInto('person')
      .output('inserted.id')
      .values({
      first_name: 'Jennifer',
      last_name: 'Aniston',
      gender: 'female',
      })
      .executeTakeFirstOrThrow()

      The generated SQL (MSSQL):

      insert into "person" ("first_name", "last_name", "gender")
      output "inserted"."id"
      values (@1, @2, @3)

      Return multiple columns:

      const { old_first_name, old_last_name, new_first_name, new_last_name } = await db
      .updateTable('person')
      .set({ first_name: 'John', last_name: 'Doe' })
      .output([
      'deleted.first_name as old_first_name',
      'deleted.last_name as old_last_name',
      'inserted.first_name as new_first_name',
      'inserted.last_name as new_last_name',
      ])
      .where('created_at', '<', new Date())
      .executeTakeFirstOrThrow()

      The generated SQL (MSSQL):

      update "person"
      set "first_name" = @1, "last_name" = @2
      output "deleted"."first_name" as "old_first_name",
      "deleted"."last_name" as "old_last_name",
      "inserted"."first_name" as "new_first_name",
      "inserted"."last_name" as "new_last_name"
      where "created_at" < @3

      Return arbitrary expressions:

      importsql } from 'kysely'

      const { full_name } = await db
      .deleteFrom('person')
      .output((eb) => sql<string>`concat(${eb.ref('deleted.first_name')}, ' ', ${eb.ref('deleted.last_name')})`.as('full_name'))
      .where('created_at', '<', new Date())
      .executeTakeFirstOrThrow()

      The generated SQL (MSSQL):

      delete from "person"
      output concat("deleted"."first_name", ' ', "deleted"."last_name") as "full_name"
      where "created_at" < @1

      Return the action performed on the row:

      await db
      .mergeInto('person')
      .using('pet', 'pet.owner_id', 'person.id')
      .whenMatched()
      .thenDelete()
      .whenNotMatched()
      .thenInsertValues({
      first_name: 'John',
      last_name: 'Doe',
      gender: 'male'
      })
      .output([
      'inserted.id as inserted_id',
      'deleted.id as deleted_id',
      ])
      .execute()

      The generated SQL (MSSQL):

      merge into "person"
      using "pet" on "pet"."owner_id" = "person"."id"
      when matched then delete
      when not matched then
      insert ("first_name", "last_name", "gender")
      values (@1, @2, @3)
      output "inserted"."id" as "inserted_id", "deleted"."id" as "deleted_id"

      Type Parameters

      • OE extends
            | AliasedExpression<any, any>
            | AliasedExpressionFactory<OutputDatabase<DB, TB, OP>, OP>
            | `deleted.${string}`
            | `inserted.${string}`
            | `deleted.${string} as ${string}`
            | `inserted.${string} as ${string}`

      Parameters

      • selections: readonly OE[]

      Returns OutputInterface<
          DB,
          TB,
          ReturningRow<DB, TB, O, SelectExpressionFromOutputExpression<OE>>,
          OP,
      >

    • Type Parameters

      Parameters

      • callback: CB

      Returns OutputInterface<
          DB,
          TB,
          ReturningRow<DB, TB, O, SelectExpressionFromOutputCallback<CB>>,
          OP,
      >

    • Type Parameters

      • OE extends
            | AliasedExpression<any, any>
            | AliasedExpressionFactory<OutputDatabase<DB, TB, OP>, OP>
            | `deleted.${string}`
            | `inserted.${string}`
            | `deleted.${string} as ${string}`
            | `inserted.${string} as ${string}`

      Parameters

      • selection: OE

      Returns OutputInterface<
          DB,
          TB,
          ReturningRow<DB, TB, O, SelectExpressionFromOutputExpression<OE>>,
          OP,
      >