Skip to content

noConvexQueryFilter (JavaScript)

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

Disallow calling .filter() on a Convex database query.

When a Convex query uses .filter(), the database still reads every document in the table (or in the range selected by an index), and only then drops the ones that don’t match. As the table grows, the query gets slower and uses more of your database bandwidth.

Use .withIndex() with an index on the fields you want to match, so the database only reads the matching documents. When an index isn’t worth adding, collect the results and filter them in your own code: this reads the same documents as .filter(), but makes the cost visible in the code.

The rule recognizes queries that start with ctx.db.query(), db.query(), or db.privateSystem.query(), including queries stored in a const variable.

const messages = await ctx.db
.query("messages")
.filter((q) => q.eq(q.field("author"), args.author))
.collect();
code-block.js:3:6 lint/nursery/noConvexQueryFilter ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Avoid calling .filter() on a Convex database query.

1 │ const messages = await ctx.db
2 │ .query("messages")
> 3 │ .filter((q) => q.eq(q.field("author"), args.author))
│ ^^^^^^
4 │ .collect();
5 │

ℹ The database reads every document in the query before .filter() drops the ones that don't match, so the query gets slower as the table grows.

ℹ Use .withIndex() to read only the matching documents, or collect the results and filter them in your code.

ℹ 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 query = ctx.db.query("messages");
const messages = await query.filter((q) => q.eq(q.field("author"), args.author)).collect();
code-block.js:2:30 lint/nursery/noConvexQueryFilter ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ Avoid calling .filter() on a Convex database query.

1 │ const query = ctx.db.query("messages");
> 2 │ const messages = await query.filter((q) => q.eq(q.field("author"), args.author)).collect();
│ ^^^^^^
3 │

ℹ The database reads every document in the query before .filter() drops the ones that don't match, so the query gets slower as the table grows.

ℹ Use .withIndex() to read only the matching documents, or collect the results and filter them in your code.

ℹ 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_author", (q) => q.eq("author", args.author))
.collect();
const allMessages = await ctx.db.query("messages").collect();
const authorMessages = allMessages.filter((message) => message.author === args.author);