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

Commit 041dd7b

Browse files
Merge pull request #857 from vcooley/feature/expanded-query
Feature/expanded query
2 parents 06fcae8 + 8042054 commit 041dd7b

File tree

8 files changed

+114
-20
lines changed

8 files changed

+114
-20
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
2626
<Button text="Where, Order, Limit" (tap)="firestoreWhereOrderLimit()" class="button"></Button>
2727
<Button text="Where array_contains" (tap)="firestoreWhereCityHasALake()" class="button"></Button>
28+
<Button text="Start After" (tap)="firestoreStartAfter()" class="button"></Button>
2829
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>
2930
<Button text="Transactional update (iOS)" (tap)="transactionalUpdate()" class="button"></Button>
3031
<Button text="Write batch" (tap)="writeBatch()" class="button"></Button>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,17 @@ export class FirestoreComponent {
344344
.then(() => console.log(`Transaction successfully committed`))
345345
.catch(error => console.log("doTransaction error: " + error));
346346
}
347+
348+
public firestoreStartAfter(): void {
349+
const beijingSnapshot = firebase.firestore().collection('cities')
350+
.doc('BJ')
351+
.get()
352+
.then(doc => {
353+
firebase.firestore().collection('cities')
354+
.orderBy('country', 'asc')
355+
.startAfter(doc)
356+
.get()
357+
.then(snap => snap.forEach(doc => console.log(doc.id, doc.data())));
358+
});
359+
}
347360
}

demo-ng/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
"node_modules",
2727
"platforms"
2828
]
29-
}
29+
}

src/firebase-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export const firebase: any = {
215215
}
216216
};
217217

218-
export class DocumentSnapshot implements firestore.DocumentSnapshot {
218+
export abstract class DocumentSnapshot implements firestore.DocumentSnapshot {
219219
public data: () => firestore.DocumentData;
220220
constructor(public id: string, public exists: boolean, documentData: firestore.DocumentData) {
221221
this.data = () => exists ? documentData : undefined;

src/firebase.android.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DocumentSnapshot, firebase, GeoPoint, QuerySnapshot, isDocumentReference } from "./firebase-common";
1+
import { DocumentSnapshot as DocumentSnapshotBase, firebase, GeoPoint, QuerySnapshot, isDocumentReference } from "./firebase-common";
22
import * as appModule from "tns-core-modules/application";
33
import { AndroidActivityResultEventData } from "tns-core-modules/application";
44
import { ad as AndroidUtils, layout } from "tns-core-modules/utils/utils";
@@ -8,6 +8,15 @@ import { firestore, User } from "./firebase";
88

99
declare const android, com, org: any;
1010

11+
class DocumentSnapshot extends DocumentSnapshotBase {
12+
android: com.google.firebase.firestore.DocumentSnapshot;
13+
14+
constructor(public snapshot: com.google.firebase.firestore.DocumentSnapshot) {
15+
super(snapshot ? snapshot.getId() : null, snapshot.exists(), firebase.toJsObject(snapshot.getData()));
16+
this.android = snapshot;
17+
}
18+
}
19+
1120
firebase._launchNotification = null;
1221
firebase._cachedDynamicLink = null;
1322

@@ -2264,7 +2273,11 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22642273
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
22652274
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
22662275
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef),
2267-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, callback)
2276+
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, callback),
2277+
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, collectionRef),
2278+
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, collectionRef),
2279+
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, collectionRef),
2280+
endBefore: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endBefore(collectionPath, snapshot, collectionRef),
22682281
};
22692282

