Complex selections
You can select arbitrary expression including subqueries and raw sql snippets.
When you do that, you need to give a name for the selections using the as method:
import { sql } from 'kysely'
const persons = await db.selectFrom('person')
.select(({ eb, selectFrom, or, val, lit }) => [
// Select a correlated subquery
selectFrom('pet')
.whereRef('person.id', '=', 'pet.owner_id')
.select('pet.name')
.orderBy('pet.name')
.limit(1)
.as('first_pet_name'),
// Build and select an expression using
// the expression builder
or([
eb('first_name', '=', 'Jennifer'),
eb('first_name', '=', 'Arnold')
]).as('is_jennifer_or_arnold'),
// Select a raw sql expression
sql<string>`concat(first_name, ' ', last_name)`.as('full_name'),
// Select a static string value
val('Some value').as('string_value'),
// Select a literal value
lit(42).as('literal_value'),
])
.execute()More examples
The API documentation is packed with examples. The API docs are hosted here, but you can access the same documentation by hovering over functions/methods/classes in your IDE. The examples are always just one hover away!
For example, check out these sections: