sunnylqm / react-native-storage

local storage wrapper for both react-native and browser. Support size controlling, auto expiring, remote data auto syncing and getting batch data in one query.
MIT License
3.02k stars 268 forks source link

Can save() be used as an update? #63

Open Eyesonly88 opened 7 years ago

Eyesonly88 commented 7 years ago

First of all, thank you very much for your amazing library 👍

I've got a question regarding using the save() operation. In my use case, I'm saving by key and the key is the date. I want to update the rawData without overriding the existing data.

For example:

// first save operation
...
rawData: [ { car: 'tesla' } ]
...

// second save operation
...
rawData: [ { car: 'mazda' } ]
...

// Now doing a load on key, I want to get
[ { car: 'tesla' }, { car: 'mazda' } ]

// but I actually get
[ { car: 'mazda' } ]

Is that possible to do currently? or do you recommend a way of achieving this?

One way I thought I can do it is by doing a load of the data first, then appending it manually, then storing it back.

Also as a side note, my keys have numbers in them and it seems to work (you mentioned underscores are not permitted), so I'm guessing using numbers in keys is supported ?

sunnylqm commented 7 years ago

Currently "update/merge" operation is not yet supported, but it's easy to implement by yourself. You can do a load and then use Object.assign to merge data and then save, or maybe you can split a list into key-id data. I'll consider to add this update() feature when I have spare time. And of course PR is highly welcomed.

Eyesonly88 commented 7 years ago

Thanks for the quick response. Yep, I just implemented update like you said 👍 . I used the spread operator.

Just to confirm, is having a number in the key supported?

sunnylqm commented 7 years ago

You can have number in the key, just not use pure number cause it's really confusing and error-prune.

Eyesonly88 commented 7 years ago

Awesome thanks 👍. Yep my 🔑 looks like this ThuDec292016

bosz commented 6 years ago

Learnt much from the chat. Thanks @Eyesonly88 and @sunnylqm . I think this issue can be closed, maybe a label "recommendation" added to it right so that it gives way for other issues :smile: