LabL
Convex Community3y ago
4 replies
Lab

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) }
                    : {}),
            }))
        );
    },
});
Was this page helpful?