electron-store icon indicating copy to clipboard operation
electron-store copied to clipboard

Add update method [Feature Request]

Open ramtinsoltani opened this issue 2 years ago • 2 comments

Currently the only available setter Store.set() either sets a single item or takes an object to set multiple values. Providing an object would overwrite the whole value, which is a normal/expected behavior. Though, this leaves out use cases where we want to update multiple fields at a path in one go.

Example:

const store = new Store();

store.store = {
  users: {
    p9dsbfo0b0sbd: {
      name: 'John Smith',
      email: '[email protected]',
      age: 52,
      occupation: {
        title: 'Web Developer',
        remote: false
      }
    }
  }
};

// Updating multiple values (we'll either lose 'email' and 'occupation.title' fields or a schema validation error is thrown)
store.set('users.p9dsbfo0b0sbd', {
  name: 'John M. Smith',
  age: 53,
  occupation: {
    remote: true
  }
});

// Must do
store.set('users.p9dsbfo0b0sbd.name', 'John M. Smith');
store.set('users.p9dsbfo0b0sbd.age', 53);
store.set('users.p9dsbfo0b0sbd.occupation.remote', true);

// Better be able to do (object should be merged deeply)
store.update('users.p9dsbfo0b0sbd', {
  name: 'John M. Smith',
  age: 53,
  occupation: {
    remote: true
  }
});

ramtinsoltani avatar Nov 06 '22 09:11 ramtinsoltani

Seems useful to me.

Is it expected to a user that it does deep merging though?

sindresorhus avatar Nov 08 '22 10:11 sindresorhus

@sindresorhus I think deep merging is more practical, but users might expect a shallow merge based on other database operators such as MongoDB. We can however provide both methods through either:

// Additional argument
update(key: string, object: any, deepMerge: boolean = false)

// Example
store.update('users.p9dsbfo0b0sbd', {
  name: 'John M. Smith',
  age: 53,
  occupation: {
    remote: true
  }
}, true);

or a different method altogether:

// Shallow merge
update(key: string, object: any)
// Deep merge
merge(key: string, object: any)

Though I personally prefer the first approach.

If you find this useful, once we agree on an approach, I can create a dependency-free PR for conf.

ramtinsoltani avatar Nov 09 '22 20:11 ramtinsoltani