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

Add multi-get

jasonmerino opened this issue · comments

Sometimes getting two values from the simple store would be nice as a convenience method without having to call Promise.all([store.get(KEY_ONE), store.get(KEY_TWO)]).

I could do that, If you want...

That would be cool. I was thinking that being able to call .get() with an array of strings rather than just a string would be a nice clean way to get multiples. From there I think it would be appropriate to return an array of values where the index of the value in the array would match the index of the key in the array which was passed to .get(). Under the hood it would just call Promise.all() and get the store values for each key. Mostly this would just be a convenience thing, but might be a nice feature.

If you are up for it, and what I've written here makes sense, I'd love to check out a pull request.

It would be really simple, I'll just have to use the multiGet method of the AsyncStorage, and make a function to merge the key with the store value and return an object, because this method return the responses in this way:

[
  [key1, value1],
  [key2, value2]
]

And I think that the answer would look better like this:

[
  {key: key1, value: value1},
  {key: key2, value: value2}
]

That's cool. It's been a long while since I've looked at the AsyncStorage API. Didn't even remember there was a multiGet.

I agree about the multiGet response format not being the nicest. But I think I'm more a fan of the Promise.all response format where you just get an array and each index of the response is the value for the index of the requested key.

So this

store.get(['key1', 'key2'])

would return

[valueForKey1, valueForKey2]

It would look something like this 91c36a0

I commented on that commit with some more thoughts.

What about now ? f8f2f93
Should I uncouple the get and the multiGet functionality in helper functions ?

If you're still unsure about the implementation, I would go for something like this:

get(...keys) {
  return AsyncStorage.multiGet(...keys).then(values => {
    if (values.length === 1) {
      return values[0];
    }

    return values.map(value => JSON.parse(value[1]));
  }
}

I did think of that, but I don't know if the spread operator is available in react-native

@cbdavide I like what you have for f8f2f93. Can you add some tests for the multi-get functionality and create a pull request?

Also, along with the tests we need to update documentation to show that .get() accepts a string or array as the key, and then an explanation as to what the different results would be.

Ok, don't worry I'm going to do the test first

I did this for the test, what do you think ? ddc7fdb
I've made another changes because I updated jest-cli

I like it. Can you open a pull request with those changes so I can pull it down and check it out?

Resolved by #10