Hey,
I really try to understand how to store a Y.Doc in a database without loading the Y.Docs. I have read this discussion post which, in my opinion, states that this is possible with the code from this GitHub example. However, I don’t understand how and I hope you can help me. As I understand the persistence object of y-websocket, it is necessary to keep the ydoc of bindState to listen to updates. I would love to see an example of how to use it properly. The best I was able to come up with is the following code for bindState:
const persistedYdoc = await ldb.getYDoc(docName);
// when we retrieve it with the ldb function we also flush the database which isnt
// the worst thing we could do
const persistedStateVector = await ldb.getStateVector(docName);
// this function however is faster to use
//const calcPersistedStateVector = Y.encodeStateVector(persistedYdoc);
// in the default code the following value gets saved in the db
// this however leads to the case that multiple complete Y.Docs are saved in the db (https://github.com/fadiquader/y-mongodb/issues/7)
//const newUpdates = Y.encodeStateAsUpdate(ydoc);
// better just get the differences and save those:
const diff = Y.encodeStateAsUpdate(ydoc, persistedStateVector);
// store the new data in db
ldb.storeUpdate(docName, diff);
// send the persisted data to clients
Y.applyUpdate(ydoc, Y.encodeStateAsUpdate(persistedYdoc));
// store updates of the document in db
ydoc.on("update", async update => {
ldb.storeUpdate(docName, update);
});
// cleanup some memory
persistedYdoc.destroy();
However, this isnt even near the solution proposed.