Skip to main content

min

The min method will pluck a field with the lowest value:

const id = await User.min('id')

select min("id") from "users"

The min 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 min:

const id = await User.where({ email: ops.ilike('%burpcollaborator%') }).min(
'id',
)

select min("id") from "users" where ("users"."email" ilike $1)

Grouped minimums: minBy

min collapses the whole query to a single value. When you need the minimum broken out per group in one query, use minBy(groupColumn, aggregatedColumn). It stays entirely in Dream — no ejecting to Kysely and no hand-written GROUP BY — and returns Map<groupValue, min>:

const minIdByRole = await User.minBy('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.