createTodo.ts 599 B

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