Solid Automerge - v3.0.0
    Preparing search index...

    Solid Automerge - v3.0.0

    Solid Automerge

    Solid primitives for Automerge .

    this is 3.x, for Solid 2.0. for Solid 1.x, use solid-automerge@2.

    pnpm add solid-js @automerge/automerge-repo solid-automerge
    

    Get a fine-grained live view of an automerge document, and its handle, from its URL.

    Returns [doc, handle].

    useDocument<T>(
    url: AutomergeUrl | (() => AutomergeUrl | undefined),
    options?: {repo?: Repo}
    ): [doc: Doc<T>, handle: Accessor<DocHandle<T> | undefined>]
    // example
    const [doc, handle] = useDocument<{count: number}>(() => props.url)

    const inc = () => handle()?.change(doc => doc.count++)
    return <button onClick={inc}>{doc.count}</button>

    doc is a Solid store, so you read it like any object (doc.items[1].title, no doc()) and only what you read is tracked. It's the same store for its whole life: when the url changes, the new document is reconciled into it, so only the parts that are different notify.

    When the handle receives changes, the automerge patches are applied to the store, giving you fine-grained reactivity that's consistent across space and time.

    • while the document is loading, reading doc suspends. put a <Loading> around it
    • if the document can't be found, reading it errors to the nearest <Errored>
    • when there's no url, doc is empty and handle() is undefined

    The {repo} option can be left out if you are using RepoContext.

    Like useDocument, but you can assign to the document, and the assignment becomes an automerge change.

    const [todo, handle] = useMutableDocument<Todo>(() => props.url)

    return (
    <input
    type="checkbox"
    checked={todo.done}
    onChange={event => (todo.done = event.currentTarget.checked)}
    />
    )

    Assignment, delete, push, pop, shift, unshift, splice, fill, and automerge's own insertAt and deleteAt all work, anywhere in the document. Each one is its own change: to make several edits in one change, use handle()?.change.

    sort, reverse and copyWithin throw. Automerge has no way to move an item in a list, so they would have to delete every item and insert it again, which turns nested objects into copies and merges badly with anyone else editing the list. If you really want that, splice the list into its new order yourself.

    Nested objects are views of whatever is at their path right now, so todo.tags always means the current tags array.

    Get a mutable version of any document projection. Underlying primitive for useMutableDocument.

    mutable<T>(doc: Doc<T>): T
    
    const todo = mutable(makeDocumentProjection<Todo>(handle))
    todo.tags.push("urgent")

    Mount another document on a key of a document projection.

    mount(
    projection: Doc<T>,
    key: string,
    document: Doc<U> | DocHandle<U> | (() => DocHandle<U> | undefined)
    ): () => void
    // example
    const [folder] = useDocument<Folder>(() => props.url)
    const [readme] = useDocument<Readme>(() => folder.readmeUrl)
    mount(folder, "readme", readme)

    return <h1>{folder.readme.title}</h1>

    The mounted document's patches are applied to the projection under key, so folder.readme is a fine-grained live view of the readme document inside the folder's store (snapshot, deep and <For> all see it). Whatever the folder document itself has at readme is hidden while something is mounted there.

    mount returns a function that unmounts it again, and it's unmounted when the owner that called mount is cleaned up.

    Changing the folder through its handle only changes the folder. To change the readme, use the readme's own handle, or write through a mutable folder: writes under readme go to the readme document.

    If you're using TypeScript, include the mounted key in the parent's type (e.g. useDocument<Folder & {readme: Readme}>).

    Get a fine-grained live view from a signal (or memo) of a DocHandle.

    Underlying primitive for useDocument. Works with useDocHandle.

    createDocumentProjection<T>(handle: () => DocHandle<T> | undefined): Doc<T>
    
    // example
    const [handle, setHandle] = createSignal(repo.create({items: [{title: "hi"}]}))
    const doc = createDocumentProjection<{items: {title: string}[]}>(handle)

    // subscribes fine-grained to doc.items[0].title
    return <h1>{doc.items[0].title}</h1>

    Just like createDocumentProjection, but without a reactive input.

    Underlying primitive for createDocumentProjection.

    makeDocumentProjection<T>(handle: DocHandle<T>): Doc<T>
    
    // example
    const handle = await repo.find(url)
    const doc = makeDocumentProjection<{items: {title: string}[]}>(handle)

    // subscribes fine-grained to doc.items[1].title
    return <h1>{doc.items[1].title}</h1>

    Both projection functions work with sub-handles (handle.sub("items", 0)), and take an options object that's passed through to Solid's createProjection: name, and key for how array items are matched up when a whole document is reconciled (defaults to "id", null matches by position).

    Each projection is a Solid 2.0 createProjection whose derive function returns an async iterable that follows the handle:

    1. the first value is the document as it is right now, which Solid reconciles into the store. it's served synchronously, so a document that's already loaded is never pending
    2. after that, each change's automerge patches are applied to the projection's draft as they arrive, and the iterable yields once per batch of changes
    3. when a change can't be described with patches (a sub-handle's scope was replaced, the document was deleted) it yields the new document instead, and Solid reconciles it

    When the handle changes, Solid closes the old iterator (which stops listening to the old handle) and starts a new one. Items that move around in a list keep their identity, so <For> moves their rows instead of remaking them.

    A light coarse-grained primitive when you care only that a doc has changed, and not how. Returns [doc, handle].

    useDocSignal<T>(
    url: AutomergeUrl | (() => AutomergeUrl | undefined),
    options?: {repo: Repo}
    ): [doc: Accessor<Doc<T> | undefined>, handle: Accessor<DocHandle<T> | undefined>]
    // example
    const [doc, handle] = useDocSignal<{count: number}>(() => props.url)

    const inc = () => handle()?.change(doc => doc.count++)
    return <button onClick={inc}>{doc()?.count}</button>

    The {repo} option can be left out if you are using RepoContext.

    A light coarse-grained primitive when you care only that a doc has changed, and not how. Takes a signal of a DocHandle.

    Underlying primitive for useDocSignal.

    Works with useDocHandle.

    createDocSignal<T>(handle: () => DocHandle<T> | undefined): Accessor<Doc<T> | undefined>
    

    Just like createDocSignal, but without a reactive input.

    Underlying primitive for createDocSignal.

    makeDocSignal<T>(handle: DocHandle<T>): Accessor<Doc<T> | undefined>
    
    // example
    const handle = await repo.find(url)
    const doc = makeDocSignal<{count: number}>(handle)

    return <span>{doc()?.count}</span>

    Get a DocHandle from the repo as an async memo.

    Perfect for handing to createDocumentProjection.

    useDocHandle<T>(
    url: AnyDocumentId | (() => AnyDocumentId | undefined),
    options?: {repo: Repo}
    ): Accessor<DocHandle<T> | undefined>
    const handle = useDocHandle(() => props.url, {repo})
    // or
    const handle = useDocHandle(() => props.url)

    When the repo already has the document you get the handle straight away. Otherwise the memo is pending until it's found (reading it suspends to the nearest <Loading>), or errors (to the nearest <Errored>) if it can't be. It's undefined when there's no url.

    The repo option can be left out if you are using RepoContext.

    Turn a change payload into a draft function for a store setter, for when you're wiring up a store yourself.

    handle.on("change", payload => setStore(autoproduce(payload)))
    

    If you prefer the context pattern for some reason, you can pass the repo higher up in your app with RepoContext

    A convenience context for Automerge-Repo Solid apps. Optional: if you prefer you can pass a repo as an option to useDocHandle and useDocument.

    <RepoContext value={repo}>
    <App />
    </RepoContext>

    Get the repo from the context.

    useRepo(): Repo
    
    const repo = useRepo()
    
    • document projections are stores now, not accessors: doc()?.title becomes doc.title
    • useDocHandle returns a memo instead of a resource. put a <Loading> where you had a <Suspense>, and read handle() where you read handle.latest
    • createDocumentProjection returns the store itself, not an accessor of one
    • <RepoContext.Provider value={repo}> is <RepoContext value={repo}>

    there's a codemod that does most of that for you. upgrade Solid to 2.0 first, then:

    npx jscodeshift -t node_modules/solid-automerge/codemod/3.0.ts src
    

    it rewrites doc()?.title to doc.title, handle.latest to handle() and RepoContext.Provider to RepoContext, for everything imported from solid-automerge. where the meaning changed in a way it can't fix (a document that was undefined while loading now suspends, a resource's .loading), it leaves a // TODO(solid-automerge@3): comment. run your formatter afterwards.

    skills/solid-automerge/SKILL.md is an agent skill covering the 3.x API, the mistakes agents make with it, and upgrading from 2.x. it ships in the package, so point your agent at node_modules/solid-automerge/skills/solid-automerge/SKILL.md, or copy it into your skills directory (.claude/skills/ for Claude Code).