Mark nodes from position (from,to) of a previous selection

Hey there,

I’m using ProseMirror for read-only content (text, images…) that users can highlight by selecting different types of nodes (text, list, images …) and then they are saved in my database (table called Highlights) for later use

When a selection is ended (different nodes contained in the selection), I’m getting the from & to positions and save them in my database

            mouseup: (view, _) => {
              const fromPos = view.state.selection.ranges[0].$from.pos;
              const toPos = view.state.selection.ranges[0].$to.pos;
              const hasTextSelection = fromPos !== toPos;

              //check is a selection is made
              if (hasTextSelection) {
                   // ...
                   // save fromPos & toPos in my database
                   // ...

Later, I’m fetching all highlights in the database and I want to inject them back into the editor content (highlighted parts with a yellow background), what’s the best way to reliably do that?