Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update method [Feature Request] #240

Open
ramtinsoltani opened this issue Nov 6, 2022 · 2 comments
Open

Add update method [Feature Request] #240

ramtinsoltani opened this issue Nov 6, 2022 · 2 comments

Comments

@ramtinsoltani
Copy link

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
  }
});
@sindresorhus
Copy link
Owner

Seems useful to me.

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

@ramtinsoltani
Copy link
Author

@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.

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

No branches or pull requests

2 participants