samuelgozi/firebase-firestore-lite

Functions for accessing meta information of document

vivekweb2013 opened this issue · 5 comments

[Feature Request]

The documents retrieved with query has the meta information accessible with __meta__ key.
Raising this issue if its feasible to add the getter functions for retrieving meta information.

Feel free to close if you think this is unnecessary.

Thanks

What do you mean? Like document.meta shortcut for the __meta__ prop?
Or something else?

Currently, the Document class is the only thing I'm not sure about. I wanted documents to feel like regular objects, but still to retain some context. The issue is that, if they are treated like regular objects then devs will expect that no additional properties will be present, and this is an issue because if I put the metadata in a property called meta, then if the firebase document has a field called meta too, then it will overwrite it or won't be accessible.

To solve this problem there are two options:
1 - Put all the metadata under a hidden property.
2 - Put all of the document fields inside a property.

In the first solution, all of the metadata resides in the __meta__(or any other name) property, and It will never be overwritten by an existing document field because it is an invalid field name in Firestore documents(all __.*__ names are invalid). In addition, I made __meta__ innumerable, so it doesn't show up in loops, or Object.keys. The downside is that it is a little bit ugly.

The second option is what the Official SDK did: the returned document is a class instance with some methods, and if you want to access the document fields you need to call document.data().
This also solves the problem, as now the developer knows that his data resides inside document.data(), and all the rest are metadata fields.

The reason I choose the first solution is that I believe developers access metadata less frequently than the document's fields. So I felt like it will feel "cleaner". However, I knew that I might be wrong, and accessing the metadata by using __meta__ might not really be a better solution.

Do you think that I should do it in a different way? if so, can you please show a code example of how you would like to access that data? You don't have to write the implementation, just how you would like it to work.

What do you mean? Like document.meta shortcut for the meta prop?

No. I wanted functions for accessing the __meta__ properties. Something like
Object.defineProperty(this, 'createTime', { value: function(){return __meta__.createTime} });

I wanted this because I thought accessing the meta properties using __meta__ looks ugly.

BTW not a big fan of accessing data using document.data(), your current implementation looks fine and a cleaner approach allowing to access the data using properties directly on the document. So please don't change this part.

Thanks

Ok, thanks for the clarification.
I have an idea, please tell me if you think it sounds good.

Have a meta getter on the document's prototype that points to __meta__.
However, if the firebase document also has a property with that name, then it will be overwritten by it, and then you will have to use the __meta__ prop.

Ok, thanks for the clarification.
I have an idea, please tell me if you think it sounds good.

Have a meta getter on the document's prototype that points to __meta__.
However, if the firebase document also has a property with that name, then it will be overwritten by it, and then you will have to use the __meta__ prop.

Oh I got it now, sorry I didn't get the override thing correctly earlier.
Yes you are right, even in case we define the getter functions there is a chance of overriding with firebase data having the same key.

Seems like storing meta info into some hidden property is the only option left if we are to access the primary data directly using properties.

I think I'm okay with accessing meta info using __meta__, so keep it as it is, no need to change anything.

Thanks

No problem. I will try to work on an elegant solution, but for now, I'm out of ideas.
Feel free to comment/reopen an issue if you think of something.