@sanity/mutate
TypeScript icon, indicating that this package has built-in type declarations

0.18.1 • Public • Published

@sanity/mutate

@sanity/mutate

[!NOTE] Experimental. Use at your own risk.

A TypeScript toolkit for Sanity mutations:

  • Declarative, composable mutation creators
  • A pure, in-memory apply engine
  • An optimistic local store with rebase semantics

Install

npm install @sanity/mutate

Example

import {
  at,
  create,
  createIfNotExists,
  patch,
  SanityEncoder,
  set,
  setIfMissing,
} from '@sanity/mutate'

const projectId = '<projectId>'
const dataset = '<dataset>'

const mutations = [
  create({_type: 'dog', name: 'Fido'}),
  createIfNotExists({_id: 'document-1', _type: 'someType'}),
  patch('other-document', [
    at('published', set(true)),
    at('address', setIfMissing({_type: 'address'})),
    at('address.city', set('Oslo')),
  ]),
]

await fetch(
  `https://${projectId}.api.sanity.io/v2026-05-12/data/mutate/${dataset}`,
  {
    method: 'POST',
    mode: 'cors',
    credentials: 'include',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(SanityEncoder.encodeAll(mutations)),
  },
)

Documentation

Full reference and guides at https://mutate.sanity.dev (sources in docs/; run the site locally with pnpm docs:dev):

License

MIT

Readme

Keywords