Module

x/kysely_postgrs_js_dialect/deps.ts>Kysely

Kysely dialect for PostgreSQL using the Postgres.js client.
class Kysely
implements QueryExecutorProvider
extends QueryCreator<DB>
import { Kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";

The main Kysely class.

You should create one instance of Kysely per database using the Kysely constructor. Each Kysely instance maintains it's own connection pool.

Examples

This example assumes your database has tables person and pet:

importKysely, Generated, PostgresDialect } from 'kysely'

interface PersonTable {
  id: Generated<number>
  first_name: string
  last_name: string
}

interface PetTable {
  id: Generated<number>
  owner_id: number
  name: string
  species: 'cat' | 'dog'
}

interface Database {
  person: PersonTable,
  pet: PetTable
}

const db = new Kysely<Database>({
  dialect: new PostgresDialect({
    host: 'localhost',
    database: 'kysely_test',
  })
})

Constructors

new
Kysely(args: KyselyConfig)
new
Kysely(args: KyselyProps)

Properties

readonly
dynamic: DynamicModule

Returns a the {@link DynamicModule} module.

The {@link DynamicModule} module can be used to bypass strict typing and passing in dynamic values for the queries.

readonly
fn: FunctionModule<DB, keyof DB>

Returns a {@link FunctionModule} that can be used to write type safe function calls.

await db.selectFrom('person')
  .innerJoin('pet', 'pet.owner_id', 'person.id')
  .select((eb) => [
    'person.id',
    eb.fn.count('pet.id').as('pet_count')
  ])
  .groupBy('person.id')
  .having((eb) => eb.fn.count('pet.id'), '>', 10)
  .execute()

The generated SQL (PostgreSQL):

select "person"."id", count("pet"."id") as "pet_count"
from "person"
inner join "pet" on "pet"."owner_id" = "person"."id"
group by "person"."id"
having count("pet"."id") > $1
readonly
introspection: DatabaseIntrospector

Returns a {@link DatabaseIntrospector | database introspector}.

readonly
isTransaction: boolean

Returns true if this Kysely instance is a transaction.

You can also use db instanceof Transaction.

readonly
schema: SchemaModule

Returns the {@link SchemaModule} module for building database schema.

Methods

case(): CaseBuilder<DB, keyof DB>

Creates a case statement/operator.

See {@link ExpressionBuilder.case} for more information.

case<V>(value: Expression<V>): CaseBuilder<DB, keyof DB, V>
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.

executeQuery<R>(query: CompiledQuery<R> | Compilable<R>, queryId?: QueryId): Promise<QueryResult<R>>

Executes a given compiled query or query builder.

See splitting build, compile and execute code recipe for more information.

getExecutor(): QueryExecutor
transaction(): TransactionBuilder<DB>

Creates a {@link TransactionBuilder} that can be used to run queries inside a transaction.

The returned {@link TransactionBuilder} can be used to configure the transaction. The {@link TransactionBuilder.execute} method can then be called to run the transaction. {@link 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 {@link 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): Kysely<DB>

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

withSchema(schema: string): Kysely<DB>
withTables<T extends Record<string, Record<string, any>>>(): Kysely<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:

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