22702283
} catch (ex) {
@@ -2277,7 +2290,7 @@ firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.D
22772290
const listener = docRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
22782291
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception) => {
22792292
if (exception === null) {
2280-
callback(new DocumentSnapshot(snapshot ? snapshot.getId() : null, snapshot.exists(), firebase.toJsObject(snapshot.getData())));
2293+
callback(new DocumentSnapshot(snapshot));
22812294
}
22822295
})
22832296
})
@@ -2296,7 +2309,7 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
22962309
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
22972310
for (let i = 0; i < snapshot.size(); i++) {
22982311
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = snapshot.getDocuments().get(i);
2299-
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, firebase.toJsObject(documentSnapshot.getData())));
2312+
docSnapshots.push(new DocumentSnapshot(documentSnapshot));
23002313
}
23012314
const snap = new QuerySnapshot();
23022315
snap.docSnapshots = docSnapshots;
@@ -2505,7 +2518,7 @@ firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.Q
25052518
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
25062519
for (let i = 0; i < result.size(); i++) {
25072520
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
2508-
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, firebase.toJsObject(documentSnapshot.getData())));
2521+
docSnapshots.push(new DocumentSnapshot(documentSnapshot));
25092522
}
25102523
const snap = new QuerySnapshot();
25112524
snap.docSnapshots = docSnapshots;
@@ -2554,8 +2567,7 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
25542567
reject(ex && ex.getReason ? ex.getReason() : ex);
25552568
} else {
25562569
const result: com.google.firebase.firestore.DocumentSnapshot = task.getResult();
2557-
const exists = result.exists();
2558-
resolve(new DocumentSnapshot(exists ? result.getId() : null, exists, firebase.toJsObject(result.getData())));
2570+
resolve(new DocumentSnapshot(result));
25592571
}
25602572
}
25612573
});
@@ -2592,7 +2604,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
25922604
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
25932605
for (let i = 0; i < result.size(); i++) {
25942606
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
2595-
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, firebase.toJsObject(documentSnapshot.getData())));
2607+
docSnapshots.push(new DocumentSnapshot(documentSnapshot));
25962608
}
25972609
const snap = new QuerySnapshot();
25982610
snap.docSnapshots = docSnapshots;
@@ -2605,7 +2617,11 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
26052617
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
26062618
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
26072619
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
2608-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback)
2620+
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback),
2621+
startAfter: (snapshot: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, snapshot, query),
2622+
startAt: (snapshot: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, snapshot, query),
2623+
endAt: (snapshot: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, snapshot, query),
2624+
endBefore: (snapshot: DocumentSnapshot) => firebase.firestore.endBefore(collectionPath, snapshot, query),
26092625
};
26102626
};
26112627

@@ -2655,4 +2671,22 @@ firebase.firestore.limit = (collectionPath: string, limit: number, query: com.go
26552671
return firebase.firestore._getQuery(collectionPath, query);
26562672
};
26572673

2674+
firebase.firestore.startAfter = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2675+
query = query.startAt(snapshot.android);
2676+
return firebase.firestore._getQuery(collectionPath, query);
2677+
};
2678+
2679+
firebase.firestore.startAt = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2680+
return firebase.firestore._getQuery(collectionPath, query);
2681+
};
2682+
2683+
firebase.firestore.endAt = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2684+
return firebase.firestore._getQuery(collectionPath, query);
2685+
};
2686+
2687+
firebase.firestore.endBefore = (collectionPath: string, snapshot: DocumentSnapshot, query: com.google.firebase.firestore.Query): firestore.Query => {
2688+
return firebase.firestore._getQuery(collectionPath, query);
2689+
};
2690+
2691+
26582692
module.exports = firebase;

src/firebase.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,10 @@ export namespace firestore {
713713
}
714714

715715
export interface DocumentSnapshot {
716+
ios?: FIRDocumentSnapshot;
717+
android?: com.google.firebase.firestore.DocumentSnapshot;
716718
id: string;
717719
exists: boolean;
718-
719720
data(): DocumentData;
720721
}
721722

@@ -744,6 +745,14 @@ export namespace firestore {
744745
limit(limit: number): Query;
745746

746747
onSnapshot(callback: (snapshot: QuerySnapshot) => void): () => void;
748+
749+
startAt(snapshot: DocumentSnapshot): Query;
750+
751+
startAfter(snapshot: DocumentSnapshot): Query;
752+
753+
endAt(snaptshot: DocumentSnapshot): Query;
754+
755+
endBefore(snapshot: DocumentSnapshot): Query;
747756
}
748757

749758
export interface CollectionReference extends Query {

src/firebase.ios.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { firebase, DocumentSnapshot, QuerySnapshot, GeoPoint, isDocumentReference } from "./firebase-common";
1+
import { firebase, DocumentSnapshot as DocumentSnapshotBase, QuerySnapshot, GeoPoint, isDocumentReference } from "./firebase-common";
22
import * as application from "tns-core-modules/application";
33
import { ios as iOSUtils } from "tns-core-modules/utils/utils";
44
import { getClass } from "tns-core-modules/utils/types";
@@ -14,6 +14,14 @@ firebase._cachedInvitation = null;
1414
firebase._cachedDynamicLink = null;
1515
firebase._configured = false;
1616

17+
class DocumentSnapshot extends DocumentSnapshotBase {
18+
ios: FIRDocumentSnapshot;
19+
constructor(snapshot: FIRDocumentSnapshot) {
20+
super(snapshot.documentID, snapshot.exists, firebase.toJsObject(snapshot.data()));
21+
this.ios = snapshot;
22+
}
23+
}
24+
1725
// Note that FIRApp.configure must be called only once, but not here (see https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/564)
1826

1927
const invokeOnRunLoop = (() => {
@@ -2205,7 +2213,11 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22052213
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
22062214
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, fIRCollectionReference),
22072215
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, fIRCollectionReference),
2208-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(fIRCollectionReference, callback)
2216+
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(fIRCollectionReference, callback),
2217+
startAfter: (document: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, document, fIRCollectionReference),
2218+
startAt: (document: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, document, fIRCollectionReference),
2219+
endAt: (document: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, document, fIRCollectionReference),
2220+
endBefore: (document: DocumentSnapshot) => firebase.firestore.endBefore(collectionPath, document, fIRCollectionReference),
22092221
};
22102222

