react-native-storage icon indicating copy to clipboard operation
react-native-storage copied to clipboard

Can save() be used as an update?

Open Eyesonly88 opened this issue 9 years ago • 5 comments

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 ?

Eyesonly88 avatar Dec 29 '16 02:12 Eyesonly88

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.

sunnylqm avatar Dec 29 '16 02:12 sunnylqm

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?

Eyesonly88 avatar Dec 29 '16 03:12 Eyesonly88

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

sunnylqm avatar Dec 29 '16 03:12 sunnylqm

Awesome thanks 👍. Yep my 🔑 looks like this ThuDec292016

Eyesonly88 avatar Dec 29 '16 03:12 Eyesonly88

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:

bosz avatar Aug 06 '18 05:08 bosz