Akita: Persist only slices of a store

Created on 25 Aug 2018  路  7Comments  路  Source: datorama/akita

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior


When I want to persist a part of the state I only can include/exclude based on complete stores.

Expected behavior


It would be nice to select a nested slice of a store, e.g. 'auth.tokens'.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?


I want to persist only some parts of a store, e.g. the tokens in the auth store and reset dynamic properties like loading in an EntityStore.
(like it's possible in the ngxs plugin)

enhancement

Most helpful comment

@twittwer I have added this functionality. In the next release, you can add to the include array what you asked. For example:

persistState({ include: ['todos.ui']});
persistState({ include: ['auth.token']});

All 7 comments

Thanks. I will add this feature to our roadmap.

This might be something interesting to look at as a backdrop to that:
https://github.com/cyrilletuzi/angular-async-local-storage

@twittwer I have added this functionality. In the next release, you can add to the include array what you asked. For example:

persistState({ include: ['todos.ui']});
persistState({ include: ['auth.token']});

@NetanelBasal Really nice, that was amazingly fast 馃槻

:tada: This issue has been resolved in version 1.7.0 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

What happens when I want to do something like this:
I have an EntityStore of contacts with a UI state that sets the searchTerm. I want the contacts to be cached but I don't want the UI state of searchTerm to ever be cached. How do I do that?

export interface ContactState extends EntityState<Contact> {
  ui:
  {
    contactFilterTerm: string;
  }
}


const initialState = {
  ui:{
    contactFilterTerm: ''
  }
} as ContactState;

@Injectable({ providedIn: 'root' })
@StoreConfig({ name: 'contact', cache: { ttl: 36000 }, resettable: true })
export class ContactStore extends EntityStore<ContactState, Contact> {

  constructor() {
    super(initialState);
  }

}

Use the hooks to add/remove what you need - https://netbasal.gitbook.io/akita/enhancers/persist-state#custom-hooks.

Was this page helpful?
0 / 5 - 0 ratings