getCount.ts 506 B

12345678910111213141516
  1. import { query, type Ctx } from '@briven/cli/server';
  2. interface Args {
  3. id?: string;
  4. }
  5. /**
  6. * Reactive query — returns the current count for the named counter.
  7. * A missing row is treated as 0 so the first subscriber doesn't need
  8. * to seed the counters table first.
  9. */
  10. export default query(async (ctx: Ctx, args: Args) => {
  11. const id = args.id ?? 'default';
  12. const [row] = await ctx.db('counters').select(['count']).where({ id }).limit(1);
  13. return { id, count: row ? Number(row.count) : 0 };
  14. });