import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.4/deps.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
E extends CommonTableExpression<DB, N>