PatolordP
Convex Community2y ago
4 replies
Patolord

How do I count items on a table?

I want to do the equivalent of this:
SELECT COUNT(*) FROM table_name;

In Convex i tried this but it seems not performant:
export const countMaterialsTable = query({ handler: async (ctx) => { const materials = await ctx.db.query("materials").collect(); return materials.length; },

I Installed convex cache helper thinking it may help since i dont need the counter to be that reactive.
- ConvexQueryCacheProvider (provider.tsx)
- UseQuery (hooks.ts)

import { useQuery } from "convex-helpers/react/cache/hooks"; // ... const materials = useQuery(api.materials.countMaterialsTable);

It worked fine on the development server, but when i deployed to vercel my cache was not working ( i attached the console.log on debug mode, it loops every 5 seconds or so),
between the actual value "185" and undefined.
debu.png
Was this page helpful?