Querying
Read rows with filters, ordering, pagination, includes, and aggregates
All rows
const todos = await db.orm.todo.all();all() returns an async iterable result. You can await it as an array or call .toArray().
const todos = await db.orm.todo.where({ done: false }).all().toArray();Find by primary key
const user = await db.orm.user.findUnique("user_1");findUnique() takes the primary key value directly and returns null when no row exists.
First match
const firstOpenTodo = await db.orm.todo.where({ done: false }).first();first() returns null when no row matches.
Filtering
Use shorthand equality filters for common cases:
const openTodos = await db.orm.todo.where({ done: false, userId: "user_1" }).all();Use callback filters for operators:
const recent = await db.orm.todo.where((todo) => todo.title.contains("release")).all();Available field operators include eq, neq, gt, gte, lt, lte, in, notIn, contains, startsWith, endsWith, isNull, and isNotNull.
Logical helpers are exported from @prisma-next-idb/client-idb/orm:
import { and, or } from "@prisma-next-idb/client-idb/orm";
const todos = await db.orm.todo
.where((todo) => or(todo.title.contains("release"), and(todo.done.eq(false), todo.userId.eq("user_1"))))
.all();Sorting and pagination
const page = await db.orm.todo.orderBy({ title: "asc" }).skip(20).take(10).all();Select
const summaries = await db.orm.todo.select("id", "title").all();Include relations
const todos = await db.orm.todo.include("user").all();Refine included to-many relations:
const users = await db.orm.user.include("todos", (todos) => todos.where({ done: false }).take(5)).all();Count an included to-many relation:
const users = await db.orm.user.include("todos", (todos) => todos.count()).all();Count
const count = await db.orm.todo.where({ done: false }).count();skip() and take() affect count(). If you need the total matching rows, count before adding pagination.
Aggregates
const stats = await db.orm.todo.aggregate((agg) => ({
total: agg.count(),
}));Numeric fields can use sum, avg, min, and max:
const stats = await db.orm.task.aggregate((agg) => ({
total: agg.count(),
avgPriority: agg.avg("priority"),
}));Group by
const byUser = await db.orm.todo.groupBy("userId").aggregate((agg) => ({
total: agg.count(),
}));groupBy() returns one aggregate row per distinct group key.