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

Commit 156b92f

Browse files
Merge branch 'master' into firestore-onsnapshot
2 parents 238790f + 8bbd128 commit 156b92f

File tree

10 files changed

+125
-133
lines changed

10 files changed

+125
-133
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
- [Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
44
- [Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

6+
## 8.0.1 (2019, March 16)
7+
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/95?closed=1)
8+
9+
610
## 8.0.0 (2019, February 26)
711
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/94?closed=1)
812

913
### BREAKING CHANGES
1014
- `getAuthToken` no longer returns a token (`string`), but an `GetAuthTokenResult` object which contains more data. See #1008.
1115
- For better alignment with the Web API, `changePassword` is now `updatePassword`, `resetPassword` is now `sendPasswordResetEmail`. See #1080.
1216

17+
1318
## 7.7.0 (2019, January 20)
1419
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/92?closed=1)
1520

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));

demo-ng/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"nativescript-angular": "^6.1.0",
2626
"nativescript-camera": "~4.1.1",
2727
"nativescript-imagepicker": "~6.0.5",
28-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.0.tgz",
28+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
2929
"nativescript-theme-core": "~1.0.4",
3030
"reflect-metadata": "~0.1.10",
3131
"rxjs": "~6.0.0 || >=6.1.0",

demo-push/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
},
1111
"dependencies": {
12-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.0.tgz",
12+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
1515
"tns-core-modules": "~4.2.0"

demo-vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
},
1616
"dependencies": {
17-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.0.tgz",
17+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
1818
"nativescript-theme-core": "^1.0.4",
1919
"nativescript-vue": "^2.0.0",
2020
"tns-core-modules": "^5.0.2"

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"firebase-functions": "^2.0.5",
13-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.0.tgz",
13+
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-8.0.1.tgz",
1414
"nativescript-theme-core": "^1.0.4",
1515
"nativescript-unit-test-runner": "^0.3.4",
1616
"tns-core-modules": "~5.2.0"

src/firebase.android.ts

Lines changed: 47 additions & 63 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), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, optionsOrCallback, callbackOrOnError, onError),
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;
@@ -2351,19 +2335,49 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
23512335
return () => listener.remove();
23522336
};
23532337

2354-
firebase.firestore._getDocumentReference = (javaObj: JDocumentReference, collectionPath, documentPath): firestore.DocumentReference => {
2338+
firebase.firestore._getDocumentReference = (docRef?: JDocumentReference): firestore.DocumentReference => {
2339+
if (!docRef) {
2340+
return null;
2341+
}
2342+
2343+
const collectionPath = docRef.getParent().getPath();
2344+
23552345
return {
23562346
discriminator: "docRef",
2357-
id: javaObj.getId(),
2358-
path: javaObj.getPath(),
2359-
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
2360-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, javaObj.getId(), data, options),
2361-
get: () => firebase.firestore.getDocument(collectionPath, javaObj.getId()),
2362-
update: (data: any) => firebase.firestore.update(collectionPath, javaObj.getId(), data),
2363-
delete: () => firebase.firestore.delete(collectionPath, javaObj.getId()),
2364-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void,
2365-
onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(javaObj, optionsOrCallback, callbackOrOnError, onError),
2366-
android: javaObj
2347+
id: docRef.getId(),
2348+
parent: firebase.firestore._getCollectionReference(docRef.getParent()),
2349+
path: docRef.getPath(),
2350+
collection: cp => firebase.firestore.collection(`${collectionPath}/${docRef.getId()}/${cp}`),
2351+
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2352+
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2353+
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2354+
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2355+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(javaObj, optionsOrCallback, callbackOrOnError, onError),
2356+
android: docRef
2357+
};
2358+
};
2359+
2360+
firebase.firestore._getCollectionReference = (colRef?: JCollectionReference): firestore.CollectionReference => {
2361+
if (!colRef) {
2362+
return null;
2363+
}
2364+
2365+
const collectionPath = colRef.getPath();
2366+
2367+
return {
2368+
id: colRef.getId(),
2369+
parent: firebase.firestore._getDocumentReference(colRef.getParent()),
2370+
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
2371+
add: document => firebase.firestore.add(collectionPath, document),
2372+
get: () => firebase.firestore.get(collectionPath),
2373+
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
2374+
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, colRef),
2375+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, colRef),
2376+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(colRef, optionsOrCallback, callback),
2377+
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, colRef),
2378+
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, colRef),
2379+
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, colRef),
2380+
endBefore: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshot, colRef),
23672381
};
23682382
};
23692383

