We are trying to receive a y-doc from the client and store it in MongoDB. After confirming that typeof(doc) was an object, we tried the following three methods.
Convert to string and store: JSON.stringify(doc)
doc.JSON()
Save doc in schema as it is.
we need your help because we failed everything.
How can y-doc be stored in a commercial DB (mongoDB) rather than y-leveldb, y-indexdb?
we are university student
You can encode the document to a binary object and transform it back to a Yjs document. This process is documented here: Document Updates - Yjs Docs
// encode the document to a binary object:
storeInDatabase(Y.encodeStateAsUpdate(ydoc))
loadFromDatabase().then(state => {
const ydoc = new Y.Doc()
Y.applyUpdate(ydoc)
ydoc // should now have the same information as the initial document
})
Sorry for re opening this thread after so long. For example when I do Y.encodeStateAsUpdate(ydoc1) I get a UInt8Array with value { "0": 1, "1": 1, "2": 136, "3": 145, "4": 193, "5": 243, "6": 6, "7": 0, "8": 4, "9": 1, "10": 11, "11": 99, "12": 111, "13": 110, "14": 116, "15": 101, "16": 110, "17": 116, "18": 72, "19": 84, "20": 77, "21": 76, "22": 1, "23": 100, "24": 0 }
lets store it in foo and do Y.applyUpdate(ydoc2, foo) and boom I get an error.
Unhandled Runtime Error
Error: Unexpected end of array
It happens even if I use the UintArray constructor like this new Uint8Array(foo). I have been banging my head since last week solving this error. What am I missing here?
⇐ This is not a Uint8Array. This is a stringified JSON.stringify’d Uint8Array. There is a section about how you should transmit data over the network: Document Updates | Yjs Docs