Similar to Transaction.$extendTables but read-only.
Similar to Transaction.$omitTables but read-only.
Similar to Transaction.$pickTables but read-only.
Creates a case statement/operator.
See ExpressionBuilder.case for more information.
Creates a case statement/operator.
See ExpressionBuilder.case for more information.
Similar to Kysely.executeQuery but read-only.
Optionaloptions: AbortableQueryOptionsCreates a select query builder for the given table or tables.
The tables passed to this method are built as the query's from clause.
Create a select query for one table:
db.selectFrom('person').selectAll()
The generated SQL (PostgreSQL):
select * from "person"
Create a select query for one table with an alias:
const persons = await db.selectFrom('person as p')
.select(['p.id', 'first_name'])
.execute()
console.log(persons[0].id)
The generated SQL (PostgreSQL):
select "p"."id", "first_name" from "person" as "p"
Create a select query from a subquery:
const persons = await db.selectFrom(
(eb) => eb.selectFrom('person').select('person.id as identifier').as('p')
)
.select('p.identifier')
.execute()
console.log(persons[0].identifier)
The generated SQL (PostgreSQL):
select "p"."identifier",
from (
select "person"."id" as "identifier" from "person"
) as p
Create a select query from raw sql:
import { sql } from 'kysely'
const items = await db
.selectFrom(sql<{ one: number }>`(select 1 as one)`.as('q'))
.select('q.one')
.execute()
console.log(items[0].one)
The generated SQL (PostgreSQL):
select "q"."one",
from (
select 1 as one
) as q
When you use the sql tag you need to also provide the result type of the
raw snippet / query so that Kysely can figure out what columns are
available for the rest of the query.
The selectFrom method also accepts an array for multiple tables. All
the above examples can also be used in an array.
import { sql } from 'kysely'
const items = await db.selectFrom([
'person as p',
db.selectFrom('pet').select('pet.species').as('a'),
sql<{ one: number }>`(select 1 as one)`.as('q')
])
.select(['p.id', 'a.species', 'q.one'])
.execute()
The generated SQL (PostgreSQL):
select "p".id, "a"."species", "q"."one"
from
"person" as "p",
(select "pet"."species" from "pet") as a,
(select 1 as one) as "q"
Creates a select query builder without a from clause.
If you want to create a select from query, use the selectFrom method instead.
This one can be used to create a plain select statement without a from clause.
This method accepts the same inputs as SelectQueryBuilder.select. See its documentation for more examples.
const result = await db.selectNoFrom((eb) => [
eb.selectFrom('person')
.select('id')
.where('first_name', '=', 'Jennifer')
.limit(1)
.as('jennifer_id'),
eb.selectFrom('pet')
.select('id')
.where('name', '=', 'Doggo')
.limit(1)
.as('doggo_id')
])
.executeTakeFirstOrThrow()
console.log(result.jennifer_id)
console.log(result.doggo_id)
The generated SQL (PostgreSQL):
select (
select "id"
from "person"
where "first_name" = $1
limit $2
) as "jennifer_id", (
select "id"
from "pet"
where "name" = $3
limit $4
) as "doggo_id"
Creates a select query builder without a from clause.
If you want to create a select from query, use the selectFrom method instead.
This one can be used to create a plain select statement without a from clause.
This method accepts the same inputs as SelectQueryBuilder.select. See its documentation for more examples.
const result = await db.selectNoFrom((eb) => [
eb.selectFrom('person')
.select('id')
.where('first_name', '=', 'Jennifer')
.limit(1)
.as('jennifer_id'),
eb.selectFrom('pet')
.select('id')
.where('name', '=', 'Doggo')
.limit(1)
.as('doggo_id')
])
.executeTakeFirstOrThrow()
console.log(result.jennifer_id)
console.log(result.doggo_id)
The generated SQL (PostgreSQL):
select (
select "id"
from "person"
where "first_name" = $1
limit $2
) as "jennifer_id", (
select "id"
from "pet"
where "name" = $3
limit $4
) as "doggo_id"
Creates a select query builder without a from clause.
If you want to create a select from query, use the selectFrom method instead.
This one can be used to create a plain select statement without a from clause.
This method accepts the same inputs as SelectQueryBuilder.select. See its documentation for more examples.
const result = await db.selectNoFrom((eb) => [
eb.selectFrom('person')
.select('id')
.where('first_name', '=', 'Jennifer')
.limit(1)
.as('jennifer_id'),
eb.selectFrom('pet')
.select('id')
.where('name', '=', 'Doggo')
.limit(1)
.as('doggo_id')
])
.executeTakeFirstOrThrow()
console.log(result.jennifer_id)
console.log(result.doggo_id)
The generated SQL (PostgreSQL):
select (
select "id"
from "person"
where "first_name" = $1
limit $2
) as "jennifer_id", (
select "id"
from "pet"
where "name" = $3
limit $4
) as "doggo_id"
Similar to QueryCreator.with but read-only.
Similar to Transaction.withoutPlugins but read-only.
Similar to Transaction.withPlugin but read-only.
Similar to QueryCreator.withRecursive but read-only.
Similar to Transaction.withSchema but read-only.
Similar to Transaction.withTables but read-only.
use $extendTables instead.
Similar to Transaction but read-only.