@@ -2382,7 +2396,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
23822396
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
23832397
const colRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath);
23842398
const docRef: com.google.firebase.firestore.DocumentReference = documentPath ? colRef.document(documentPath) : colRef.document();
2385-
return firebase.firestore._getDocumentReference(docRef, collectionPath, documentPath);
2399+
return firebase.firestore._getDocumentReference(docRef);
23862400
} catch (ex) {
23872401
console.log("Error in firebase.firestore.doc: " + ex);
23882402
return null;
@@ -2396,9 +2410,7 @@ firebase.firestore.docRef = (documentPath: string): firestore.DocumentReference
23962410
}
23972411

23982412
const db: com.google.firebase.firestore.FirebaseFirestore = com.google.firebase.firestore.FirebaseFirestore.getInstance();
2399-
const docRef: JDocumentReference = db.document(documentPath);
2400-
2401-
return convertDocRef(docRef);
2413+
return firebase.firestore._getDocumentReference(db.document(documentPath));
24022414
};
24032415

24042416
firebase.firestore.add = (collectionPath: string, document: any): Promise<firestore.DocumentReference> => {
@@ -2414,18 +2426,7 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
24142426

24152427
const onSuccessListener = new gmsTasks.OnSuccessListener({
24162428
onSuccess: (docRef: com.google.firebase.firestore.DocumentReference) => {
2417-
resolve({
2418-
discriminator: "docRef",
2419-
id: docRef.getId(),
2420-
path: docRef.getPath(),
2421-
collection: cp => firebase.firestore.collection(cp),
2422-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2423-
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2424-
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2425-
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2426-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void,
2427-
onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callbackOrOnError, onError)
2428-
});
2429+
resolve(firebase.firestore._getDocumentReference(docRef))
24292430
}
24302431
});
24312432

@@ -2711,24 +2712,7 @@ firebase.firestore.endBefore = (collectionPath: string, snapshot: DocumentSnapsh
27112712
};
27122713

27132714
export type JDocumentReference = com.google.firebase.firestore.DocumentReference;
2714-
2715-
function convertDocRef(docRef: JDocumentReference): firestore.DocumentReference {
2716-
const collectionPath = docRef.getParent().getPath();
2717-
2718-
return {
2719-
discriminator: "docRef",
2720-
id: docRef.getId(),
2721-
path: docRef.getPath(),
2722-
collection: cp => firebase.firestore.collection(`${collectionPath}/${docRef.getId()}/${cp}`),
2723-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2724-
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2725-
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2726-
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2727-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void,
2728-
onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callbackOrOnError, onError),
2729-
android: docRef
2730-
};
2731-
}
2715+
export type JCollectionReference = com.google.firebase.firestore.CollectionReference;
27322716

27332717
function convertDocChangeType(type: com.google.firebase.firestore.DocumentChange.Type) {
27342718
switch (type) {

src/firebase.d.ts

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

790790
export interface DocumentReference {
791-
discriminator: "docRef";
791+
readonly discriminator: "docRef";
792792

793-
id: string;
793+
readonly id: string;
794+
795+
/**
796+
* A reference to the Collection to which this DocumentReference belongs.
797+
*/
798+
readonly parent: CollectionReference;
794799

795-
path: string;
800+
readonly path: string;
796801

797802
collection: (collectionPath: string) => CollectionReference;
798803

@@ -832,7 +837,12 @@ export namespace firestore {
832837
}
833838

834839
export interface CollectionReference extends Query {
835-
id: string;
840+
readonly id: string;
841+
842+
/**
843+
* A reference to the containing Document if this is a subcollection, else null.
844+
*/
845+
readonly parent: DocumentReference | null;
836846

837847
doc(documentPath?: string): DocumentReference;
838848

0 commit comments

Comments
 (0)