Skip to content

Commit b4f64f4

Browse files
Port matchesAllDocuments()
1 parent 0433b82 commit b4f64f4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/firestore/src/core/query.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ export class Query {
199199
);
200200
}
201201

202+
/**
203+
* Returns true if this query does not specify any query constraints that
204+
* could remove results.
205+
*/
206+
matchesAllDocuments(): boolean {
207+
return (
208+
this.filters.length === 0 &&
209+
this.limit === null &&
210+
this.startAt == null &&
211+
this.endAt == null &&
212+
(this.explicitOrderBy.length === 0 ||
213+
(this.explicitOrderBy.length === 1 &&
214+
this.getFirstOrderByField()!.isKeyField()))
215+
);
216+
}
217+
202218
// TODO(b/29183165): This is used to get a unique string from a query to, for
203219
// example, use as a dictionary key, but the implementation is subject to
204220
// collisions. Make it collision-free.

packages/firestore/test/unit/core/query.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,4 +591,27 @@ describe('Query', () => {
591591
orderBy(DOCUMENT_KEY_NAME, 'asc')
592592
]);
593593
});
594+
595+
it('matchesAllDocuments() considers filters, orders and bounds', () => {
596+
const baseQuery = Query.atPath(ResourcePath.fromString('collection'));
597+
expect(baseQuery.matchesAllDocuments()).to.be.true;
598+
599+
let query = baseQuery.addOrderBy(orderBy('__name__'));
600+
expect(query.matchesAllDocuments()).to.be.true;
601+
602+
query = baseQuery.addOrderBy(orderBy('foo'));
603+
expect(query.matchesAllDocuments()).to.be.false;
604+
605+
query = baseQuery.addFilter(filter('foo', '==', 'bar'));
606+
expect(query.matchesAllDocuments()).to.be.false;
607+
608+
query = baseQuery.withLimit(1);
609+
expect(query.matchesAllDocuments()).to.be.false;
610+
611+
query = baseQuery.withStartAt(bound([], true));
612+
expect(query.matchesAllDocuments()).to.be.false;
613+
614+
query = baseQuery.withEndAt(bound([], true));
615+
expect(query.matchesAllDocuments()).to.be.false;
616+
});
594617
});

0 commit comments

Comments
 (0)