Skip to main content
Module

x/kysely_deno_postgres_dialect/deps.ts>Transaction

Kysely dialect for PostgreSQL using the deno-postgres client.
Latest
class Transaction
extends Kysely<DB>
import { Transaction } from "https://deno.land/x/kysely_deno_postgres_dialect@v0.27.1/deps.ts";

Constructors

new
Transaction(props: KyselyProps)

Properties

readonly
isTransaction: true

Returns true if this Kysely instance is a transaction.

You can also use db instanceof Transaction.

Methods

connection(): ConnectionBuilder<DB>

Provides a kysely instance bound to a single database connection.

Examples

await db
  .connection()
  .execute(async (db) => {
    // `db` is an instance of `Kysely` that's bound to a single
    // database connection. All queries executed through `db` use
    // the same connection.
    await doStuff(db)
  })
destroy(): Promise<void>

Releases all resources and disconnects from the database.

You need to call this when you are done using the Kysely instance.

transaction(): TransactionBuilder<DB>

Creates a TransactionBuilder that can be used to run queries inside a transaction.

The returned TransactionBuilder can be used to configure the transaction. The TransactionBuilder.execute method can then be called to run the transaction. TransactionBuilder.execute takes a function that is run inside the transaction. If the function throws, the transaction is rolled back. Otherwise the transaction is committed.

The callback function passed to the TransactionBuilder.execute | execute method gets the transaction object as its only argument. The transaction is of type Transaction which inherits Kysely. Any query started through the transaction object is executed inside the transaction.

Examples

This example inserts two rows in a transaction. If an error is thrown inside the callback passed to the execute method, the transaction is rolled back. Otherwise it's committed.

const catto = await db.transaction().execute(async (trx) => {
  const jennifer = await trx.insertInto('person')
    .values({
      first_name: 'Jennifer',
      last_name: 'Aniston',
      age: 40,
    })
    .returning('id')
    .executeTakeFirstOrThrow()

  return await trx.insertInto('pet')
    .values({
      owner_id: jennifer.id,
      name: 'Catto',
      species: 'cat',
      is_favorite: false,
    })
    .returningAll()
    .executeTakeFirst()
})

Setting the isolation level:

await db
  .transaction()
  .setIsolationLevel('serializable')
  .execute(async (trx) => {
    await doStuff(trx)
  })

Returns a copy of this Kysely instance without any plugins.

withPlugin(plugin: KyselyPlugin): Transaction<DB>

Returns a copy of this Kysely instance with the given plugin installed.

withSchema(schema: string): Transaction<DB>
withTables<T extends Record<string, Record<string, any>>>(): Transaction<DrainOuterGeneric<DB & T>>

Returns a copy of this Kysely instance with tables added to its database type.

This method only modifies the types and doesn't affect any of the executed queries in any way.

Examples

The following example adds and uses a temporary table: