sveltefire icon indicating copy to clipboard operation
sveltefire copied to clipboard

Free up `id` in Document's and Collection data.

Open JediBurrell opened this issue 5 years ago • 6 comments

Documents & Collections are returning data with the id field set as the title of the document. This is superfluous as the id is accessible via ref.id and overwrites data fields 'id'.

JediBurrell avatar Mar 13 '20 18:03 JediBurrell

Documents should not add an id field to the data. Are you sure this is the case? Also, what do you mean by "title of the document"?

Collections do add id field to each document as a convenience to avoid having to access it on each snapshot. However, we could add an option to disable this feature.

codediodeio avatar Mar 13 '20 19:03 codediodeio

I was using title to differentiate the document id, sorry for the confusion.

My data structure has the SKU as the document ID, and the product ID as an id field as the former is unique (and the latter is not).

This currently makes it impossible for me to grab the product ID from a collection.

JediBurrell avatar Mar 13 '20 20:03 JediBurrell

Gotcha, that's what I thought. I'm thinking about adding an option like this to prevent name collisions:

<Collection idField={'foo'}>
<Collection idField={null}>

That way you can change the field name, or omit it entirely.

codediodeio avatar Mar 13 '20 20:03 codediodeio

I would really like to keep meta-level things like id and ref out of the values set in the document. Right now if I want to "dump out the doc" it seems I need to specifically filter out the ref key? I think firebase API itself has this right: the "document object" has fields like id, ref and data, and data is an object.

Done this way, id and ref "keys" don't need to be customizable; they never conflict with data.

tv42 avatar Aug 14 '20 17:08 tv42

That's a good point. I think the next version should remove default values for id and ref.

codediodeio avatar Aug 16 '20 14:08 codediodeio

I've always just done what Jeff did { id: doc.id, ...doc.data() }. I've always just wanted "my data" + the DocumentId. But you bring up an interesting point. Maybe having .data is a better pattern and I'm just being lazy. I wonder if its worth doing data: doc.data() or if .data() is just returning a cached value, we just return the snapshop in its entirety as QueryDocumentSnapshot<T>.

metruzanca avatar Sep 12 '23 15:09 metruzanca