Skip to content

Add option for updating objects #3

@paceaux

Description

@paceaux

Problem:

get/set is fine for small data types like numbers, text, booleans.

But when storing arrays or objects, it's a lot of steps:

  1. get the array
  2. modify the array
  3. set the array again

Solution

It would be nice if there were some sort of option for modifying objects that could be a single step.

There should probably be a put and/or patch option where:

  1. If the item doesn't exist, it's created with .set()
  2. If the item does exist, and it's an object, it's treated as an object where the new data is merged in via something like Object.assign()
  3. Instructions for this warn that if it's an array, then ordering may be borked?
  4. It then returns the updated value?
  5. it also ofc triggers the notification system and has the type "put"

Implementations?

But, there should maybe be two kinds of in-place editing

  • put: puts all new data on to the object
  • patch: does a smart merge (compares to source and removes what isn't there, adds what is)

Chainable methods?

Maybe the solution could be that it's chainable?

storage.get('someObject').put('newObject')

This could mean that .get has to be modified to put a method on the object prototype only when it's an object. That means modifying the prototype, which could be sketch.

Method(s) with callbacks

Maybe a method with a callback is the right answer:

storage.patch('someObject', (objectData) => {
  return whateverTheUpdateIs
})

This doesn't require messing with prototype, and is cleaner visually. It also wouldn't matter, then, if it's an array or object.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions