Skip to content

Commit

Permalink
Add beforeEachMigration option
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 30, 2022
1 parent e78308b commit 26df5b3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"save"
],
"dependencies": {
"conf": "^10.1.2",
"type-fest": "^2.12.2"
"conf": "^10.2.0",
"type-fest": "^2.17.0"
},
"devDependencies": {
"ava": "^2.4.0",
Expand Down
46 changes: 46 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,52 @@ const store = new Store({
});
```

### beforeEachMigration

Type: `Function`\
Default: `undefined`

The given callback function will be called before each migration step.

The function receives the store as the first argument and a context object as the second argument with the following properties:

- `fromVersion` - The version the migration step is being migrated from.
- `toVersion` - The version the migration step is being migrated to.
- `finalVersion` - The final version after all the migrations are applied.
- `versions` - All the versions with a migration step.

This can be useful for logging purposes, preparing migration data, etc.

Example:

```js
const Store = require('electron-store');

console.log = someLogger.log;

const mainConfig = new Store({
beforeEachMigration: (store, context) => {
console.log(`[main-config] migrate from ${context.fromVersion}${context.toVersion}`);
},
migrations: {
'0.4.0': store => {
store.set('debugPhase', true);
}
}
});

const secondConfig = new Store({
beforeEachMigration: (store, context) => {
console.log(`[second-config] migrate from ${context.fromVersion}${context.toVersion}`);
},
migrations: {
'1.0.1': store => {
store.set('debugPhase', true);
}
}
});
```

#### name

Type: `string`\
Expand Down

0 comments on commit 26df5b3

Please sign in to comment.