listMessages.ts19 lines · main
| 1 | import { brivenError, query, type Ctx } from '@briven/cli/server'; |
| 2 | |
| 3 | interface Args { |
| 4 | roomId: string; |
| 5 | limit?: number; |
| 6 | } |
| 7 | |
| 8 | export default query(async (ctx: Ctx, args: Args) => { |
| 9 | if (!args.roomId) |
| 10 | throw new brivenError('validation_failed', 'roomId is required', { status: 400 }); |
| 11 | |
| 12 | const limit = Math.min(Math.max(args.limit ?? 50, 1), 200); |
| 13 | return ctx |
| 14 | .db('messages') |
| 15 | .select(['id', 'roomId', 'authorName', 'body', 'createdAt']) |
| 16 | .where({ roomId: args.roomId }) |
| 17 | .orderBy('createdAt', 'desc') |
| 18 | .limit(limit); |
| 19 | }); |