bug(firestore): fix multiple cursor query

* fixed, fieldPath:'__name__' should be added once for queries with multiple cursors

* fixed es-lint errors

* test(firestore): test for multiple query

Co-authored-by: Mohammed Maaz <maazproductions25@gmail.com>
Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>
This commit is contained in:
Russell Wheatley
2020-02-11 08:40:46 +00:00
committed by GitHub
parent af768e3eb5
commit 13e9c86c24
2 changed files with 34 additions and 4 deletions

View File

@@ -162,4 +162,25 @@ describe('firestore().collection().startAfter()', () => {
qs.docs.length.should.eql(1);
qs.docs[0].id.should.eql('doc3');
});
it('runs startAfter & endBefore in the same query', async () => {
const colRef = firebase.firestore().collection('v6/startAfter/snapshot');
const doc1 = colRef.doc('doc1');
const doc2 = colRef.doc('doc2');
const doc3 = colRef.doc('doc3');
await Promise.all([doc1.set({ age: 1 }), doc2.set({ age: 2 }), doc3.set({ age: 3 })]);
const first = await doc1.get();
const last = await doc3.get();
const inBetween = await colRef
.orderBy('age', 'asc')
.startAfter(first)
.endBefore(last)
.get();
inBetween.docs.length.should.eql(1);
inBetween.docs[0].id.should.eql('doc2');
});
});

View File

@@ -72,6 +72,11 @@ export default class FirestoreQuery {
for (let i = 0; i < currentOrders.length; i++) {
const order = currentOrders[i];
//skip if fieldPath is '__name__'
if (order.fieldPath === '__name__') {
continue;
}
const value = documentSnapshot.get(order.fieldPath);
if (value === undefined) {
@@ -87,10 +92,14 @@ export default class FirestoreQuery {
// Based on https://github.com/invertase/react-native-firebase/issues/2854#issuecomment-552986650
if (modifiers._orders.length) {
modifiers._orders.push({
fieldPath: '__name__',
direction: modifiers._orders[modifiers._orders.length - 1].direction,
});
const lastOrder = modifiers._orders[modifiers._orders.length - 1];
//push '__name__' field only if not present already
if (lastOrder.fieldPath !== '__name__') {
modifiers._orders.push({
fieldPath: '__name__',
direction: lastOrder.direction,
});
}
} else {
modifiers._orders.push({
fieldPath: '__name__',