Skip to content

noConvexDuplicateIndexes (JavaScript)

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

Disallow Convex database indexes whose fields are the first fields of another index on the same table.

A query can use a Convex index to filter on any number of its first fields, so an index on ["author", "channel"] already answers every query that an index on ["author"] can. Every index slows down writes to the table and takes up storage, so the shorter index only adds cost. Indexes with identical fields are reported too.

Staged indexes (staged: true) are ignored: staging the longer index while the shorter one is still in use is how you replace one with the other.

Convex appends _creationTime to every index. If you need results sorted by the shorter index’s fields followed by _creationTime, keep both and suppress this rule on the shorter one.

import { defineTable } from "convex/server";
import { v } from "convex/values";
defineTable({ author: v.string(), channel: v.string() })
.index("by_author", ["author"])
.index("by_author_and_channel", ["author", "channel"]);
code-block.js:5:6 lint/nursery/noConvexDuplicateIndexes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The index by_author is redundant because the index by_author_and_channel starts with the same fields.

4 │ defineTable({ author: v.string(), channel: v.string() })
> 5 │ .index("by_author", ["author"])
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ .index("by_author_and_channel", ["author", "channel"]);
7 │

ℹ The index by_author_and_channel is defined here.

4 │ defineTable({ author: v.string(), channel: v.string() })
5 │ .index("by_author", ["author"])
> 6 │ .index("by_author_and_channel", ["author", "channel"]);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 │

ℹ Every index slows down inserts and updates and takes up storage. Queries that use by_author find the same documents with by_author_and_channel, so delete by_author and use by_author_and_channel instead.

ℹ Keep both indexes only if you need the documents in a different order: by_author sorts matching documents by _creationTime, while by_author_and_channel sorts them by channel.

ℹ 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.

import { defineTable } from "convex/server";
import { v } from "convex/values";
defineTable({ author: v.string() })
.index("by_author", ["author"])
.index("by_writer", ["author"]);
code-block.js:6:6 lint/nursery/noConvexDuplicateIndexes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The index by_writer has the same fields as the index by_author.

4 │ defineTable({ author: v.string() })
5 │ .index("by_author", ["author"])
> 6 │ .index("by_writer", ["author"]);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7 │

ℹ The index by_author is defined here.

4 │ defineTable({ author: v.string() })
> 5 │ .index("by_author", ["author"])
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ .index("by_writer", ["author"]);
7 │

ℹ Both indexes find the same documents in the same order, but each one slows down inserts and updates and takes up storage. Delete by_writer and use by_author instead.

ℹ 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.

import { defineTable } from "convex/server";
import { v } from "convex/values";
// Neither index starts with all the fields of the other one.
defineTable({ author: v.string(), channel: v.string() })
.index("by_channel", ["channel"])
.index("by_author_and_channel", ["author", "channel"]);
// The longer index is staged, so the shorter one is still needed until it's ready.
defineTable({ author: v.string(), channel: v.string() })
.index("by_author", ["author"])
.index("by_author_and_channel", { fields: ["author", "channel"], staged: true });
// To query by `author` alone, use the longer index and filter only on its first field.
await ctx.db
.query("messages")
.withIndex("by_author_and_channel", (q) => q.eq("author", author))
.collect();