Module

x/kysely_postgrs_js_dialect/mod.ts>kysely.QueryCreator#with

Kysely dialect for PostgreSQL using the Postgres.js client.
Latest
method kysely.QueryCreator.prototype.with
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.4/mod.ts";
const { QueryCreator } = kysely;

Creates a with query (Common Table Expression).

Examples

await db
  .with('jennifers', (db) => db
    .selectFrom('person')
    .where('first_name', '=', 'Jennifer')
    .select(['id', 'age'])
  )
  .with('adult_jennifers', (db) => db
    .selectFrom('jennifers')
    .where('age', '>', 18)
    .select(['id', 'age'])
  )
  .selectFrom('adult_jennifers')
  .where('age', '<', 60)
  .selectAll()
  .execute()

The CTE name can optionally specify column names in addition to a name. In that case Kysely requires the expression to retun rows with the same columns.

await db
  .with('jennifers(id, age)', (db) => db
    .selectFrom('person')
    .where('first_name', '=', 'Jennifer')
    // This is ok since we return columns with the same
    // names as specified by `jennifers(id, age)`.
    .select(['id', 'age'])
  )
  .selectFrom('jennifers')
  .selectAll()
  .execute()

The first argument can also be a callback. The callback is passed a CTEBuilder instance that can be used to configure the CTE:

await db
  .with(
    (cte) => cte('jennifers').materialized(),
    (db) => db
      .selectFrom('person')
      .where('first_name', '=', 'Jennifer')
      .select(['id', 'age'])
  )
  .selectFrom('jennifers')
  .selectAll()
  .execute()

Type Parameters

N extends string
E extends CommonTableExpression<DB, N>

Parameters

nameOrBuilder: N | CTEBuilderCallback<N>
expression: E

Returns

QueryCreatorWithCommonTableExpression<DB, N, E>