mesqueeb/vuex-easy-firestore

Restrict set method to only do one job.

nawazishali opened this issue ยท 8 comments

Issue:
I am trying to set a document in a sub collection with named ID, Which throws an error cause firestore can't patch the documents that don't exist yet.

Here is an example path.
restaurants/{restaurantId}/meta/rawResponse

even paths like these don't work when the object doesn't exist and I try to set one with an ID.
users/{userId}

Reason:
dispatch('myModule/set') is trying to patch a document when there is an ID present in the data or in this case the path itself contains the id.

Suggested Fix:
Restrict set method to only create new documents and not decide on it's own. So when there is no ID present it should allow firestore to create an ID for it otherwise use the given ID.

I can also take this myself as an open source contribution if you can point me in the right direction. Thanks a ton for building such a useful and easy to use library.

Hi @nawazishali thanks for your issue.

In fact, set is just a shortcut that will either dispatch insert or patch depending on if the document is already in your vuex state or not.

You can look at the source code of "set" here.

If you want to an action only to either insert or patch, you can just use the insert and patch actions instead. ๐Ÿ˜‰

Does this help? Feel free to continue the discussion down below.

BTW, currently you can only manually "insert" a new document into Firebase via my library, when the vuex-easy-firestore module is a "collection".

In case of a "document" type vuex-easy-firestore, my library will automatically insert it into firebase if it isn't found upon opening the DB channel.

I'm not 100% sure what you are trying to do.

@mesqueeb Thanks for the reply.

Here are some more details of the issue I am facing.

  1. I am only talking about doc mode here so insert is not an option.
  2. Automatic doc insertion doesn't work here cause I am not using openDbChannel for these docs. I just need to make a simple set call but since the library is assuming that if the ID is present then the doc must exist already which is not the case.

In your action.ts file on line 822-829 you are telling it to insert only when an ID is not preset. And this is what's causing the problem for me. I am not sure what was your thought process for that but in my opinion it should always just insert a new doc cause patch is there already for the purpose of updating it.

Let me know if you need more details on this or if I am getting this wrong. I really need this fix to get my work done smoothly. The work around of using collection mode is only going to add extra files and code in the project that doesn't sound right.

If you want to only patch the doc, you can just use the patch action.
If you want my library to automatically insert a doc in firebase if it doesn't exist yet, this is currently only supported with the openDBChannel action. As a work around, you could either open and close the DB channel to rely on my library's insertion if required, or you could write your own check function with the firebase SDK that inserts a doc when non-existent.

I'm currently not sure how to improve the set action specifically, and I'm open to PRs.

I'm working hard on v2.0 which is a complete rewrite and where this is done different entirely anyway. ๐Ÿ˜…

@mesqueeb Yeah I read your RFC for v2 and it sounds like a really good plan. ๐Ÿ‘ :)

OK I will try not to bother you anymore. I am currently trying all the workarounds I can think of right now. But If nothing works I am gonna make a PR for this fix.

Last question though, Can we make an insert call in doc mode as well?

No, is the current implementation was only anticipating people to use the openDBChannel action first, so that makes sure the doc exists in firebase. If not, it inserts it. Then you can use just the patch action, and I believe that solves your problems.

I know the design is not the best.... I was still young when I made it. :D muuuch better solutions for V2.0

Cool, Using openDBChannel does solve the issue for me although it still adds unnecessary code but it does the job. Thanks for help. :)

And for a young coder this library still rocks. I am looking forward to the next version already. Will definitely buy you some coffee for the work you are doing.

I think that writing a function with Firebase SDK that does a transaction which inserts the doc if it's not existent should not be too hard and the cleanest option. Good luck! And I'll keep you up to date for v2.0 ๐Ÿ˜‰