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

Commit 5825dc2

Browse files
Firestore anytime soon? #507 (orderBy, limit)
1 parent 8406397 commit 5825dc2

File tree

5 files changed

+135
-68
lines changed

5 files changed

+135
-68
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<ActionBar title="My App" class="action-bar">
22
</ActionBar>
33

4-
<StackLayout class="page">
5-
<Label text="Authentication" class="h2"></Label>
6-
<Button text="login anonymously" (tap)="loginAnonymously()" class="button button-user"></Button>
7-
<Label text="Firestore" class="h2"></Label>
8-
<Button text="Add" (tap)="firestoreAdd()" class="button"></Button>
9-
<Button text="Set" (tap)="firestoreSet()" class="button"></Button>
10-
<Button text="Set (auto id)" (tap)="firestoreSetByAutoID()" class="button"></Button>
11-
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
12-
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
13-
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
14-
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>
15-
16-
</StackLayout>
4+
<ScrollView>
5+
<StackLayout class="page">
6+
<Label text="Authentication" class="h2"></Label>
7+
<Button text="login anonymously" (tap)="loginAnonymously()" class="button button-user"></Button>
8+
<Label text="Firestore" class="h2"></Label>
9+
<Button text="Add" (tap)="firestoreAdd()" class="button"></Button>
10+
<Button text="Set" (tap)="firestoreSet()" class="button"></Button>
11+
<Button text="Set (auto id)" (tap)="firestoreSetByAutoID()" class="button"></Button>
12+
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
13+
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
14+
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
15+
<Button text="Where, Order, Limit" (tap)="firestoreWhereOrderLimit()" class="button"></Button>
16+
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>
17+
</StackLayout>
18+
</ScrollView>

demo-ng/app/item/items.component.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ export class ItemsComponent implements OnInit {
6060
population: 3900000
6161
});
6262

63+
citiesRef.doc("SAC").set({
64+
name: "Sacramento",
65+
state: "CA",
66+
country: "USA",
67+
capital: true,
68+
population: 500000
69+
});
70+
6371
citiesRef.doc("DC").set({
6472
name: "Washington, D.C.",
6573
state: "WA",
@@ -130,7 +138,7 @@ export class ItemsComponent implements OnInit {
130138
public firestoreWhere(): void {
131139
const query: firestore.Query = firebase.firestore().collection("cities")
132140
.where("state", "==", "CA")
133-
.where("population", "<", 1000000);
141+
.where("population", "<", 550000);
134142

135143
query
136144
.get()
@@ -142,6 +150,22 @@ export class ItemsComponent implements OnInit {
142150
.catch(err => console.log("Where-get failed, error" + err));
143151
}
144152

153+
public firestoreWhereOrderLimit(): void {
154+
const query: firestore.Query = firebase.firestore().collection("cities")
155+
.where("state", "==", "CA")
156+
.orderBy("population", "desc")
157+
.limit(2);
158+
159+
query
160+
.get()
161+
.then((querySnapshot: firestore.QuerySnapshot) => {
162+
querySnapshot.forEach(doc => {
163+
console.log(`${JSON.stringify(doc.data())}`);
164+
});
165+
})
166+
.catch(err => console.log("firestoreWhereOrderLimit failed, error" + err));
167+
}
168+
145169
public firestoreDelete(): void {
146170
firebase.firestore().collection("dogs").doc("fave")
147171
.delete()

src/firebase.android.ts

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,9 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
21982198
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
21992199
add: document => firebase.firestore.add(collectionPath, document),
22002200
get: () => firebase.firestore.get(collectionPath),
2201-
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value)
2201+
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
2202+
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
2203+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef)
22022204
};
22032205

22042206
} catch (ex) {
@@ -2479,6 +2481,39 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
24792481
});
24802482
};
24812483

2484+
firebase.firestore._getQuery = (collectionPath: string, query: com.google.firebase.firestore.Query): firestore.Query => {
2485+
return {
2486+
get: () => new Promise((resolve, reject) => {
2487+
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
2488+
onComplete: task => {
2489+
if (!task.isSuccessful()) {
2490+
const ex = task.getException();
2491+
reject(ex && ex.getReason ? ex.getReason() : ex);
2492+
} else {
2493+
const result: com.google.firebase.firestore.QuerySnapshot = task.getResult();
2494+
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
2495+
for (let i = 0; i < result.size(); i++) {
2496+
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
2497+
docSnapshots.push({
2498+
id: documentSnapshot.getId(),
2499+
exists: true,
2500+
data: () => firebase.toJsObject(documentSnapshot.getData())
2501+
});
2502+
}
2503+
const snap = new QuerySnapshot();
2504+
snap.docSnapshots = docSnapshots;
2505+
resolve(snap);
2506+
}
2507+
}
2508+
});
2509+
query.get().addOnCompleteListener(onCompleteListener);
2510+
}),
2511+
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query),
2512+
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
2513+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query)
2514+
};
2515+
};
2516+
24822517
firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any, query?: com.google.firebase.firestore.Query): firestore.Query => {
24832518
try {
24842519
if (typeof(com.google.firebase.firestore) === "undefined") {
@@ -2504,39 +2539,22 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
25042539
return null;
25052540
}
25062541

2507-
return {
2508-
get: () => new Promise((resolve, reject) => {
2509-
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
2510-
onComplete: task => {
2511-
if (!task.isSuccessful()) {
2512-
const ex = task.getException();
2513-
reject(ex && ex.getReason ? ex.getReason() : ex);
2514-
} else {
2515-
const result: com.google.firebase.firestore.QuerySnapshot = task.getResult();
2516-
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
2517-
for (let i = 0; i < result.size(); i++) {
2518-
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
2519-
docSnapshots.push({
2520-
id: documentSnapshot.getId(),
2521-
exists: true,
2522-
data: () => firebase.toJsObject(documentSnapshot.getData())
2523-
});
2524-
}
2525-
const snap = new QuerySnapshot();
2526-
snap.docSnapshots = docSnapshots;
2527-
resolve(snap);
2528-
}
2529-
}
2530-
});
2531-
query.get().addOnCompleteListener(onCompleteListener);
2532-
}),
2533-
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query)
2534-
};
2542+
return firebase.firestore._getQuery(collectionPath, query);
25352543

