@Persistence

import {provideStore} from '@ngxs/store';
import {provideNgxsDataPlugin} from '@angular-ru/ngxs';
import {withNgxsDataStorage} from '@angular-ru/ngxs/storage';

export const appConfig: ApplicationConfig = {
  providers: [provideStore([TodoState]), provideNgxsDataPlugin(withNgxsDataStorage())],
};
@Persistence()
@StateRepository()
@State<string[]>({
  name: 'todo',
  defaults: [],
})
@Injectable()
export class TodoState extends NgxsDataRepository<string[]> {
  // ..
}

@Persistence() - If you add a current decorator without options, then the todo state will synchronize with LocalStorage by default.

API

@Persistence(options?: PersistenceProvider[] | PersistenceProvider)


PersistenceProvider

  • existingEngine (required|optional, DataStorage|Storage) - Specify an object that conforms to the Storage interface to use, this will default to localStorage.

  • useClass (required|optional, Type<T>) - If no existingEngine is specified, you can provide by class token your storage container.

  • path (optional, string) - Path for slice data from NGXS store, this will default path to current state in store.

  • version (optional, number) - You can migrate data from one version to another during the startup of the store, this will default first version.

  • ttl (optional, number) - You can determine the lifetime of a given key (default: -1, disable).

  • ttlDelay (optional, number) - The time, in milliseconds (thousandths of a second), the timer should delay in between checking for expiration time live (default: 60000ms / 1min).

  • ttlExpiredStrategy (optional, TTL_EXPIRED_STRATEGY) - You can determine what to do with the key if it expires (default: TTL_EXPIRED_STRATEGY.REMOVE_KEY_AFTER_EXPIRED).

  • fireInit (optional, boolean) - Disable initial synchronized with the storage after occurred rehydrate from storage (by always default will be synchronized).

  • nullable (optional, boolean) - If the state is undefined or null in the storage by key, then it will overwrite the default state when initial prepared.

  • rehydrate (optional, boolean) - Pull initial state from storage on a startup (true by default).

  • migrate (optional, defaults: T, storage: R) => T) - Function that accepts a state and expects the new state in return.

  • skipMigrate (optional, boolean) - Skip key migration (default: false).

  • decode (optional, STORAGE_DECODE_TYPE) - You can also decode or encode your data in base64 (default: none).

Fire init

If you don't want your value that was received from the storage to be synchronized again with the storage, you can disable this step. You will see that lastChanged is not updated again and again when the page reloads.

Time to live (TTL)

Migration strategy

Multiples providers

However, a situation may arise when you need to migrate different data sources. Suppose you had different models for a nested state:

And the new model now looks like this:

In this case, you can define a handler for each:

Also, if you want skipping migration for another provider, you can set skipMigrate to true.

Storage events

The storage event of the Window interface fires when a storage area (localStorage or sessionStorage) has been modified in the context of another document.

When an event occurs, you will receive a new state, also, if you implemented a ngxsDataAfterStorageEvent method, it will be called.

Decode/encode

Override global prefix key

By default, key search uses the prefix @ngxs.store., but you can override the prefix:

Use base64 for decode/encode data in storage by default everything

Nested states

In more complex cases, when you need to use other storage, or you want to save part of the state, you can use the complex options:

Global custom storage

Injectable Storage

Last updated