Configuring custom storage

By default localStorage is used. However you can define other storages in one of two ways:

  • Setting it globally by calling PersistStateManager.configureStorage({...}) - passed object should contain clear, getItem, removeItem, setItem methods and method to gather keys from storage () => Object.keys(sessionStorage)

PersistStateManager.configureStorage({
    clear: () => timer(2000).pipe(tap(_ => localStorage.clear())),
    getItem: (key: string) => timer(2000).pipe(map(_ => localStorage.getItem(key))),
    removeItem: (key: string) => timer(2000).pipe(tap(_ => localStorage.removeItem(key))),
    setItem: (key: string, value: any) => timer(2000).pipe(tap(_ => localStorage.setItem(key, value))),
    }, () => timer(2000).pipe(map(_ => Object.keys(localStorage))));
  • passing to params when calling save method for example:

this.store.select(['todos']).storage.save({ 
    storageConfig: { 
        storage: sessionStorage, getKeys: Object.keys(sessionStorage) 
        } 
    });

Last updated