mesqueeb / vuex-easy-firestore

Easy coupling of firestore and a vuex module. 2-way sync with 0 boilerplate!
https://mesqueeb.github.io/vuex-easy-firestore
MIT License
234 stars 28 forks source link

use arrayUnion on non-array values #136

Open mesqueeb opened 5 years ago

mesqueeb commented 5 years ago

Currently using arrayUnion on a prop that's not an array will not work. I will change this behaviour so it will change the value into an array for you.


After about two years of open source, I finally got accepted for Github Sponsors!

💜 github.com/sponsors/mesqueeb 💜

A little about me:

If anyone was helped with vuex-easy-firestore, I'd greatly appreciate any support!

BTW, donations get's paid DOUBLE by GitHub! (they're alchemists... 🦾)

Going forward 👨🏼‍💻

zabaat commented 5 years ago

Would this be for the best? maybe it should throw an error to the console when you are in debug mode or something rather than turning it straight into an array?

I'm not adverse to the change you suggest really, but thought I'd throw that out there.

mesqueeb commented 5 years ago

I for one would love to use arrayUnion a lot but, i'm often in a situation where I don't know if a doc will be a new doc or an existing doc that has to be patched. That's why I implemented "set" in Vuex easy fire. But it's really annoying for me to always have to check if the doc and prop exists and is an array in order to be able to use arrayUnion. ><

But I do see your point as well. Haha

Sent with GitHawk

mesqueeb commented 5 years ago

@zabaat

How about this: dispatch('user/set', {prop: arrayUnion('hi')})

louisameline commented 4 years ago

That's actually how Firestore works. If the initial property value is anything but an array (including undefined), it overwrites the property value with an array that contains the value(s) provided. If it already was an array, the new values are added to it if they were not already present.

So yes, dispatch('user/set', { prop: arrayUnion('hi') }) should definitely create (or update existing) an array in all cases. Now, about an error being thrown:

if the prop is undefined [...], if the prop exists [...]

There is a wrong assumption here: you think you know the property before the update. Until this library implements transactions, you actually don't know what the property value will be when the write operation is carried out.

So we probably shouldn't let users rely on an Error they think they could catch when they mess up their types, because if this happens at Firestore, they will actually never get one (again, unless in a transaction). I'd rather log a big warning, but not promise an Error you're not going to be able to provide consistently.

This wrong assumption also seems to be made in the current way set in collection mode operates, as it checks if a doc exists locally to decide to use insert or patch. Two scenarios come to mind:

I might very well be wrong as I currently have a hard time following what really happens in the code, but I fear there could be wrong methods being called there, resulting in potential data loss. We need to make a list of all potential scenarios at some point to make sure we have everything covered