ProtectedonHandles errors thrown by a registered migration during run(). This is not used if upgrade() is called directly.
This implementation simply rethrows the error so that it propagates out of run(). If overridden to return a value, that will be used as the result of run().
You could use this to e.g. to provide a nice message to the user explaining that their data couldn't be recovered, and use the default value.
the object taken from persistence, before any migrations ran
the default value passed to run()
Registers a function to update an object that is currently at sourceVersion. The function must return a new object at a higher version number. Most commonly each migration will upgrade the object by only 1 version. The output of the older migrations will be passed in turn to newer migrations until the target version is reached.
Use undefined as the sourceVersion to handle migrations from a legacy format that did not have the _version key.
migrateFunction will be called with the migration manager itself as this. That allows subclasses to pass in methods as a migration function without any special binding. E.g.:
class MigrationService extends MigrationManager<MyState> {
constructor(private messaging: MessagingService) {
super();
this.registerMigration(1, this.#migrateFrom1); // no special binding
}
#migrateFrom1(source: MyState): MyState {
this.messaging.show("You've been upgraded!"); // you can still use `this`
return { ...source, _version: 2 };
}
}
Returns the value from persistence, upgraded to match the version in defaultValue. If persistence was empty, returns defaultValue directly. Updates peristence to reflect the returned value.
If an error is thrown by a migration, see onError() for details.
Runs any registered migrations necessary to convert source to targetVersion. If it is already at targetVersion, no migrations run and it is returned unmodified.
If you are using Persistence you will probably not call this directly; use .run() instead.
Use to migrate "versioned objects" from an old version to the latest. This is useful e.g. when keeping state in
Persistence, and you release a new version of your app that changes its format.For example, say your app persists an object with the shape
{ _version: 3, key_1: string }. You change your code to remove the underscore fromkey_1, so you want to migrate data on users' machines accordingly. Bump your version to 4 and useMigrationManagerlike this: