Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

[Feature Request] Add ignorePaths or excludePaths option #288

Open
benface opened this issue Jul 15, 2020 · 9 comments
Open

[Feature Request] Add ignorePaths or excludePaths option #288

benface opened this issue Jul 15, 2020 · 9 comments

Comments

@benface
Copy link

benface commented Jul 15, 2020

The paths option is nice, but sometimes I would like it to be the other way around and specify which paths should NOT persist.

@gangsthub
Copy link
Contributor

  paths: [/* ... */],

paths has this behaviour already: you choose what to include here. If some paths are not specified, they won't be stored. Do you think it should be better documented, @benface?

@benface
Copy link
Author

benface commented Jul 15, 2020

@gangsthub Sorry I guess I wasn’t clear. I know this already, but I would like a way to store all paths except a few that I specify. That way when I add new root-level keys to the store, they are automatically persisted, unless I add them to ignorePaths / excludePaths. Do you see what I mean?

@gangsthub
Copy link
Contributor

Oh, I see what you mean. But maybe it could be "easily" added in user-land?

import { modules } from '@/store'

const modulesToOmit = ['moduleSomethingSomething']
const excludeModules = module => !modulesToOmit.includes(module)
const paths = Object.keys(modules).filter(excludeModules)

// Then use `paths` constant in `path` option...

What do you think?

@benface
Copy link
Author

benface commented Jul 16, 2020

Yes, sure could. Thanks for sharing that code. Feel free to close this if you think it’s not worth adding as an official API.

@gangsthub
Copy link
Contributor

Let's leave that to @robinvdvleuten ;)

@robinvdvleuten
Copy link
Owner

The example @gangsthub provided only excludes paths at a top-level. I think that a excludePaths boolean option alongside the paths array would work. The excludePaths option can be false by default, so any given path in paths are persisted. But when you pass true to excludePaths, all paths that are not in the paths option are persisted. Feel free to open a PR for this.

@matiasjg
Copy link

Hi, I like the idea of having another attribute to exclude paths or maybe a ! symbol?

I was wondering if it is possible to use the setItem function? but I am not sure how to import the default storage object.

@paulmartimx
Copy link

Hi. In my case, I need to persist all keys, except two. As I couldn't find any documentation about it, I tried with the "reducer" function, like this:

plugins: [createPersistedState({

    reducer: (state) => {
      let reducer = Object.assign({}, state)
      delete reducer.firstKeyToRemove
      delete reducer.secondKeyToRemove

      return (reducer)
    }
  })]

Where firstKeyToRemove and secondKeyToRemove are the ones to be excluded. It worked as expected, but it's certainly funny, because I understand the "reduce" function should be the opposite: "include only this keys".

@mreall
Copy link

mreall commented Mar 9, 2021

Hi. In my case, I need to persist all keys, except two. As I couldn't find any documentation about it, I tried with the "reducer" function, like this:

A previous issue mentioned that there are some quirks with using the reducer in this way, especially if you use namespaces.

I agree that having a way to exclude specific paths would be helpful.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants