Skip to main content
Module

x/kysely_deno_postgres_dialect/mod.ts>kysely.OrWrapper

Kysely dialect for PostgreSQL using the deno-postgres client.
Latest
class kysely.OrWrapper
implements AliasableExpression<T>
import { kysely } from "https://deno.land/x/kysely_deno_postgres_dialect@v0.27.1/mod.ts";
const { OrWrapper } = kysely;

Constructors

new
OrWrapper(node: OrNode)

Type Parameters

DB
TB extends keyof DB
T extends SqlBool

Properties

readonly
expressionType: T | undefined

All expressions need to have this getter for complicated type-related reasons. Simply add this getter for your expression and always return undefined from it:

class SomeExpression<T> implements Expression<T> {
  get expressionType(): T | undefined {
    return undefined
  }
}

The getter is needed to make the expression assignable to another expression only if the types T are assignable. Without this property (or some other property that references T), you could assing Expression<string> to Expression<number>.

Methods

$castTo<T extends SqlBool>(): OrWrapper<DB, TB, T>

Change the output type of the expression.

This method call doesn't change the SQL in any way. This methods simply returns a copy of this OrWrapper with a new output type.

as<A extends string>(alias: A): AliasedExpression<T, A>

Returns an aliased version of the expression.

In addition to slapping as "the_alias" to the end of the SQL, this method also provides strict typing:

const result = await db
  .selectFrom('person')
  .select(eb =>
    eb('first_name', '=', 'Jennifer')
      .or('first_name', '=', 'Sylvester')
      .as('is_jennifer_or_sylvester')
  )
  .executeTakeFirstOrThrow()

// `is_jennifer_or_sylvester: SqlBool` field exists in the result type.
console.log(result.is_jennifer_or_sylvester)

The generated SQL (PostgreSQL):

select "first_name" = $1 or "first_name" = $2 as "is_jennifer_or_sylvester"
from "person"
as<A extends string>(alias: Expression<unknown>): AliasedExpression<T, A>

Returns an aliased version of the expression.

In addition to slapping as "the_alias" at the end of the expression, this method also provides strict typing:

const result = await db
  .selectFrom('person')
  .select((eb) =>
    // `eb.fn<string>` returns an AliasableExpression<string>
    eb.fn<string>('concat', ['first_name' eb.val(' '), 'last_name']).as('full_name')
  )
  .executeTakeFirstOrThrow()

// `full_name: string` field exists in the result type.
console.log(result.full_name)

The generated SQL (PostgreSQL):

select
  concat("first_name", $1, "last_name") as "full_name"
from
  "person"

You can also pass in a raw SQL snippet (or any expression) but in that case you must provide the alias as the only type argument:

const values = sql<{ a: number, b: string }>`(values (1, 'foo'))`

// The alias is `t(a, b)` which specifies the column names
// in addition to the table name. We must tell kysely that
// columns of the table can be referenced through `t`
// by providing an explicit type argument.
const aliasedValues = values.as<'t'>(sql`t(a, b)`)

await db
  .insertInto('person')
  .columns(['first_name', 'last_name'])
  .expression(
    db.selectFrom(aliasedValues).select(['t.a', 't.b'])
  )

The generated SQL (PostgreSQL):

insert into "person" ("first_name", "last_name")
from (values (1, 'foo')) as t(a, b)
select "t"."a", "t"."b"
or<RE extends ReferenceExpression<DB, TB>, VE extends OperandValueExpressionOrList<DB, TB, RE>>(
lhs: RE,
rhs: VE,
): OrWrapper<DB, TB, T>

Combines this and another expression using OR.

See ExpressionWrapper.or for examples.

or<E extends OperandExpression<SqlBool>>(expression: E): OrWrapper<DB, TB, T>

Creates the OperationNode that describes how to compile this expression into SQL.

If you are creating a custom expression, it's often easiest to use the sql template tag to build the node:

class SomeExpression<T> implements Expression<T> {
  toOperationNode(): OperationNode {
    return sql`some sql here`.toOperationNode()
  }
}