import { NgxsStoragePluginModule, StorageEngine, STORAGE_ENGINE } from '@ngxs/storage-plugin';
export class MyStorageEngine implements StorageEngine {
get length(): number {
// Your logic here
}
getItem(key: string): any {
// Your logic here
}
setItem(key: string, val: any): void {
// Your logic here
}
removeItem(key: string): void {
// Your logic here
}
clear(): void {
// Your logic here
}
key(val: number): string {
// Your logic here
}
}
@NgModule({
imports: [NgxsModule.forRoot([]), NgxsStoragePluginModule.forRoot()],
providers: [
{
provide: STORAGE_ENGINE,
useClass: MyStorageEngine
}
]
})
export class MyModule {}
You can migrate data from one version to another during the startup of the store. Below is a strategy to migrate my state from animals to newAnimals. 您可以在存储启动期间将数据从一个版本迁移到另一个版本。 以下是将我的状态从 animals 迁移到 newAnimals 的策略。
@NgModule({
imports: [
NgxsModule.forRoot([]),
NgxsStoragePluginModule.forRoot({
migrations: [
{
version: 1,
key: 'zoo',
versionKey: 'myVersion',
migrate: state => {
return {
newAnimals: state.animals,
version: 2 // Important to set this to the next version!
};
}
}
]
})
]
})
export class MyModule {}
在迁移策略中,我们定义:
version: 我们正在迁移的版本
versionKey: 版本标识符(默认为'version')
migrate: 接受状态并期望返回新状态的函数。
key: 要迁移的项目的键。如果未指定,它将使用整个存储状态。
Note: Its important to specify the strategies in the order of which they should progress. 注意:重要的是要按其执行顺序指定策略。