Database — Using Modifiers

Refine result shape and ordering with modifiers.
Chain them after filters and before select().

Prerequisite: You have a client created with a dbSpec (schema + relations [+ optional seed, meta]) and an adapter factory for SQLite Web/Expo or Supabase.


Execution Rule

  • In v1.0, .select(projection) executes the query.
  • Add all modifiers before calling .select(...).

order(column, { ascending?, nullsFirst? })

Sort results by a column.

1// Newest first
2const newest = await db
3  .from('posts')
4  .order('created_at', { ascending: false })
5  .select('id, title, created_at')
6
7// Name ascending, NULLs first
8const byName = await db
9  .from('users')
10  .order('name', { ascending: true, nullsFirst: true })
11  .select('id, name')

limit(n)

Return up to n rows.

1const top10 = await db
2  .from('posts')
3  .order('created_at', { ascending: false })
4  .limit(10)
5  .select('id, title')

range(from, to)

Return a window (inclusive indices). Useful for paging.

1// Rows 20..39 (20 items)
2const windowed = await db
3  .from('users')
4  .order('created_at')
5  .range(20, 39)
6  .select('id, name, created_at')

Composing modifiers

Mix and match with filters:

1const recent = await db
2  .from('posts')
3  .eq('published', true)
4  .order('created_at', { ascending: false })
5  .limit(5)
6  .select('id, title, created_at')

Types & validation

  • Column names used in order are checked against dbSpec.schema.
  • limit and range expect positive integers and valid windows.
  • Return shapes are inferred from your projection.

Adapter notes

  • SQLite (Web / Expo): modifiers translate to SQLite query clauses; ensure your migrations (SQL DDL) align with indexed columns for performance.
  • Supabase (bundled in @vibecode-db/client): modifiers map to PostgREST via Supabase; ensure columns/types match and RLS permits reads.

Summary

Apply order, limit, and range to shape results deterministically. Compose with filters, then execute with .select(...). Same fluent surface across adapters—only storage changes, not your code.