createTodo.ts20 lines · main
| 1 | import { brivenError, mutation, type Ctx } from '@briven/cli/server'; |
| 2 | import { ulid } from '@briven/shared'; |
| 3 | |
| 4 | interface Args { |
| 5 | body: string; |
| 6 | } |
| 7 | |
| 8 | export default mutation(async (ctx: Ctx, args: Args) => { |
| 9 | const body = args.body?.trim(); |
| 10 | if (!body) throw new brivenError('validation_failed', 'body is required', { status: 400 }); |
| 11 | if (body.length > 280) |
| 12 | throw new brivenError('validation_failed', 'body too long (max 280 chars)', { status: 400 }); |
| 13 | |
| 14 | const id = ulid('td'); |
| 15 | const [row] = await ctx |
| 16 | .db('todos') |
| 17 | .insert({ id, body, done: false }) |
| 18 | .returning(); |
| 19 | return row; |
| 20 | }); |