Aggregates
exists()
Section titled “exists()”Returns boolean indicating if any rows match. Uses SELECT EXISTS() for optimal performance.
const hasAdmin = await db.users.where({ role: 'admin' }).exists();// true or falsecount()
Section titled “count()”Table-level count
Section titled “Table-level count”const result = await db.users.count({ status: true });// { success: true, data: [{ count: 42 }] }Chainable count
Section titled “Chainable count”const result = await db.users.where({ status: true }).count();// { success: true, data: 42 }distinct()
Section titled “distinct()”Get distinct values for a column.
const roles = await db.users.distinct('role');// { success: true, data: ['admin', 'member', 'viewer'] }Performance Tips
Section titled “Performance Tips”- Use
first()instead ofmany()[0]- AddsLIMIT 1automatically - Use
exists()for boolean checks - UsesSELECT EXISTS()subquery - Use
select()to limit columns - Reduces data transfer - Use
whereIn()for batch lookups - Single query instead of N queries - Batch inserts - Pass array to
insert()for single statement