Canica

Files

Uploads, Convex-style: the page uploads the file, gets its id, and passes the id to an ordinary mutation that stores it in a file() field. Queries turn ids into links.

// server/index.ts
import { file, Files, … } from "canica/server"
todos: table({ text: string(), owner: userId(), image: file().optional() }) // an Id<"_files">

todos: query(function* () {
  const files = yield* Files // url, meta; mutations also delete
  const list = yield* (yield* Db).select().from(todos).where(eq(todos.owner, (yield* Viewer).userId))
  // "medium": the image at most 640 px wide (WebP); thumbnails ask a smaller size of the same link
  return list.map((t) => ({ ...t, imageUrl: t.image ? files.url(t.image, { size: "medium" }) : null }))
}),
setImage: mutation({ id: id("todos"), image: file().optional() }, function* ({ id, image }) {
  const todo = yield* ownTodo(id)
  yield* (yield* Db).update(todos).set({ image: image ?? null }).where(eq(todos.id, id))
  if (todo.image && todo.image !== image) yield* (yield* Files).delete(todo.image) // or let the cleanup do it
}),
// in a component (a modal, a form): FileInput uploads with progress, then calls onUpload with the id
<FileInput label="Image" accept="image/*" description="Up to 25 MB" onUpload={(image) => void setImage({ id, image })} />
{todo.imageUrl && <Thumbnail src={todo.imageUrl} alt={`Image of ${todo.text}`} size="sm" />} // sm md lg xl
// your own UI: const upload = useUpload(); const r = await upload(file) → r.value is the id; .progress, .waiting, .status