max
The max method will pluck a field with the highest value:
const id = await User.max('id')
select max("id") from "users"
The max method is smart enough to know the return type for id, so the returned value will already have the correct type cast for the id field.
Chaining
You can leverage chainable methods to filter out records at the SQL level before calling max:
const id = await User.where({ email: ops.ilike('%burpcollaborator%') }).max(
'id',
)
select max("id") from "users" where ("users"."email" ilike $1)
Grouped maximums: maxBy
max collapses the whole query to a single value. When you need the maximum broken out per group in one query, use maxBy(groupColumn, aggregatedColumn). It stays entirely in Dream — no ejecting to Kysely and no hand-written GROUP BY — and returns Map<groupValue, max>:
const maxIdByRole = await User.maxBy('role', 'id')
Only groups with at least one matching row are present in the returned Map — seed absent keys as needed — and a nullable group column produces a real null key.