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

Change "update" to a deep merge

keeleycarrigan opened this issue · comments

First, really like this little module. It really simplifies interacting with AsyncStorage.

I did notice one thing though. When you do an update it doesn't do a deep merge. This comes into play as soon as any key off your main storage item is an object because Object.assign doesn't do a deep merge. For example if I want to really organize my saved data and have a key for all user info that's separate from any other data I need to repeated get the current storage object, do a merge, then reassign it or my user data will keep getting messed up.

So I have:

"mainStorageKey": {
    "other_data": "something"
    "user_info": {
        "email": "guy@whatever.com",
        "token": "345983nfk23jrh239r3",
        "lat": 0,
        "lng": 0
    }
}

Whenever I want to update the user's latitude and longitude I need to pull down the main storage key off AsyncStorage and then merge the new coordinates with the user info. I see it's going a shallow copy here, but maybe I'm looking at this the wrong way or is there another way to accomplish what I'm talking about? Seems like adding lodash could add this functionality really easily.

Hey @keeleycarrigan! Glad you are enjoying the module. You are definitely correct about the .update() function not doing a deep merge. That is something that I had in the back of my mind when I initially wrote this, but never needed a deep merge, so it's been off my radar.

I agree that changing the Object.assign to a deep merge would definitely more understandable from a consumer standpoint.

If you would like to put together a pull request changing the .update() function so that it calls _.merge() via the lodash.merge module and add some tests I would be happy to get that merged. Otherwise, I will put it on my list, but I can't guarantee any speedy delivery on it.

What do you say?

Awesome. Sure I'll give it a whirl. Thanks!

This has been resolved by #15