npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@automerge/automerge-repo-svelte-store

v2.5.1

Published

A Svelte store containing your automerge documents

Readme

Svelte store for Automerge Repo

A reactive Svelte store for Automerge documents. Compatible with Svelte 3, 4, and 5.

Installation

npm install @automerge/automerge-repo-svelte-store

Example Usage

Basic Usage

<script>
  import { Repo } from "@automerge/automerge-repo"
  import { createAutomergeStore } from "@automerge/automerge-repo-svelte-store"

  // Create a repo
  const repo = new Repo({
    // Configuration...
  })

  // Create a store from the repo
  const automergeStore = createAutomergeStore(repo)

  // Load or create a document
  let documentStore = null

  async function loadDocument(url) {
    documentStore = await automergeStore.find(url)
  }

  async function createDocument() {
    documentStore = await automergeStore.create({ count: 0 })
  }

  // Update the document
  function incrementCounter() {
    if (documentStore) {
      documentStore.change(doc => {
        doc.count = (doc.count || 0) + 1
      })
    }
  }
</script>

Using with Svelte Context (recommended)

<!-- App.svelte -->
<script>
  import { Repo } from "@automerge/automerge-repo"
  import { setContextRepo } from "@automerge/automerge-repo-svelte-store"
  import Counter from './Counter.svelte'

  const repo = new Repo({
    // Configuration...
  })

  // Make repo available to child components
  setContextRepo(repo)

  // Create a document asynchronously
  let docUrl = null

  async function setupDoc() {
    const handle = await repo.create({ count: 0 })
    docUrl = handle.url
  }

  setupDoc()
</script>

{#if docUrl}
  <Counter {docUrl} />
{:else}
  <p>Creating document...</p>
{/if}
<!-- Counter.svelte -->
<script>
  import { document } from "@automerge/automerge-repo-svelte-store"
  import { onMount } from "svelte"

  export let docUrl

  // The document store - initially null
  let docStore = null

  // Load the document on mount
  onMount(async () => {
    // document() is async and returns a Promise
    docStore = await document(docUrl)
  })

  function increment() {
    if (!docStore) return

    // Access the document using the Svelte store $ syntax
    docStore.change(doc => {
      doc.count = (doc.count || 0) + 1
    })
  }
</script>

{#if docStore}
  <button on:click={increment}>
    Count: {$docStore.count || 0}
  </button>
{:else}
  <p>Loading document...</p>
{/if}

Using with Svelte 5 Runes

With Svelte 5, you can use the store with runes syntax for a more ergonomic experience:

<script>
  import { document } from "@automerge/automerge-repo-svelte-store"

  export let docUrl

  // Initialize state
  let docStore = $state(null)
  let loading = $state(true)
  let error = $state(null)
  let count = $derived(docStore?.$doc?.count || 0)

  // Load document when component initializes
  $effect(() => {
    async function loadDocument() {
      try {
        loading = true
        docStore = await document(docUrl)
        loading = false
      } catch (err) {
        console.error("Failed to load document:", err)
        error = err
        loading = false
      }
    }

    loadDocument()
  })

  function increment() {
    if (docStore) {
      docStore.change(doc => {
        doc.count = (doc.count || 0) + 1
      })
    }
  }
</script>

{#if loading}
  <p>Loading document...</p>
{:else if error}
  <p>Error: {error.message}</p>
{:else}
  <button on:click={increment}>
    Count: {count}
  </button>
{/if}

API Reference

createAutomergeStore(repo)

Creates an Automerge store factory from a repo instance.

setContextRepo(repo)

Sets the Automerge repo in the current component's context.

getContextRepo()

Gets the Automerge repo from the current component's context.

document(docIdOrUrl, repoInstance?)

Convenience method to load a document from the repo in context or a provided repo. Important: This function returns a Promise that resolves to the document store.

Store API

The document store returned by find() and create() implements Svelte's writable store contract with these additional methods:

  • change(fn): Make changes to the document
  • url: Get the document URL
  • documentId: Get the document ID
  • handle: Access the underlying Automerge document handle

Contributors

Originally written by Dylan MacKenzie (@ecstatic-morse). Updated for Svelte 5 compatibility by the Automerge team.