Lab
Lab•14mo ago

Mark a Query as no-store?

Hiya folks, I'm trying to build a quiz app using Convex, and currently I have a 'Try again' button at the end of the page, but I would like it to fetch completely fresh question data from my Convex db instead of the same queried data again. So my question is if there's any way to designate a query function not to cache? This is my current query implemented:
import { Id } from './_generated/dataModel';
import { query } from './_generated/server';

const shuffle = (
array: {
_id: Id<'rooms'>;
_creationTime: number;
type: string;
imageId: string;
title: string;
}[]
) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};

export const get = query({
args: {},
handler: async (ctx) => {
let rooms = await ctx.db.query('rooms').collect();
const shuffledRooms = shuffle(rooms).slice(0, 10);
return await Promise.all(
shuffledRooms.map(async (room) => ({
...room,
...(room.imageId
? { url: await ctx.storage.getUrl(room.imageId) }
: {}),
}))
);
},
});
import { Id } from './_generated/dataModel';
import { query } from './_generated/server';

const shuffle = (
array: {
_id: Id<'rooms'>;
_creationTime: number;
type: string;
imageId: string;
title: string;
}[]
) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};

export const get = query({
args: {},
handler: async (ctx) => {
let rooms = await ctx.db.query('rooms').collect();
const shuffledRooms = shuffle(rooms).slice(0, 10);
return await Promise.all(
shuffledRooms.map(async (room) => ({
...room,
...(room.imageId
? { url: await ctx.storage.getUrl(room.imageId) }
: {}),
}))
);
},
});
3 Replies
ian
ian•14mo ago
You can provide the random value from the client, like randomSeed and use that with math random to control client-side when the shuffling happens Or you can make it a mutation to run fresh every time.
jamwt
jamwt•14mo ago
@Lab when I did this, I ended up using a client-provided seed. that way the client can control if it wants the same result (maybe when the user didn't finish it closed/resumed the app?) or a different one
Lab
LabOP•14mo ago
Thank you both! I passed a client seed into the query args and the shuffle function from my frontend and it works exactly how I wanted it to 🎉

Did you find this page helpful?