Skip to content

Commit

Permalink
Update readme with migration snippet
Browse files Browse the repository at this point in the history
Provide sample snippet which lets app migrate data previously stored in
`AsyncStorage` to `redux-persist-filesystem-storage`

Original issues:
rt2zz/redux-persist#199
rt2zz/redux-persist#284

Converastions related to provided snippet:
rt2zz/redux-persist#679
robwalkerco#7
  • Loading branch information
maxkomarychev committed Jan 26, 2018
1 parent 55325f0 commit e21c18d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,44 @@ persistStore(
},
)
)
```
## migration from previous storage
**tested with redux-persist v4**
the snippet below lets you migrate redux data previously stored in
`AsyncStorage` to `redux-persist-filesystem-storage`.
**NOTE** This snippet lets you migrate _healthy_ data. It will not _restore_
data if it is already hit limits of `AsyncStorage`
```javascript
import { persistStore, getStoredState } from 'redux-persist'
import FilesystemStorage from 'redux-persist-filesystem-storage'
import { AsyncStorage } from 'react-native'
import _ from 'lodash'
import { createStore } from 'redux'

const store = createStore(...)

// create persistor for `redux-persist-filesystem-storage`
const fsPersistor = persistStore(
store,
{ storage: FilesystemStorage },
async (fsError, fsResult) => {
if (_.isEmpty(fsResult)) {
// if state from fs storage is empty try to read state from previous storage
try {
const asyncState = await getStoredState({ storage: AsyncStorage })
if (!_.isEmpty(asyncState)) {
// if data exists in `AsyncStorage` - rehydrate fs persistor with it
fsPersistor.rehydrate(asyncState, { serial: false })
}
} catch (getStateError) {
console.warn("getStoredState error", getStateError)
}
}
}
)
```

0 comments on commit e21c18d

Please sign in to comment.