Comparing version 10.1.2 to 10.2.0
@@ -150,3 +150,3 @@ "use strict"; | ||
} | ||
this._migrate(options.migrations, options.projectVersion); | ||
this._migrate(options.migrations, options.projectVersion, options.beforeEachMigration); | ||
} | ||
@@ -407,3 +407,3 @@ } | ||
} | ||
_migrate(migrations, versionToMigrate) { | ||
_migrate(migrations, versionToMigrate, beforeEachMigration) { | ||
let previousMigratedVersion = this._get(MIGRATION_KEY, '0.0.0'); | ||
@@ -415,2 +415,10 @@ const newerVersions = Object.keys(migrations) | ||
try { | ||
if (beforeEachMigration) { | ||
beforeEachMigration(this, { | ||
fromVersion: previousMigratedVersion, | ||
toVersion: version, | ||
finalVersion: versionToMigrate, | ||
versions: newerVersions | ||
}); | ||
} | ||
const migration = migrations[version]; | ||
@@ -417,0 +425,0 @@ migration(this); |
@@ -100,2 +100,8 @@ /// <reference types="node" /> | ||
/** | ||
The given callback function will be called before each migration step. | ||
This can be useful for logging purposes, preparing migration data, etc. | ||
*/ | ||
beforeEachMigration?: BeforeEachMigrationCallback<T>; | ||
/** | ||
__You most likely don't need this. Please don't use it unless you really have to.__ | ||
@@ -213,2 +219,9 @@ | ||
export declare type Migrations<T extends Record<string, any>> = Record<string, (store: Conf<T>) => void>; | ||
export declare type BeforeEachMigrationContext = { | ||
fromVersion: string; | ||
toVersion: string; | ||
finalVersion: string; | ||
versions: string[]; | ||
}; | ||
export declare type BeforeEachMigrationCallback<T extends Record<string, any>> = (store: Conf<T>, context: BeforeEachMigrationContext) => void; | ||
export declare type Schema<T> = { | ||
@@ -215,0 +228,0 @@ [Property in keyof T]: ValueSchema; |
{ | ||
"name": "conf", | ||
"version": "10.1.2", | ||
"version": "10.2.0", | ||
"description": "Simple config handling for your app or module", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -134,2 +134,48 @@ # conf | ||
### 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 Conf = require('conf'); | ||
console.log = someLogger.log; | ||
const mainConfig = new Conf({ | ||
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 Conf({ | ||
beforeEachMigration: (store, context) => { | ||
console.log(`[second-config] migrate from ${context.fromVersion} β ${context.toVersion}`); | ||
}, | ||
migrations: { | ||
'1.0.1': store => { | ||
store.set('debugPhase', true); | ||
}, | ||
} | ||
}); | ||
``` | ||
#### configName | ||
@@ -136,0 +182,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49972
747
446