Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usage with redux-persist: persist only some models #653

Open
Leooo opened this issue Jan 18, 2021 · 1 comment
Open

usage with redux-persist: persist only some models #653

Leooo opened this issue Jan 18, 2021 · 1 comment

Comments

@Leooo
Copy link

Leooo commented Jan 18, 2021

Hello, this addon works great with redux-persist, only I can't manage to persist only a few models - more exactly, I kind of have done it, but I can't manage to populate again seed data on models I haven't persisted when reloading the page.

There are similar issues opened in redux-persist, but it feels like there should be one specific to redux-orm too.

inspired by rt2zz/redux-persist#134 (comment), this works:

//src/configureStore.js

//..
import { persistStore, persistReducer, createTransform } from 'redux-persist'
import omit from 'lodash/omit'
//..

import rootReducer from './redux/reducers';

let blacklistTransform = createTransform(
  (inboundState, key) => {
    if (key === 'session') {
      //from https://github.com/rt2zz/redux-persist/issues/134#issuecomment-427802073
      return omit(inboundState, ['Hint']);
    } else {
      return inboundState;
    }
  },
  (outboundState, key) => {
    if (key === 'session') {
       // maybe could use orm.getDefaultState() instead. Result should be the same
      outboundState.Hint = {
        indexes: {},
        items: [],
        itemsById: {},
        meta: {}
      };
      return outboundState;
    } else {
      return outboundState;
    }
  }
)
const persistConfig = {
    key: 'root',
    storage,
    transforms: [blacklistTransform]
}

const persistedReducer = persistReducer(persistConfig, rootReducer);

Now that's this bit (orm reducer) that doesn't work for me: inspired by #228 (comment), I tried to add another case where the state is not empty but the model creation (Person.seedDatabase()) is not persisted unfortunately. If I remove hasInitializedDB, now it's been created three times. Surely there must be a better way?

import orm from '../models/orm';
import { defaultUpdater } from 'redux-orm/lib/redux';

let hasInitializedDB = false;
//https://github.com/redux-orm/redux-orm/issues/228#issuecomment-419891508
//this was supposed to handle the case of redux-persist returning empty model lists (Hint, for example)
const initializeState = (orm, state) => {
    const noInitialState = !state;
    state = state || orm.getEmptyState();
    const { Person, Hint } = orm.mutableSession(state);
    if (noInitialState) {
      Person.seedDatabase();
    }
    if (!hasInitializedDB) {
      //hasInitializedDB is needed because, despite the `mutableSession` bit,
      //the models creation below don't directly create models in the store - and this is run at least three times before it does
      //yes, this is a dirty hack TODO
      hasInitializedDB = true;
      if (!state.Hint.items.length) Hint.seedDatabase();
    }
    return state;
};

const createReducer = (orm, updater = defaultUpdater) => {
    return (state, action) => {
        // changed from: orm.session(state || orm.getEmptyState());
       // to orm.session(state || initializeState(orm)); and then the below
        const session = orm.session(initializeState(orm, state));
        updater(session, action);
        return session.state;
    };
};
export default createReducer(orm);

Many thanks to anyone who has a working solution for redux-orm persistence to share (I also looked at https://github.com/rt2zz/redux-persist#nested-persists without success)

@Leooo Leooo closed this as completed Jan 18, 2021
@Leooo Leooo reopened this Jan 18, 2021
@PiotrKujawa
Copy link

@Leooo there you go, here is what you can do using my package redux-deep-persist
which can be helpful in creating a config for redux-persist.

import { getPersistConfig } from 'redux-deep-persist';

const config = getPersistConfig({
    key: 'root',
    storage: LocalStorage, // whatever storage you use
    blacklist: [
        'a1.b1.c1',  
        'a2.b2.c2',  
        'a3',
        'a4.b4.c4.no.matter.how.deep.you.go'
    ],
    rootReducer, // your root reducer must be also passed here
    ... // any other props from original redux-persist config, omitting the stateReconciler
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants