#local-storage #auto-merge #persistent #document #persistence #wrapper #persisting

automerge-persistent-localstorage

A web-based localstorage adapter for persisting Automerge documents

4 releases (breaking)

0.4.0 May 13, 2023
0.3.0 Mar 8, 2023
0.2.0 Dec 20, 2022
0.1.0 Sep 26, 2022

#3 in #persisting

MIT license

39KB
677 lines

Automerge persistent

This project extends automerge-rs with some persistence. There is a core trait for what functionality a persister should have and a backend wrapper struct to utilise this.

For now, see the benches for an example of a sled backend. Adding more backends to this repo would be very much appreciated.

Good backends to have would be:

  • memory (for some testing scenarios)
  • sled
  • localstorage
  • indexeddb
  • filesystem
  • other suggestions welcome!

Usage

The PersistentBackend struct should be the main point of reference and should fit in place of a normal Backend. It can load from the persistent storage and automatically saves on the appropriate actions (but more efficiently). Occasionally the user should schedule a call to compact if storage and load time are of concern. This gathers the changes and saves the backend in the more compressed form, then the old changes are removed.


lib.rs:

A persister targetting LocalStorage in the browser.

let storage = web_sys::window()
    .unwrap()
    .local_storage()
    .map_err(LocalStoragePersisterError::StorageError)?
    .unwrap();

let persister = LocalStoragePersister::new(storage, "document".to_owned(), "changes".to_owned(), "sync-states".to_owned())?;
let doc = PersistentAutomerge::load(persister).unwrap();

Dependencies

~13MB
~266K SLoC