Skip to content

noConvexCollectInQuery (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noConvexCollectInQuery": "error"
}
}
}
}

Disallow loading every result of a Convex database query with .collect().

As a table grows, .collect() returns more and more documents, until the query fails at the Convex query limits. Use .take() or .paginate() instead.

const messages = await ctx.db.query("messages").collect();
code-block.js:1:49 lint/nursery/noConvexCollectInQuery ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This database query loads every matching document with .collect().

> 1 │ const messages = await ctx.db.query("messages").collect();
│ ^^^^^^^^^
2 │

ℹ A query that returns many documents uses a lot of bandwidth, makes mutations more likely to conflict, and fails once it reaches the Convex query limits.

ℹ Use .take() to load only the first few documents, or .paginate() to load them one page at a time. If this query always returns a small number of documents, suppress this diagnostic with a biome-ignore comment.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const messages = await ctx.db
.query("messages")
.withIndex("by_channel", (q) => q.eq("channel", channel))
.order("desc")
.collect();
code-block.js:5:6 lint/nursery/noConvexCollectInQuery ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This database query loads every matching document with .collect().

3 │ .withIndex("by_channel", (q) => q.eq("channel", channel))
4 │ .order("desc")
> 5 │ .collect();
│ ^^^^^^^^^
6 │

ℹ A query that returns many documents uses a lot of bandwidth, makes mutations more likely to conflict, and fails once it reaches the Convex query limits.

ℹ Use .take() to load only the first few documents, or .paginate() to load them one page at a time. If this query always returns a small number of documents, suppress this diagnostic with a biome-ignore comment.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const latest = await ctx.db.query("messages").order("desc").take(10);
const page = await ctx.db.query("messages").paginate(paginationOpts);
const first = await ctx.db.query("messages").first();