rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.94k stars 866 forks source link

Question: Is it possible to divide reducer persistence by id #594

Closed adeelshahid closed 6 years ago

adeelshahid commented 6 years ago

for example,

documents reducer with a Map of documents (each key represents a document id)

documents: {
    [document id]: {
        meta information about the document
    }
}

Is it possible to make the configuration so that each document id is stored in a separate key inside the localStorage

rt2zz commented 6 years ago

no this is not possible, unless you model each document as a separate reducer (but that has its own set of challenges). You might be able to toy with redux-persist's internals to get this functionality, but tbh I think this is a pretty specific use case and probably a custom solution.

One big question: during rehydration, how do you know which documents you want to restore?

adeelshahid commented 6 years ago

@rt2zz we can have a reserved reducer with a prefix _, like _persist.

e.g. _docs: [documents ids] _docs:doc_id (contains the meta)

later on rehydrate we merge all _docs:parts into docs

This can be used for extensibility in any decent size application, which will have a similar model.

rt2zz commented 6 years ago

Edit woops answered on wrong issue, moved answer here: https://github.com/rt2zz/redux-persist/issues/589#issuecomment-348333268

ethanroday commented 6 years ago

@rt2zz Curious about this as well. To expand on @adeelshahid's example, let's say I have an application where users can create and edit documents. Some slice of my state looks like this:

{
  docIds: ['a', 'b', 'c', ...],
  docs: {
    a: { title: 'Document A', text: 'Lorem ipsum ...' },
    b: { title: 'Document B', text: 'Lorem ipsum ...' },
    c: { title: 'Document C', text: 'Lorem ipsum ...' },
    ...
  }
}

Let's say documents can get fairly large, and that there can be a lot of them. I wouldn't want the entire document state to be stored under one key. Rather, I'd likely want something like this:

Key Value
doc_ids "['a', 'b', 'c', ...]"
doc_a "{ 'title': 'Document A', 'text': 'Lorem ipsum ...' }"
doc_b "{ 'title': 'Document B', 'text': 'Lorem ipsum ...' }"
doc_c "{ 'title': 'Document C', 'text': 'Lorem ipsum ...' }"

That way, editing one document doesn't result in the whole state getting stringified and persisted.

ethanroday commented 6 years ago

@adeelshahid Why the close? Were you able to find a suitable workaround?

adeelshahid commented 6 years ago

@ethanroday no I had a server based solution to remove the complexity from the reducer. Now anything in the app that relates to preferences, progress etc. are synced from the server side.