automerge / hypermerge

Build p2p collaborative applications without any server infrastructure in Node.js
MIT License
1.28k stars 66 forks source link

[Question] How to persist and load previous saved data #73

Closed wallynm closed 1 year ago

wallynm commented 4 years ago

Hello everybody, im trying to use the project following the Readme instructions but still i couldnt understand some topics like - how do i control the file persistence on each peer.

I've saw the examples folders even the parameter path that we can set into Repo but, i don't understand how to load the data.

This was more clear into automerge project, i did started with automerge and then realized that what i was looking for was hypermege because of p2p updates, could anyone help me with this? Thanks (:

jankaszel commented 4 years ago

hi @wallynm! i'd say it's important to note that while automerge provides you "just" with functionality for merging different versions of a document (and for expressing these changes), hypermerge is a more opinionated library for exchanging changes between peers in a decentralized network.

to achieve that, hypermerge uses hyperswarm (https://github.com/hyperswarm/hyperswarm) for networking and hypercore (https://github.com/mafintosh/hypercore) for persisting changes into a data structure called an append-only log. (needless to say hypermerge uses automerge for merging changes).

hypercore has its own method of persisting state onto disk—this state will rather describe the log than something you'd expect from Automerge.save(), and hence, the state of a hypercore log can be considered more like a set of changes over time than a definite state. you can read on that here: https://github.com/automerge/hypermerge/blob/af7e10402389441cf458d606cf3c138aa67006fa/ARCHITECTURE.md#storage

i'd propose to not tinker with the state of a hypercore itself—and particularly not the binary data written onto disk—but to rather use repo.doc(url, state => { ... }) for getting the JSON representation of your document.