jasonmerino / react-native-simple-store

A minimalistic wrapper around React Native's AsyncStorage.

Home Page:https://www.npmjs.com/package/react-native-simple-store

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How would I store an array?

kevando opened this issue · comments

var location ={someCoord}
store.get('locations').then(locations => {
      locations.push(location);
      store.update('locations', {
        locations
      })
    });

Seems to bug out because locations isnt an existing array. Do I initialize it at some point?

Looking into the code it seems as though there is a bad assumption that the value passed to the update() function will either be a string or an object. As it stands today update() can't handle arrays.

Also, in your example above it looks like you are trying to update an array value which is stored in the locations key of the store and replace it with an object?

Sorry about my ignorance. Basically I want to store a new location each time the background location monitor tracks a new location. And to avoid server calls, I'd like to store it locally.

I'm sorry, the bad assumption I was talking about was in my code, not yours. This line to be specific.

So since the simple store doesn't currently support calling update for keys that contain array values you could do something like this, assuming that you wanted to push the new location onto what was stored in the locations key.

var location = { someCoord };
store.get('locations').then(locations => {
  locations.push(location);
  store.save('locations', locations)
});

Thinking about this it might be better to provide a store.push() method for easier updates of array values rather than adding array support to the store.update() method.

Yup this would still be dope salad, as I was having some trouble figuring out how to update arrays of data! This solution worked though. Thanks for the very easy to use tool.

@kevando @DZuz14 Is this what you would expect from a push method? #29

If the value for the given key is null (not defined in the simple store yet) it will create an array with what you passed it as the value at index 0.

If the value for the given key is an array it will do a push onto that array and persist it to the store.

If the value for the given key is neither an array or null it will throw an error that says something to the tune of: "Existing value for key "THE KEY YOU PASSED" must be of type null or Array, received THE VALUE STORED AT THAT KEY."

Let me know what you think!

Let's say I wanted to create an array with key, "userBios", that stores user profile information via objects. For ease of problem solving, let's pretend that under that current key of 'userBios', no array currently exists. According to this criteria, I would then go about creating this like so via your module:

      store.save('userBios', [
      {
        name: 'Dan',
        age: 29,
        location: 'Boston, Massachusetts'
      }
    ])

Notice how instead of creating an object as the second parameter of the store.save method, I used array brackets instead.

Imagine that the save went through successfully, and we now have an array with one object in it stored under the key "userBios". Now let's say that I now need to add another object. I would expect that I'd be able to do something as simple as the following.:

      store.push('userBios', 
      {
        name: 'Max',
        age: 35,
        location: 'Seattle, Washington'
      }
    )

This would then push Max's new information into the next slot of the "userBios" array. The userBios array would then currently be an array, consisting of 2 objects in it. Is this what you are thinking about?

Sorry for the long answer, but I wanted to be thorough to ensure I understand what it is you are asking.

Yeah, that's pretty much the gist of it. If you use .save to store an array value for a given key you can then go back and push a new record onto that array with .push and it would just do an Array.push to the stored array.

Although now that I'm looking at the MDN docs it seems like I may also want to add the ability to push multiple items as separate arguments (after argument[0]).

What do you guys think?

This feature is available in version 1.3.0.

Epic. Very nice work. This is by far the easiest module to use out there.