Skip to content

Port matchesAllDocuments() #2166

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
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ export class Query {
);
}

/**
* Returns true if this query does not specify any query constraints that
* could remove results.
*/
matchesAllDocuments(): boolean {
return (
this.filters.length === 0 &&
this.limit === null &&
this.startAt == null &&
this.endAt == null &&
(this.explicitOrderBy.length === 0 ||
(this.explicitOrderBy.length === 1 &&
this.explicitOrderBy[0].field.isKeyField()))
);
}

// TODO(b/29183165): This is used to get a unique string from a query to, for
// example, use as a dictionary key, but the implementation is subject to
// collisions. Make it collision-free.
Expand Down
23 changes: 23 additions & 0 deletions packages/firestore/test/unit/core/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,27 @@ describe('Query', () => {
orderBy(DOCUMENT_KEY_NAME, 'asc')
]);
});

it('matchesAllDocuments() considers filters, orders and bounds', () => {
const baseQuery = Query.atPath(ResourcePath.fromString('collection'));
expect(baseQuery.matchesAllDocuments()).to.be.true;

let query = baseQuery.addOrderBy(orderBy('__name__'));
expect(query.matchesAllDocuments()).to.be.true;

query = baseQuery.addOrderBy(orderBy('foo'));
expect(query.matchesAllDocuments()).to.be.false;

query = baseQuery.addFilter(filter('foo', '==', 'bar'));
expect(query.matchesAllDocuments()).to.be.false;

query = baseQuery.withLimit(1);
expect(query.matchesAllDocuments()).to.be.false;

query = baseQuery.withStartAt(bound([], true));
expect(query.matchesAllDocuments()).to.be.false;

query = baseQuery.withEndAt(bound([], true));
expect(query.matchesAllDocuments()).to.be.false;
});
});