This site counts visits with analytics cookies only if you accept. Read the consent

Skip to content

ExamplesCRUD Operations

CRUD Operations

Full create, read, update, and delete cycle with useMutation and useQuery.

Full CRUD

Manage the users of the docs backend with every HTTP method: create one, pick one to update or patch, or delete one. The requests below show each change, then the list fetched again after it.

ts
import { q } from "@/lib/query"

// List
const { data, refetch } = q.users.useQuery("getAll")

// Create
const create = q.users.useMutation("create", {
  onSuccess: () => refetch(),
})

// Update (PUT — full replace)
const update = q.users.useMutation("update", {
  onSuccess: () => refetch(),
})

// Patch (PATCH — partial update)
const patch = q.users.useMutation("patch", {
  onSuccess: () => refetch(),
})

// Delete
const remove = q.users.useMutation("remove", {
  onSuccess: () => refetch(),
})
q.users — create · update · patch · removeSANDBOX
    idle
    // run the request to see the response

    Requests

    as the browser recorded them and the site's server told of them

    // no request yet: run the call

    useMutation docs

    Individual Operations

    Each operation on its own, and the endpoint it calls.

    ts
    // CREATE
    const create = q.users.useMutation("create", {
      onSuccess: () => refetch(),
    })
    create.mutate({ name: "New User", email: "new@example.com" })
    
    // UPDATE (PUT)
    const update = q.users.useMutation("update")
    update.mutate({
      id: 1,
      name: "Updated Name",
      email: "updated@example.com",
      role: "moderator",
    })
    
    // PATCH
    const patch = q.users.useMutation("patch")
    patch.mutate({ id: 1, name: "Patched Name" })
    
    // DELETE
    const remove = q.users.useMutation("remove")
    remove.mutate({ id: 1 })
    POST
    users:create creates a user
    PUT
    users:update replaces the whole user
    PATCH
    users:patch changes the fields given
    DELETE
    users:remove deletes the user