Skip to main content
Version: 1.2.0-beta1

Type Alias: MigrationRule<TFrom, TTo, K>

MigrationRule<TFrom, TTo, K> = object

Defined in: src/Mnemonic/types.ts:504

A single migration step that transforms data from one schema version to another, or normalizes data at the same version.

Migration rules are composed into a MigrationPath by the SchemaRegistry to upgrade stored data across multiple versions in sequence (e.g. v1 -> v2 -> v3).

When fromVersion === toVersion, the rule is a write-time normalizer that runs on every write to that version. This is useful for data normalization (trimming strings, clamping values, injecting defaults).

Example

// Version upgrade migration
const userV1ToV2: MigrationRule = {
key: "user",
fromVersion: 1,
toVersion: 2,
migrate: (v1) => {
const old = v1 as { name: string };
return { firstName: old.name, lastName: "" };
},
};

// Write-time normalizer (same version)
const trimUserV2: MigrationRule = {
key: "user",
fromVersion: 2,
toVersion: 2,
migrate: (v) => {
const user = v as { firstName: string; lastName: string };
return { firstName: user.firstName.trim(), lastName: user.lastName.trim() };
},
};

See

Type Parameters

Type ParameterDefault type
TFromunknown
TTounknown
K extends stringstring

Properties

fromVersion

fromVersion: number

Defined in: src/Mnemonic/types.ts:515

The version the stored data is migrating from.

Version 0 is allowed, enabling migrations from unversioned data.


key

key: K

Defined in: src/Mnemonic/types.ts:508

The unprefixed storage key this rule applies to.


toVersion

toVersion: number

Defined in: src/Mnemonic/types.ts:523

The version the stored data is migrating to.

When equal to fromVersion, this rule is a write-time normalizer that runs on every write to that version.

Methods

migrate()

migrate(value): TTo

Defined in: src/Mnemonic/types.ts:535

Transformation function that converts data from fromVersion to toVersion.

Receives the decoded value at fromVersion and must return the value in the shape expected by toVersion.

Parameters

ParameterTypeDescription
valueTFromThe decoded value at fromVersion

Returns

TTo

The transformed value for toVersion