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

Enable createMigrate to handle async migrations #866 #1219

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ const migrations = {
device: undefined
}
},
1: (state) => {
// You can use also `async () => {}` function
// For example to move users to other storage
1: async ({ users, ...state }) => {
try {
await insertToOtherStorage(users)

return state
} catch (e) {
// Don't forget about exceptions
return state
}
},
2: (state) => {
// migration to keep only device state
return {
device: state.device
Expand Down
9 changes: 5 additions & 4 deletions src/createMigrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ export default function createMigrate(
if (process.env.NODE_ENV !== 'production' && debug)
console.log('redux-persist: migrationKeys', migrationKeys)
try {
let migratedState = migrationKeys.reduce((state, versionKey) => {
return migrationKeys.reduce((promiseState, versionKey) => {
if (process.env.NODE_ENV !== 'production' && debug)
console.log(
'redux-persist: running migration for versionKey',
versionKey
)
return migrations[versionKey](state)
}, state)
return Promise.resolve(migratedState)
return promiseState.then(state =>
Promise.resolve(migrations[versionKey](state))
)
}, Promise.resolve(state))
} catch (err) {
return Promise.reject(err)
}
Expand Down