How does Yjs work with backends?

Greetings, Yjs team. Apologies for this noob question. I’m currently using slate. I do persist data in Dynamodb as follows:

Post {

createdAt: TimeStamp

creatorId: ID

title: String

content: JSON String of slate state
}

User {
Id: ID
posts : [Post]
}

I’m not sure how to go from this backend to a Yjs compatible backend.

Do I need to remove the JSON representation of the content and replace it with Yjs data model? How does Yjs db storage model look like?

I learned from another post that Yjs stores only updates. Does that mean that I should create a separate table for the updates, create a relationship between Post and Updates and populate the Post.content field with the last snapshot?

I am particularly confused about how Yjs plays out with an existing backend, can anyone please shed some light on that?

Thank you

I propose that you start experimenting with Yjs updates. If you have an existing application, you can store the Yjs document alongside the “content” field:

Post {
  createdAt: TimeStamp
  creatorId: ID
  title: String
  content: JSON String of slate state
  ydoc: String // ideally you store data in a binary field
}

The ydoc field is simply a binary blob:

const documentState = Y.encodeStateAsUpdate(ydoc) // is a Uint8Array

If your database doesn’t support binary content, you should convert it to a base64-string first. See https://docs.yjs.dev/api/document-updates#example-base64-encoding

There is a lot of room for optimizations. SQL databases are not well suited for storing incremental document updates. So you could use a combination of Redis and SQL database to store incremental updates instead of the whole binary blob.

1 Like

Thank you, this clears things out. I have also looked at this https://github.com/yjs/yjs-demos/issues/8#issuecomment-651350603 which was helpful.