Skip to content

noConvexTimeOrRandomInQueries (JavaScript)

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

Disallow reading the current time or generating random values in Convex queries.

Convex caches query results, so a query that reads the current time can return an outdated time, and a query that generates a random value keeps returning the same one.

This rule checks query() and internalQuery() handlers, including the callbacks they pass and the functions in the same file they call, for calls such as Date.now(), new Date(), and Math.random().

Pass the time to the query as an argument instead, and generate random values in a mutation or an action. See the Convex best practices.

export const listRecentPosts = query({
args: {},
handler: async (ctx) => {
const since = Date.now() - 24 * 60 * 60 * 1000;
return await ctx.db
.query("posts")
.withIndex("by_creation_time", (q) => q.gt("_creationTime", since))
.collect();
},
});
code-block.js:4:23 lint/nursery/noConvexTimeOrRandomInQueries ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This query reads the current time with Date.now().

2 │ args: {},
3 │ handler: async (ctx) => {
> 4 │ const since = Date.now() - 24 * 60 * 60 * 1000;
│ ^^^^^^^^^^
5 │ return await ctx.db
6 │ .query("posts")

ℹ Convex caches the result of a query and reuses it until the data the query reads changes, so the time in a cached result can be outdated. Reading the time can also make Convex discard cached results more often than needed.

ℹ Pass the time to the query as an argument, rounded to a coarse unit such as a minute, or have a mutation or a scheduled function store time-based state in the database.

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

export const pickWinner = query({
args: {},
handler: async (ctx) => {
const entries = await ctx.db.query("entries").collect();
return entries[Math.floor(Math.random() * entries.length)];
},
});
code-block.js:5:35 lint/nursery/noConvexTimeOrRandomInQueries ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This query generates a random value with Math.random().

3 │ handler: async (ctx) => {
4 │ const entries = await ctx.db.query("entries").collect();
> 5 │ return entries[Math.floor(Math.random() * entries.length)];
│ ^^^^^^^^^^^^^
6 │ },
7 │ });

ℹ Convex caches the result of a query, so the query keeps returning the same value until the data it reads changes, instead of a new random value each time.

ℹ Generate random values in a mutation or an action 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.

function isExpired(session) {
return session.expiresAt < Date.now();
}
export const getSession = query({
args: {},
handler: async (ctx, args) => {
const session = await ctx.db.get(args.sessionId);
return isExpired(session) ? null : session;
},
});
code-block.js:9:16 lint/nursery/noConvexTimeOrRandomInQueries ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This query calls a function that reads the current time with Date.now().

7 │ handler: async (ctx, args) => {
8 │ const session = await ctx.db.get(args.sessionId);
> 9 │ return isExpired(session) ? null : session;
│ ^^^^^^^^^^^^^^^^^^
10 │ },
11 │ });

ℹ Date.now() is called here.

1 │ function isExpired(session) {
> 2 │ return session.expiresAt < Date.now();
│ ^^^^^^^^^^
3 │ }
4 │

ℹ Convex caches the result of a query and reuses it until the data the query reads changes, so the time in a cached result can be outdated. Reading the time can also make Convex discard cached results more often than needed.

ℹ Pass the time to the query as an argument, rounded to a coarse unit such as a minute, or have a mutation or a scheduled function store time-based state in the database.

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

export const listRecentPosts = query({
args: { since: v.number() },
handler: async (ctx, args) => {
return await ctx.db
.query("posts")
.withIndex("by_creation_time", (q) => q.gt("_creationTime", args.since))
.collect();
},
});
export const formatDate = query({
args: { timestamp: v.number() },
handler: async (ctx, args) => new Date(args.timestamp).toISOString(),
});
export const createPost = mutation({
args: { body: v.string() },
handler: async (ctx, args) => {
await ctx.db.insert("posts", { body: args.body, publishedAt: Date.now() });
},
});