Skip to content

Expose Collection Group queries API. #1722

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 1 commit into from
May 1, 2019
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
12 changes: 12 additions & 0 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6025,6 +6025,18 @@ declare namespace firebase.firestore {
*/
doc(documentPath: string): DocumentReference;

/**
* Creates and returns a new Query that includes all documents in the
* database that are contained in a collection or subcollection with the
* given collectionId.
*
* @param collectionId Identifies the collections to query over. Every
* collection or subcollection with this ID as the last segment of its path
* will be included. Cannot contain a slash.
* @return The created Query.
*/
collectionGroup(collectionId: string): Query;

/**
* Executes the given `updateFunction` and then attempts to commit the changes
* applied within the transaction. If any document read within the transaction
Expand Down
6 changes: 2 additions & 4 deletions packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ export class FirebaseFirestore {
*/
doc(documentPath: string): DocumentReference;

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

/**
* Executes the given updateFunction and then attempts to commit the
Expand Down
6 changes: 5 additions & 1 deletion packages/firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Unreleased
# Unreleased (I/O)
- [feature] You can now query across all collections in your database with a
given collection ID using the `FirebaseFirestore.collectionGroup()` method.

# 1.1.4
- [feature] Added an `experimentalForceLongPolling` setting that that can be
used to work around proxies that prevent the Firestore client from connecting
to the Firestore backend.
Expand Down
3 changes: 1 addition & 2 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
return DocumentReference.forPath(ResourcePath.fromString(pathString), this);
}

// TODO(b/116617988): Fix name, uncomment d.ts definitions, and update CHANGELOG.md.
_collectionGroup(collectionId: string): firestore.Query {
collectionGroup(collectionId: string): firestore.Query {
validateExactNumberOfArgs('Firestore.collectionGroup', arguments, 1);
validateArgType(
'Firestore.collectionGroup',
Expand Down
25 changes: 9 additions & 16 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ const FieldPath = firebase.firestore!.FieldPath;
const GeoPoint = firebase.firestore!.GeoPoint;
const Timestamp = firebase.firestore!.Timestamp;

// TODO(b/116617988): Use public API.
interface FirestoreInternal extends firestore.FirebaseFirestore {
_collectionGroup(collectionId: string): firestore.Query;
}

apiDescribe('Queries', persistence => {
addEqualityMatcher();

Expand Down Expand Up @@ -600,9 +595,7 @@ apiDescribe('Queries', persistence => {
}
await batch.commit();

const querySnapshot = await (db as FirestoreInternal)
._collectionGroup(collectionGroup)
.get();
const querySnapshot = await db.collectionGroup(collectionGroup).get();
expect(querySnapshot.docs.map(d => d.id)).to.deep.equal([
'cg-doc1',
'cg-doc2',
Expand Down Expand Up @@ -634,8 +627,8 @@ apiDescribe('Queries', persistence => {
}
await batch.commit();

let querySnapshot = await (db as FirestoreInternal)
._collectionGroup(collectionGroup)
let querySnapshot = await db
.collectionGroup(collectionGroup)
.orderBy(FieldPath.documentId())
.startAt(`a/b`)
.endAt('a/b0')
Expand All @@ -646,8 +639,8 @@ apiDescribe('Queries', persistence => {
'cg-doc4'
]);

querySnapshot = await (db as FirestoreInternal)
._collectionGroup(collectionGroup)
querySnapshot = await db
.collectionGroup(collectionGroup)
.orderBy(FieldPath.documentId())
.startAfter('a/b')
.endBefore(`a/b/${collectionGroup}/cg-doc3`)
Expand Down Expand Up @@ -677,8 +670,8 @@ apiDescribe('Queries', persistence => {
}
await batch.commit();

let querySnapshot = await (db as FirestoreInternal)
._collectionGroup(collectionGroup)
let querySnapshot = await db
.collectionGroup(collectionGroup)
.where(FieldPath.documentId(), '>=', `a/b`)
.where(FieldPath.documentId(), '<=', 'a/b0')
.get();
Expand All @@ -688,8 +681,8 @@ apiDescribe('Queries', persistence => {
'cg-doc4'
]);

querySnapshot = await (db as FirestoreInternal)
._collectionGroup(collectionGroup)
querySnapshot = await db
.collectionGroup(collectionGroup)
.where(FieldPath.documentId(), '>', `a/b`)
.where(FieldPath.documentId(), '<', `a/b/${collectionGroup}/cg-doc3`)
.get();
Expand Down
13 changes: 3 additions & 10 deletions packages/firestore/test/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ import {
const FieldPath = firebase.firestore!.FieldPath;
const FieldValue = firebase.firestore!.FieldValue;

// TODO(b/116617988): Use public API.
interface FirestoreInternal extends firestore.FirebaseFirestore {
_collectionGroup(collectionId: string): firestore.Query;
}

// We're using 'as any' to pass invalid values to APIs for testing purposes.
// tslint:disable:no-any

Expand Down Expand Up @@ -930,8 +925,8 @@ apiDescribe('Validation:', persistence => {
const query = db
.collection('collection')
.orderBy(firebase.firestore!.FieldPath.documentId());
const cgQuery = (db as FirestoreInternal)
._collectionGroup('collection')
const cgQuery = db
.collectionGroup('collection')
.orderBy(firebase.firestore!.FieldPath.documentId());
expect(() => query.startAt(1)).to.throw(
'Invalid query. Expected a string for document ID in ' +
Expand Down Expand Up @@ -1054,9 +1049,7 @@ apiDescribe('Validation:', persistence => {
'FieldPath.documentId(), but it was: 1.'
);
expect(() =>
(db as FirestoreInternal)
._collectionGroup('foo')
.where(FieldPath.documentId(), '>=', 'foo')
db.collectionGroup('foo').where(FieldPath.documentId(), '>=', 'foo')
).to.throw(
`Invalid third parameter to Query.where(). When querying a collection group by ` +
`FieldPath.documentId(), the value provided must result in a valid document path, ` +
Expand Down