22112223
} catch (ex) {
@@ -2217,7 +2229,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22172229
firebase.firestore.onDocumentSnapshot = (docRef: FIRDocumentReference, callback: (doc: DocumentSnapshot) => void): () => void => {
22182230
const listener = docRef.addSnapshotListener((snapshot: FIRDocumentSnapshot, error: NSError) => {
22192231
if (!error && snapshot) {
2220-
callback(new DocumentSnapshot(snapshot.documentID, snapshot.exists, firebase.toJsObject(snapshot.data())));
2232+
callback(new DocumentSnapshot(snapshot));
22212233
}
22222234
});
22232235

@@ -2238,7 +2250,7 @@ firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, callb
22382250
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
22392251
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
22402252
const document: FIRQueryDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2241-
docSnapshots.push(new DocumentSnapshot(document.documentID, true, firebase.toJsObject(document.data())));
2253+
docSnapshots.push(new DocumentSnapshot(document));
22422254
}
22432255

22442256
const snap = new QuerySnapshot();
@@ -2454,7 +2466,7 @@ firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.Q
24542466
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
24552467
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
24562468
const document: FIRQueryDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2457-
docSnapshots.push(new DocumentSnapshot(document.documentID, true, firebase.toJsObject(document.data())));
2469+
docSnapshots.push(new DocumentSnapshot(document));
24582470
}
24592471
const snap = new QuerySnapshot();
24602472
snap.docSnapshots = docSnapshots;
@@ -2489,7 +2501,7 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
24892501
reject(error.localizedDescription);
24902502
} else {
24912503
const exists = snapshot.exists;
2492-
resolve(new DocumentSnapshot(exists ? snapshot.documentID : null, exists, firebase.toJsObject(snapshot.data())));
2504+
resolve(new DocumentSnapshot(snapshot));
24932505
}
24942506
});
24952507

@@ -2511,7 +2523,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firest
25112523
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
25122524
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
25132525
const document: FIRQueryDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2514-
docSnapshots.push(new DocumentSnapshot(document.documentID, true, firebase.toJsObject(document.data())));
2526+
docSnapshots.push(new DocumentSnapshot(document));
25152527
}
25162528
const snap = new QuerySnapshot();
25172529
snap.docSnapshots = docSnapshots;
@@ -2522,7 +2534,11 @@ firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firest
25222534
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
25232535
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
25242536
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
2525-
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback)
2537+
onSnapshot: (callback: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, callback),
2538+
startAfter: (document: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, document, query),
2539+
startAt: (document: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, document, query),
2540+
endAt: (document: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, document, query),
2541+
endBefore: (document: DocumentSnapshot) => firebase.firestore.endBefore(collectionPath, document, query),
25262542
};
25272543
};
25282544

@@ -2573,6 +2589,26 @@ firebase.firestore.limit = (collectionPath: string, limit: number, query: FIRQue
25732589
return firebase.firestore._getQuery(collectionPath, query);
25742590
};
25752591

2592+
firebase.firestore.startAt = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2593+
query = query.queryStartingAtDocument(document.ios);
2594+
return firebase.firestore._getQuery(collectionPath, query);
2595+
}
2596+
2597+
firebase.firestore.startAfter = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2598+
query = query.queryStartingAfterDocument(document.ios);
2599+
return firebase.firestore._getQuery(collectionPath, query);
2600+
};
2601+
2602+
firebase.firestore.endAt = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2603+
query = query.queryEndingAtDocument(document.ios);
2604+
return firebase.firestore._getQuery(collectionPath, query);
2605+
};
2606+
2607+
firebase.firestore.endBefore = (collectionPath: string, document: DocumentSnapshot, query: FIRQuery) => {
2608+
query = query.queryEndingBeforeDocument(document.ios);
2609+
return firebase.firestore._getQuery(collectionPath, query);
2610+
};
2611+
25762612
// see https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate?language=objc
25772613
class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNotificationCenterDelegate {
25782614
public static ObjCProtocols = [];

src/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"demo.android": "npm run preparedemo && cd ../demo && rimraf platforms/android && tns run android",
4646
"demo.android.linked": "npm run tsc && cd ../demo && tns run android --syncAllFiles",
4747
"demo-ng.android": "npm run preparedemo-ng && cd ../demo-ng && tns run android",
48+
"demo-ng.android.bundled": "npm run preparedemo-ng && cd ../demo-ng && tns run android --bundle",
4849
"test": "npm run tslint && npm run tslint.demo && cd ../demo && tns build ios && tns build android",
4950
"test.ios": "cd ../demo && tns test ios --emulator",
5051
"test.ios.device": "cd ../demo && tns platform remove ios && tns test ios",

0 commit comments

Comments
 (0)