Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit c47106c

Browse files
Add support for Firestore Collection Group queries #1284
1 parent 55130d0 commit c47106c

File tree

7 files changed

+70
-10
lines changed

7 files changed

+70
-10
lines changed

demo-ng/app/tabs/firestore/firestore.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<Button text="Start at 'LA'" (tap)="firestoreStartAt()" class="button"></Button>
3232
<Button text="Start after 'LA'" (tap)="firestoreStartAfter()" class="button"></Button>
3333
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>
34+
<Button text="Collection group query" (tap)="firestoreCollectionGroupQuery()" class="button"></Button>
3435
<Button text="Transactional update (iOS)" (tap)="transactionalUpdate()" class="button"></Button>
3536
<Button text="Write batch" (tap)="writeBatch()" class="button"></Button>
3637
</StackLayout>

demo-ng/app/tabs/firestore/firestore.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ export class FirestoreComponent {
395395
.catch(err => console.log("Delete failed, error: " + err));
396396
}
397397

398+
firestoreCollectionGroupQuery(): void {
399+
firebase.firestore().collectionGroup("cities").where("population", ">=", 1_000_000)
400+
.get()
401+
.then(result => console.log(JSON.stringify(result)))
402+
.catch(err => console.log("Delete failed, error: " + err));
403+
}
404+
398405
doWebGetValueForCompanies(): void {
399406
const path = "/companies";
400407
firebase.database().ref(path)

docs/STORAGE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ In this example we'll determine the remote URL of the previously uploaded file.
212212
Note that your security rules must be version "2" for this to work,
213213
so if it fails try adding this at the top of your rules defined for your storage bucket: `rules_version = '2';`
214214

215+
Also note that this will require an index in your db, so make sure to `catch` any errors when invoking this method
216+
and log out any error messages so you can easily copy-paste the required index into your browser URL bar.
217+
215218
<details>
216219
<summary>Native API</summary>
217220

@@ -225,6 +228,10 @@ so if it fails try adding this at the top of your rules defined for your storage
225228
function (result) {
226229
console.log(JSON.stringify(result));
227230
// see the Web API example below for an advanced example
231+
},
232+
function (error) {
233+
// probably related to a missing index or security rules, see the notes in the readme above
234+
console.log(error);
228235
}
229236
);
230237
```

src/app/firestore/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export namespace firestore {
77
return firebase.firestore.collection(collectionPath);
88
}
99

10+
collectionGroup(id: string): firebase.firestore.CollectionGroup {
11+
return firebase.firestore.collectionGroup(id);
12+
}
13+
1014
doc(path: string): firebase.firestore.DocumentReference {
1115
return firebase.firestore.docRef(path);
1216
}

src/firebase.android.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,17 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
21562156
}
21572157
};
21582158

2159+
firebase.firestore.collectionGroup = (id: string): firestore.CollectionGroup => {
2160+
ensureFirestore();
2161+
try {
2162+
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
2163+
return firebase.firestore._getCollectionGroupQuery(db.collectionGroup(id));
2164+
} catch (ex) {
2165+
console.log("Error in firebase.firestore.collectionGroup: " + ex);
2166+
return null;
2167+
}
2168+
};
2169+
21592170
firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError: (docOrError: DocumentSnapshot | Error) => void, onError: (error: Error) => void): () => void => {
21602171
let options = com.google.firebase.firestore.MetadataChanges.EXCLUDE;
21612172
let onNextCallback: (snapshot: DocumentSnapshot) => void;
@@ -2240,6 +2251,16 @@ firebase.firestore._getDocumentReference = (docRef?: JDocumentReference): firest
22402251
};
22412252
};
22422253

2254+
firebase.firestore._getCollectionGroupQuery = (query?: com.google.firebase.firestore.Query): firestore.CollectionGroup => {
2255+
if (!query) {
2256+
return null;
2257+
}
2258+
2259+
return {
2260+
where: (property: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(undefined, property, opStr, value, query)
2261+
};
2262+
};
2263+
22432264
firebase.firestore._getCollectionReference = (colRef?: JCollectionReference): firestore.CollectionReference => {
22442265
if (!colRef) {
22452266
return null;

src/firebase.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,10 @@ export namespace firestore {
824824
endBefore(snapshot: DocumentSnapshot): Query;
825825
}
826826

827+
export interface CollectionGroup {
828+
where(fieldPath: string, opStr: WhereFilterOp, value: any): Query;
829+
}
830+
827831
export interface CollectionReference extends Query {
828832
readonly id: string;
829833

@@ -970,6 +974,8 @@ export namespace firestore {
970974

971975
function collection(collectionPath: string): CollectionReference;
972976

977+
function collectionGroup(id: string): CollectionGroup;
978+
973979
function doc(collectionPath: string, documentPath?: string): DocumentReference;
974980

975981
function docRef(documentPath: string): DocumentReference;

src/firebase.ios.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,10 @@ const ensureFirestore = (): void => {
16731673
if (typeof (FIRFirestore) === "undefined") {
16741674
throw new Error("Make sure 'firestore' is enabled in 'firebase.nativescript.json', then clean the node_modules and platforms folders");
16751675
}
1676+
1677+
if (!firebase.initialized) {
1678+
throw new Error("Please run firebase.init() before using Firestore");
1679+
}
16761680
};
16771681

16781682
firebase.firestore.WriteBatch = (nativeWriteBatch: FIRWriteBatch): firestore.WriteBatch => {
@@ -1785,18 +1789,23 @@ firebase.firestore.clearPersistence = (): Promise<void> => {
17851789
firebase.firestore.collection = (collectionPath: string): firestore.CollectionReference => {
17861790
ensureFirestore();
17871791
try {
1788-
if (!firebase.initialized) {
1789-
console.log("Please run firebase.init() before firebase.firestore.collection()");
1790-
return null;
1791-
}
1792-
17931792
return firebase.firestore._getCollectionReference(FIRFirestore.firestore().collectionWithPath(collectionPath));
17941793
} catch (ex) {
17951794
console.log("Error in firebase.firestore.collection: " + ex);
17961795
return null;
17971796
}
17981797
};
17991798

1799+
firebase.firestore.collectionGroup = (id: string): firestore.CollectionGroup => {
1800+
ensureFirestore();
1801+
try {
1802+
return firebase.firestore._getCollectionGroupQuery(FIRFirestore.firestore().collectionGroupWithID(id));
1803+
} catch (ex) {
1804+
console.log("Error in firebase.firestore.collectionGroup: " + ex);
1805+
return null;
1806+
}
1807+
};
1808+
18001809
firebase.firestore.onDocumentSnapshot = (docRef: FIRDocumentReference, optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError: (docOrError: DocumentSnapshot | Error) => void, onError: (error: Error) => void): () => void => {
18011810
let includeMetadataChanges = false;
18021811
let onNextCallback: (snapshot: DocumentSnapshot) => void;
@@ -1897,6 +1906,16 @@ firebase.firestore._getCollectionReference = (colRef?: FIRCollectionReference):
18971906
};
18981907
};
18991908

1909+
firebase.firestore._getCollectionGroupQuery = (query?: FIRQuery): firestore.CollectionGroup => {
1910+
if (!query) {
1911+
return null;
1912+
}
1913+
1914+
return {
1915+
where: (property: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(undefined, property, opStr, value, query)
1916+
};
1917+
};
1918+
19001919
firebase.firestore._getDocumentReference = (docRef?: FIRDocumentReference): firestore.DocumentReference => {
19011920
if (!docRef) {
19021921
return null;
@@ -1922,11 +1941,6 @@ firebase.firestore._getDocumentReference = (docRef?: FIRDocumentReference): fire
19221941
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
19231942
ensureFirestore();
19241943
try {
1925-
if (!firebase.initialized) {
1926-
console.log("Please run firebase.init() before firebase.firestore.doc()");
1927-
return null;
1928-
}
1929-
19301944
const fIRCollectionReference = FIRFirestore.firestore().collectionWithPath(collectionPath);
19311945
const fIRDocumentReference = documentPath ? fIRCollectionReference.documentWithPath(documentPath) : fIRCollectionReference.documentWithAutoID();
19321946
return firebase.firestore._getDocumentReference(fIRDocumentReference);

0 commit comments

Comments
 (0)