(resolved) Inspect the shared types in a document without prior knowledge of what those types are

Hi there,

I’m trying to do something a little bit nasty and dump an entire Y.Doc to JSON/CBOR-like structures (ignoring history; just the latest state) so that a non-JavaScript (Rust) program can read some interesting parts of the documents and store some data about it in a tracking database.

My first approach to this was to iterate through doc.share.entries() and dump each ‘shared type’ using a recursive helper function. It seemed to work OK.

However, it all fell over when I did this on a freshly-loaded document (as opposed to a document that I created and modified before dumping). From reading up about it, I think this is because I haven’t called doc.getMap("wombat") and so on, to tell it what the shared types are.

Is it possible to dump the structure of the document without knowing which types are being used (or to find out what the types are?).

In my case I could just encode the types in the document itself as a map (…or hardcode it), but I suppose I was just surprised to see no way to do it.

Bah, I think How to to persist share types? answers my question (despite the title) — seems like Y.js isn’t self-describing so it’s something that, if interested, we should layer on top by e.g. encoding the schema in a map.

1 Like

Generally I recommend to put all your data in a YMap so that you can work more reliably with your data. For example, top level YTypes cannot be removed, but fields in a YMap can be removed. If you don’t have such a setup the following snippet may be interesting:

import * as Y from 'yjs'

// Hack to "get" types in order for yDoc.toJSON() to show the data.
export const getTypes = (yDoc: Y.Doc): void =>
  yDoc.share.forEach((type, key) => {
    if (type._start !== null) {
    } else if (type._map.size !== 0) {

1 Like