Count entities
You can count how many entities a query matches without fetching them, or get the total with a page of rows. The examples on this page use the Posts schema and clients from your first entity.
Count without fetching rows
Section titled “Count without fetching rows”count takes the same filters as select: from, where, ownedBy and createdBy. fetch returns the total as a bigint:
import { eq } from "@arkiv-network/sdk/query"
const total = await publicClient .count() .from(Posts) .ownedBy(account.address) .where(eq("category", "news")) .fetch()Without from, conditions match attributes by name, as in a query without a schema.
A count needs at least one condition, or it throws InvalidPredicateError. A named schema counts as one, so count().from(Posts) counts every post.
Count alongside a page
Section titled “Count alongside a page”To show the total next to a list, add withCount to a select. The count comes back in the same request, as the page’s count:
const page = await publicClient .select({ $key: true, title: true }) .from(Posts) .ownedBy(account.address) .withCount() .limit(20) .fetch()
console.log(`${page.entities.length} of ${page.count}`)Each page reads the latest block, so its count is the total at that block. To number the pages, count once before you fetch them.
Entities that don’t fit the schema
Section titled “Entities that don’t fit the schema”The count includes entities a page leaves out for not fitting the schema. So a walk through every page can return fewer rows than count. Each page’s skipped says how many it left out.