Skip to content

Fix a bug with limitToLast and cursors #6168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ export function queryToTarget(query: Query): Target {

// We need to swap the cursors to match the now-flipped query ordering.
const startAt = queryImpl.endAt
? new Bound(queryImpl.endAt.position, !queryImpl.endAt.inclusive)
? new Bound(queryImpl.endAt.position, queryImpl.endAt.inclusive)
: null;
const endAt = queryImpl.startAt
? new Bound(queryImpl.startAt.position, !queryImpl.startAt.inclusive)
? new Bound(queryImpl.startAt.position, queryImpl.startAt.inclusive)
: null;

// Now return as a LimitType.First query.
Expand Down
42 changes: 42 additions & 0 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,48 @@ apiDescribe('Queries', (persistence: boolean) => {
});
});

it('can issue limitToLast queries with cursors', () => {
const testDocs = {
a: { k: 'a', sort: 0 },
b: { k: 'b', sort: 1 },
c: { k: 'c', sort: 1 },
d: { k: 'd', sort: 2 }
};
return withTestCollection(persistence, testDocs, async collection => {
let docs = await getDocs(
query(collection, orderBy('sort'), endBefore(2), limitToLast(3))
);
expect(toDataArray(docs)).to.deep.equal([
{ k: 'a', sort: 0 },
{ k: 'b', sort: 1 },
{ k: 'c', sort: 1 }
]);

docs = await getDocs(
query(collection, orderBy('sort'), endAt(1), limitToLast(3))
);
expect(toDataArray(docs)).to.deep.equal([
{ k: 'a', sort: 0 },
{ k: 'b', sort: 1 },
{ k: 'c', sort: 1 }
]);

docs = await getDocs(
query(collection, orderBy('sort'), startAt(2), limitToLast(3))
);
expect(toDataArray(docs)).to.deep.equal([{ k: 'd', sort: 2 }]);

docs = await getDocs(
query(collection, orderBy('sort'), startAfter(-1), limitToLast(3))
);
expect(toDataArray(docs)).to.deep.equal([
{ k: 'b', sort: 1 },
{ k: 'c', sort: 1 },
{ k: 'd', sort: 2 }
]);
});
});

it('key order is descending for descending inequality', () => {
const testDocs = {
a: {
Expand Down