redux-firestore icon indicating copy to clipboard operation
redux-firestore copied to clipboard

feat(query) enable support for DocumentSnapshot with start/endAt

Open goleary opened this issue 5 years ago • 9 comments

What is the feature? enable passing a DocumentSnapshot to a query https://firebase.google.com/docs/reference/js/firebase.firestore.Query#start-at

Do you have thoughts/examples on how the feature would be used (i.e. API)?

I'm trying to implement a table that allows me to page through the documents returned by a query. Because of duplication of properties of the documents I cannot reliable use these properties to paginate. I need to use startAt/endAt with an actual DocumentSnapshot passed to ensure I am not missing any documents. This behaviour is talked about in the firestore docs

goleary avatar Dec 15 '19 07:12 goleary

There is the snapshot cache that was added by @illuminist in v0.11.0 - that should be useful for doing this, just haven't written up an example yet

prescottprue avatar Dec 16 '19 14:12 prescottprue

Thanks for pointing that out @prescottprue. I tried using that code, but after passing getSnapshotFromObject the data for a particular object instead of returning a DocumentSnapshot, it returns a QuerySnapshot.

image

goleary avatar Dec 16 '19 16:12 goleary

@goleary Thanks for reaching out - I reached out to @illumnist over gitter for more detail.

How did you end up using it? Would you mind sharing the app logic?

prescottprue avatar Dec 16 '19 16:12 prescottprue

image

goleary avatar Dec 16 '19 16:12 goleary

looking into that WeakMap this is what I see: image

It looks like I can access the DocumentSnapshot, but I assumed it would be a littler easier/simpler than this. Everything in the WeakMap is a QuerySnapshot.

goleary avatar Dec 16 '19 16:12 goleary

so if I obtain the DocumentSnapshot as such: image I am able to pass it to startAfter, but something seems off. The WeakMap contains quite a bit of duplicated data (the same QuerySnapshot and 10 DocumentSnapshots for each of the documents within that query).

goleary avatar Dec 16 '19 16:12 goleary

Thank you @goleary

I have found my mistake. I set document data as a key for QuerySnapshot instead of intended DocumentSnapshot. I even forgot to add the responding unit test to test if snapshot is right for the inner data.

PR https://github.com/prescottprue/redux-firestore/pull/258

illuminist avatar Dec 19 '19 10:12 illuminist

@illuminist Thanks a bunch for looking into it and making the PR!

prescottprue avatar Dec 19 '19 17:12 prescottprue

@prescottprue I found a new problem where updating QuerySnapshot isn’t update along with WeakMap key. The new ordered array and a data object is being created inside of reducer where I couldn’t access snapshot, and passing snapshot with action is against redux rules. One possible solution is to reduce the whole state inside of action creator and pass that entire data and ordered object into action instead. The DocumentSnapshot for pagination is still fine though.

So what is your thought?

illuminist avatar Dec 31 '19 14:12 illuminist