Skip to content

Commit a7bb0b5

Browse files
Michael LehenbauerFeiyang1
authored andcommitted
Expose Collection Group queries API. (#1722)
1 parent f7a46de commit a7bb0b5

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

packages/firebase/index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6176,6 +6176,18 @@ declare namespace firebase.firestore {
61766176
*/
61776177
doc(documentPath: string): DocumentReference;
61786178

6179+
/**
6180+
* Creates and returns a new Query that includes all documents in the
6181+
* database that are contained in a collection or subcollection with the
6182+
* given collectionId.
6183+
*
6184+
* @param collectionId Identifies the collections to query over. Every
6185+
* collection or subcollection with this ID as the last segment of its path
6186+
* will be included. Cannot contain a slash.
6187+
* @return The created Query.
6188+
*/
6189+
collectionGroup(collectionId: string): Query;
6190+
61796191
/**
61806192
* Executes the given `updateFunction` and then attempts to commit the changes
61816193
* applied within the transaction. If any document read within the transaction

packages/firestore-types/index.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ export class FirebaseFirestore {
173173
*/
174174
doc(documentPath: string): DocumentReference;
175175

176-
// TODO(b/116617988): Uncomment method and change jsdoc comment to "/**"
177-
// once backend support is ready.
178-
/*
176+
/**
179177
* Creates and returns a new Query that includes all documents in the
180178
* database that are contained in a collection or subcollection with the
181179
* given collectionId.
@@ -185,7 +183,7 @@ export class FirebaseFirestore {
185183
* will be included. Cannot contain a slash.
186184
* @return The created Query.
187185
*/
188-
//collectionGroup(collectionId: string): Query;
186+
collectionGroup(collectionId: string): Query;
189187

190188
/**
191189
* Executes the given updateFunction and then attempts to commit the

packages/firestore/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Unreleased
1+
# Unreleased (I/O)
2+
- [feature] You can now query across all collections in your database with a
3+
given collection ID using the `FirebaseFirestore.collectionGroup()` method.
4+
5+
# 1.1.4
26
- [feature] Added an `experimentalForceLongPolling` setting that that can be
37
used to work around proxies that prevent the Firestore client from connecting
48
to the Firestore backend.

packages/firestore/src/api/database.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
538538
return DocumentReference.forPath(ResourcePath.fromString(pathString), this);
539539
}
540540

541-
// TODO(b/116617988): Fix name, uncomment d.ts definitions, and update CHANGELOG.md.
542-
_collectionGroup(collectionId: string): firestore.Query {
541+
collectionGroup(collectionId: string): firestore.Query {
543542
validateExactNumberOfArgs('Firestore.collectionGroup', arguments, 1);
544543
validateArgType(
545544
'Firestore.collectionGroup',

packages/firestore/test/integration/api/query.test.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ const FieldPath = firebase.firestore!.FieldPath;
3737
const GeoPoint = firebase.firestore!.GeoPoint;
3838
const Timestamp = firebase.firestore!.Timestamp;
3939

40-
// TODO(b/116617988): Use public API.
41-
interface FirestoreInternal extends firestore.FirebaseFirestore {
42-
_collectionGroup(collectionId: string): firestore.Query;
43-
}
44-
4540
apiDescribe('Queries', persistence => {
4641
addEqualityMatcher();
4742

@@ -600,9 +595,7 @@ apiDescribe('Queries', persistence => {
600595
}
601596
await batch.commit();
602597

603-
const querySnapshot = await (db as FirestoreInternal)
604-
._collectionGroup(collectionGroup)
605-
.get();
598+
const querySnapshot = await db.collectionGroup(collectionGroup).get();
606599
expect(querySnapshot.docs.map(d => d.id)).to.deep.equal([
607600
'cg-doc1',
608601
'cg-doc2',
@@ -634,8 +627,8 @@ apiDescribe('Queries', persistence => {
634627
}
635628
await batch.commit();
636629

637-
let querySnapshot = await (db as FirestoreInternal)
638-
._collectionGroup(collectionGroup)
630+
let querySnapshot = await db
631+
.collectionGroup(collectionGroup)
639632
.orderBy(FieldPath.documentId())
640633
.startAt(`a/b`)
641634
.endAt('a/b0')
@@ -646,8 +639,8 @@ apiDescribe('Queries', persistence => {
646639
'cg-doc4'
647640
]);
648641

649-
querySnapshot = await (db as FirestoreInternal)
650-
._collectionGroup(collectionGroup)
642+
querySnapshot = await db
643+
.collectionGroup(collectionGroup)
651644
.orderBy(FieldPath.documentId())
652645
.startAfter('a/b')
653646
.endBefore(`a/b/${collectionGroup}/cg-doc3`)
@@ -677,8 +670,8 @@ apiDescribe('Queries', persistence => {
677670
}
678671
await batch.commit();
679672

680-
let querySnapshot = await (db as FirestoreInternal)
681-
._collectionGroup(collectionGroup)
673+
let querySnapshot = await db
674+
.collectionGroup(collectionGroup)
682675
.where(FieldPath.documentId(), '>=', `a/b`)
683676
.where(FieldPath.documentId(), '<=', 'a/b0')
684677
.get();
@@ -688,8 +681,8 @@ apiDescribe('Queries', persistence => {
688681
'cg-doc4'
689682
]);
690683

691-
querySnapshot = await (db as FirestoreInternal)
692-
._collectionGroup(collectionGroup)
684+
querySnapshot = await db
685+
.collectionGroup(collectionGroup)
693686
.where(FieldPath.documentId(), '>', `a/b`)
694687
.where(FieldPath.documentId(), '<', `a/b/${collectionGroup}/cg-doc3`)
695688
.get();

packages/firestore/test/integration/api/validation.test.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ import {
3333
const FieldPath = firebase.firestore!.FieldPath;
3434
const FieldValue = firebase.firestore!.FieldValue;
3535

36-
// TODO(b/116617988): Use public API.
37-
interface FirestoreInternal extends firestore.FirebaseFirestore {
38-
_collectionGroup(collectionId: string): firestore.Query;
39-
}
40-
4136
// We're using 'as any' to pass invalid values to APIs for testing purposes.
4237
// tslint:disable:no-any
4338

@@ -930,8 +925,8 @@ apiDescribe('Validation:', persistence => {
930925
const query = db
931926
.collection('collection')
932927
.orderBy(firebase.firestore!.FieldPath.documentId());
933-
const cgQuery = (db as FirestoreInternal)
934-
._collectionGroup('collection')
928+
const cgQuery = db
929+
.collectionGroup('collection')
935930
.orderBy(firebase.firestore!.FieldPath.documentId());
936931
expect(() => query.startAt(1)).to.throw(
937932
'Invalid query. Expected a string for document ID in ' +
@@ -1054,9 +1049,7 @@ apiDescribe('Validation:', persistence => {
10541049
'FieldPath.documentId(), but it was: 1.'
10551050
);
10561051
expect(() =>
1057-
(db as FirestoreInternal)
1058-
._collectionGroup('foo')
1059-
.where(FieldPath.documentId(), '>=', 'foo')
1052+
db.collectionGroup('foo').where(FieldPath.documentId(), '>=', 'foo')
10601053
).to.throw(
10611054
`Invalid third parameter to Query.where(). When querying a collection group by ` +
10621055
`FieldPath.documentId(), the value provided must result in a valid document path, ` +

0 commit comments

Comments
 (0)