25362544
} catch (ex) {
25372545
console.log("Error in firebase.firestore.where: " + ex);
25382546
return null;
25392547
}
25402548
};
25412549

2550+
firebase.firestore.orderBy = (collectionPath: string, fieldPath: string, direction: firestore.OrderByDirection, query: com.google.firebase.firestore.Query): firestore.Query => {
2551+
query = query.orderBy(fieldPath, direction === "desc" ? com.google.firebase.firestore.Query.Direction.DESCENDING : com.google.firebase.firestore.Query.Direction.ASCENDING);
2552+
return firebase.firestore._getQuery(collectionPath, query);
2553+
};
2554+
2555+
firebase.firestore.limit = (collectionPath: string, limit: number, query: com.google.firebase.firestore.Query): firestore.Query => {
2556+
query = query.limit(limit);
2557+
return firebase.firestore._getQuery(collectionPath, query);
2558+
};
2559+
25422560
module.exports = firebase;

src/firebase.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ export namespace dynamicLinks {
787787
export namespace firestore {
788788
export type DocumentData = { [field: string]: any };
789789
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>';
790+
export type OrderByDirection = 'desc' | 'asc';
790791

791792
export interface SetOptions {
792793
merge?: boolean;
@@ -812,6 +813,10 @@ export namespace firestore {
812813
get(): Promise<QuerySnapshot>;
813814

814815
where(fieldPath: string, opStr: WhereFilterOp, value: any): Query;
816+
817+
orderBy(fieldPath: string, directionStr: firestore.OrderByDirection): Query;
818+
819+
limit(limit: number): Query;
815820
}
816821

817822
export interface CollectionReference extends Query {

src/firebase.ios.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,7 +2226,9 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22262226
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
22272227
add: document => firebase.firestore.add(collectionPath, document),
22282228
get: () => firebase.firestore.get(collectionPath),
2229-
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value)
2229+
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
2230+
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, fIRCollectionReference),
2231+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, fIRCollectionReference)
22302232
};
22312233

22322234
} catch (ex) {
@@ -2456,6 +2458,35 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
24562458
});
24572459
};
24582460

2461+
firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firestore.Query => {
2462+
return {
2463+
get: () => new Promise((resolve, reject) => {
2464+
query.getDocumentsWithCompletion((snapshot: FIRQuerySnapshot, error: NSError) => {
2465+
if (error) {
2466+
reject(error.localizedDescription);
2467+
} else {
2468+
console.log(">> .where, snapshot: " + snapshot);
2469+
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
2470+
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
2471+
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2472+
docSnapshots.push({
2473+
id: document.documentID,
2474+
exists: true,
2475+
data: () => firebase.toJsObject(document.data())
2476+
});
2477+
}
2478+
const snap = new QuerySnapshot();
2479+
snap.docSnapshots = docSnapshots;
2480+
resolve(snap);
2481+
}
2482+
});
2483+
}),
2484+
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
2485+
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
2486+
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query)
2487+
};
2488+
};
2489+
24592490
firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any, query?: FIRQuery): firestore.Query => {
24602491
try {
24612492
if (typeof(FIRFirestore) === "undefined") {
@@ -2480,37 +2511,24 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
24802511
return null;
24812512
}
24822513

2483-
return {
2484-
get: () => new Promise((resolve, reject) => {
2485-
query.getDocumentsWithCompletion((snapshot: FIRQuerySnapshot, error: NSError) => {
2486-
if (error) {
2487-
reject(error.localizedDescription);
2488-
} else {
2489-
console.log(">> .where, snapshot: " + snapshot);
2490-
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
2491-
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
2492-
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2493-
docSnapshots.push({
2494-
id: document.documentID,
2495-
exists: true,
2496-
data: () => firebase.toJsObject(document.data())
2497-
});
2498-
}
2499-
const snap = new QuerySnapshot();
2500-
snap.docSnapshots = docSnapshots;
2501-
resolve(snap);
2502-
}
2503-
});
2504-
}),
2505-
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query)
2506-
};
2514+
return firebase.firestore._getQuery(collectionPath, query);
25072515

25082516
} catch (ex) {
25092517
console.log("Error in firebase.firestore.where: " + ex);
25102518
return null;
25112519
}
25122520
};
25132521

2522+
firebase.firestore.orderBy = (collectionPath: string, fieldPath: string, direction: firestore.OrderByDirection, query: FIRQuery): firestore.Query => {
2523+
query = query.queryOrderedByFieldDescending(fieldPath, direction === "desc");
2524+
return firebase.firestore._getQuery(collectionPath, query);
2525+
};
2526+
2527+
firebase.firestore.limit = (collectionPath: string, limit: number, query: FIRQuery): firestore.Query => {
2528+
query = query.queryLimitedTo(limit);
2529+
return firebase.firestore._getQuery(collectionPath, query);
2530+
};
2531+
25142532
// see https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate?language=objc
25152533
class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNotificationCenterDelegate {
25162534
public static ObjCProtocols = [];

0 commit comments

Comments
 (0)