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

Commit 8bbd128

Browse files
Add 'parent' property to Firestore's DocumentReference and CollectionReference #1188
1 parent 908b710 commit 8bbd128

File tree

4 files changed

+113
-122
lines changed

4 files changed

+113
-122
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,28 @@ export class FirestoreComponent {
160160

161161
firestoreGet(): void {
162162
const collectionRef: firestore.CollectionReference = firebase.firestore().collection("dogs");
163+
console.log(">> collectionRef.parent: " + collectionRef.parent); // should be null (has no parent)
163164
collectionRef.get()
164165
.then((querySnapshot: firestore.QuerySnapshot) => {
165166
querySnapshot.forEach(doc => console.log(`${doc.id} => ${JSON.stringify(doc.data())}`));
166167
})
167168
.catch(err => console.log("Get failed, error: " + err));
168169

170+
// testing 'parent'
171+
const bjDistrictsRef: firestore.CollectionReference = firebase.firestore().collection("cities").doc("BJ").collection("districts");
172+
console.log(">> bjDistrictsRef.parent.id: " + bjDistrictsRef.parent.id);
173+
169174
// examples from https://firebase.google.com/docs/firestore/query-data/get-data
170175
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("BJ");
176+
console.log(">> docRef.parent.id: " + docRef.parent.id);
171177

172178
docRef.get()
173179
.then((doc: firestore.DocumentSnapshot) => {
174180
if (doc.exists) {
175181
console.log("Document data:", JSON.stringify(doc.data()));
176182
// since there's a reference stored here, we can use that to retrieve its data
177183
const docRef: firestore.DocumentReference = doc.data().referenceToCitiesDC;
184+
console.log(">> docRef2.parent.id: " + docRef.parent.id);
178185
docRef.get()
179186
.then(res => console.log("docref.get: " + JSON.stringify(res.data())))
180187
.catch(err => console.log("docref.get error: " + err));

src/firebase.android.ts

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DocumentSnapshot extends DocumentSnapshotBase {
3636
};
3737

3838
constructor(public snapshot: com.google.firebase.firestore.DocumentSnapshot) {
39-
super(snapshot ? snapshot.getId() : null, snapshot.exists(), firebase.toJsObject(snapshot.getData()), convertDocRef(snapshot.getReference()));
39+
super(snapshot ? snapshot.getId() : null, snapshot.exists(), firebase.toJsObject(snapshot.getData()), firebase.firestore._getDocumentReference(snapshot.getReference()));
4040
this.android = snapshot;
4141
}
4242
}
@@ -2265,23 +2265,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22652265
}
22662266

22672267
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
2268-
const collectionRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath);
2269-
2270-
return {
2271-
id: collectionRef.getId(),
2272-
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
2273-
add: document => firebase.firestore.add(collectionPath, document),
2274-
get: () => firebase.firestore.get(collectionPath),
2275-
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
2276-
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
2277-
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef),
2278-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, optionsOrCallback, callback),
2279-
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, collectionRef),
2280-
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, collectionRef),
2281-
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, collectionRef),
2282-
endBefore: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshot, collectionRef),
2283-
};
2284-
2268+
return firebase.firestore._getCollectionReference(db.collection(collectionPath));
22852269
} catch (ex) {
22862270
console.log("Error in firebase.firestore.collection: " + ex);
22872271
return null;
@@ -2331,18 +2315,49 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
23312315
return () => listener.remove();
23322316
};
23332317

2334-
firebase.firestore._getDocumentReference = (javaObj: JDocumentReference, collectionPath, documentPath): firestore.DocumentReference => {
2318+
firebase.firestore._getDocumentReference = (docRef?: JDocumentReference): firestore.DocumentReference => {
2319+
if (!docRef) {
2320+
return null;
2321+
}
2322+
2323+
const collectionPath = docRef.getParent().getPath();
2324+
23352325
return {
23362326
discriminator: "docRef",
2337-
id: javaObj.getId(),
2338-
path: javaObj.getPath(),
2339-
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
2340-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, javaObj.getId(), data, options),
2341-
get: () => firebase.firestore.getDocument(collectionPath, javaObj.getId()),
2342-
update: (data: any) => firebase.firestore.update(collectionPath, javaObj.getId(), data),
2343-
delete: () => firebase.firestore.delete(collectionPath, javaObj.getId()),
2344-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(javaObj, optionsOrCallback, callback),
2345-
android: javaObj
2327+
id: docRef.getId(),
2328+
parent: firebase.firestore._getCollectionReference(docRef.getParent()),
2329+
path: docRef.getPath(),
2330+
collection: cp => firebase.firestore.collection(`${collectionPath}/${docRef.getId()}/${cp}`),
2331+
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2332+
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2333+
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2334+
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2335+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((doc: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callback),
2336+
android: docRef
2337+
};
2338+
};
2339+
2340+
firebase.firestore._getCollectionReference = (colRef?: JCollectionReference): firestore.CollectionReference => {
2341+
if (!colRef) {
2342+
return null;
2343+
}
2344+
2345+
const collectionPath = colRef.getPath();
2346+
2347+
return {
2348+
id: colRef.getId(),
2349+
parent: firebase.firestore._getDocumentReference(colRef.getParent()),
2350+
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
2351+
add: document => firebase.firestore.add(collectionPath, document),
2352+
get: () => firebase.firestore.get(collectionPath),
2353+
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
2354+
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, colRef),
2355+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, colRef),
2356+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(colRef, optionsOrCallback, callback),
2357+
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, colRef),
2358+
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, colRef),
2359+
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, colRef),
2360+
endBefore: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshot, colRef),
23462361
};
23472362
};
23482363

