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