sindresorhus / electron-store

Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc
MIT License
4.57k stars 148 forks source link

store.reset('options.items') not working #242

Open pierretusseau opened 1 year ago

pierretusseau commented 1 year ago

Hello,

I've got defaults setup as such :

{
  "initialized": false,
  ...
  "options": {
    "admin": {
      "darkmode": true,
      ...
    },
    "items": {
      "preview": false,
      ...
    }
  }
}

I'm looking to reset only "options": {"items": { ... }} in my store but I couldn't find how to do it.

If I do a store.reset('options') all of my store options get correctly cleared, but also any other data inside of it (like admin).

I feel like reset(...keys) doesn't understand the dot notation, is there something I'm missing, or should I split my store into something like :

{
  ...,
  "adminOptions": {
    "darkmode": true,
    ...
  },
  "itemsOptions": {
    "preview": false,
    ...
  }
}
ProMahmudul commented 1 year ago

@pierretusseau Have you found solutions? I also want to know this.

kashamalasha commented 1 year ago

I found a workaround for this problem. At first, define the schema before creating a store:

const schema = {
  "options": {
    "type": "object",
    "properties": {
      "items": {
        "type": "object",
        "properties": {
          "preview": {
            "type": "boolean",
            "default": false
          }
        },
        "required": ["preview"]
      }
    },
    "required": ["items"]
  }
};

const store = new Store({ schema });

Then you can use this to resets all keys within the options.items to default values according to the json schema values. store.set(`options.items`, {});

Unfortunately, the method store.reset(key) also doesn't work for me properly.