@@ -2361,7 +2376,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
23612376
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
23622377
const colRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath);
23632378
const docRef: com.google.firebase.firestore.DocumentReference = documentPath ? colRef.document(documentPath) : colRef.document();
2364-
return firebase.firestore._getDocumentReference(docRef, collectionPath, documentPath);
2379+
return firebase.firestore._getDocumentReference(docRef);
23652380
} catch (ex) {
23662381
console.log("Error in firebase.firestore.doc: " + ex);
23672382
return null;
@@ -2375,9 +2390,7 @@ firebase.firestore.docRef = (documentPath: string): firestore.DocumentReference
23752390
}
23762391

23772392
const db: com.google.firebase.firestore.FirebaseFirestore = com.google.firebase.firestore.FirebaseFirestore.getInstance();
2378-
const docRef: JDocumentReference = db.document(documentPath);
2379-
2380-
return convertDocRef(docRef);
2393+
return firebase.firestore._getDocumentReference(db.document(documentPath));
23812394
};
23822395

23832396
firebase.firestore.add = (collectionPath: string, document: any): Promise<firestore.DocumentReference> => {
@@ -2393,17 +2406,7 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
23932406

23942407
const onSuccessListener = new gmsTasks.OnSuccessListener({
23952408
onSuccess: (docRef: com.google.firebase.firestore.DocumentReference) => {
2396-
resolve({
2397-
discriminator: "docRef",
2398-
id: docRef.getId(),
2399-
path: docRef.getPath(),
2400-
collection: cp => firebase.firestore.collection(cp),
2401-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2402-
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2403-
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2404-
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2405-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callback)
2406-
});
2409+
resolve(firebase.firestore._getDocumentReference(docRef))
24072410
}
24082411
});
24092412

@@ -2689,23 +2692,7 @@ firebase.firestore.endBefore = (collectionPath: string, snapshot: DocumentSnapsh
26892692
};
26902693

26912694
export type JDocumentReference = com.google.firebase.firestore.DocumentReference;
2692-
2693-
function convertDocRef(docRef: JDocumentReference): firestore.DocumentReference {
2694-
const collectionPath = docRef.getParent().getPath();
2695-
2696-
return {
2697-
discriminator: "docRef",
2698-
id: docRef.getId(),
2699-
path: docRef.getPath(),
2700-
collection: cp => firebase.firestore.collection(`${collectionPath}/${docRef.getId()}/${cp}`),
2701-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2702-
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2703-
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2704-
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2705-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((doc: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callback),
2706-
android: docRef
2707-
};
2708-
}
2695+
export type JCollectionReference = com.google.firebase.firestore.CollectionReference;
27092696

27102697
function convertDocChangeType(type: com.google.firebase.firestore.DocumentChange.Type) {
27112698
switch (type) {

src/firebase.d.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,13 @@ export namespace firestore {
788788
}
789789

790790
export interface DocumentReference {
791-
discriminator: "docRef";
792-
id: string;
793-
path: string;
791+
readonly discriminator: "docRef";
792+
readonly id: string;
793+
/**
794+
* A reference to the Collection to which this DocumentReference belongs.
795+
*/
796+
readonly parent: CollectionReference;
797+
readonly path: string;
794798
collection: (collectionPath: string) => CollectionReference;
795799
set: (document: any, options?: SetOptions) => Promise<void>;
796800
get: () => Promise<DocumentSnapshot>;
@@ -824,7 +828,12 @@ export namespace firestore {
824828
}
825829

826830
export interface CollectionReference extends Query {
827-
id: string;
831+
readonly id: string;
832+
833+
/**
834+
* A reference to the containing Document if this is a subcollection, else null.
835+
*/
836+
readonly parent: DocumentReference | null;
828837

829838
doc(documentPath?: string): DocumentReference;
830839

0 commit comments

Comments
 (0)