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

Commit

Permalink
Firestore anytime soon? #507 (orderBy, limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 27, 2017
1 parent 8406397 commit 5825dc2
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 68 deletions.
28 changes: 15 additions & 13 deletions demo-ng/app/item/items.component.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<ActionBar title="My App" class="action-bar">
</ActionBar>

<StackLayout class="page">
<Label text="Authentication" class="h2"></Label>
<Button text="login anonymously" (tap)="loginAnonymously()" class="button button-user"></Button>
<Label text="Firestore" class="h2"></Label>
<Button text="Add" (tap)="firestoreAdd()" class="button"></Button>
<Button text="Set" (tap)="firestoreSet()" class="button"></Button>
<Button text="Set (auto id)" (tap)="firestoreSetByAutoID()" class="button"></Button>
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>

</StackLayout>
<ScrollView>
<StackLayout class="page">
<Label text="Authentication" class="h2"></Label>
<Button text="login anonymously" (tap)="loginAnonymously()" class="button button-user"></Button>
<Label text="Firestore" class="h2"></Label>
<Button text="Add" (tap)="firestoreAdd()" class="button"></Button>
<Button text="Set" (tap)="firestoreSet()" class="button"></Button>
<Button text="Set (auto id)" (tap)="firestoreSetByAutoID()" class="button"></Button>
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
<Button text="Where, Order, Limit" (tap)="firestoreWhereOrderLimit()" class="button"></Button>
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>
</StackLayout>
</ScrollView>
26 changes: 25 additions & 1 deletion demo-ng/app/item/items.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export class ItemsComponent implements OnInit {
population: 3900000
});

citiesRef.doc("SAC").set({
name: "Sacramento",
state: "CA",
country: "USA",
capital: true,
population: 500000
});

citiesRef.doc("DC").set({
name: "Washington, D.C.",
state: "WA",
Expand Down Expand Up @@ -130,7 +138,7 @@ export class ItemsComponent implements OnInit {
public firestoreWhere(): void {
const query: firestore.Query = firebase.firestore().collection("cities")
.where("state", "==", "CA")
.where("population", "<", 1000000);
.where("population", "<", 550000);

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

public firestoreWhereOrderLimit(): void {
const query: firestore.Query = firebase.firestore().collection("cities")
.where("state", "==", "CA")
.orderBy("population", "desc")
.limit(2);

query
.get()
.then((querySnapshot: firestore.QuerySnapshot) => {
querySnapshot.forEach(doc => {
console.log(`${JSON.stringify(doc.data())}`);
});
})
.catch(err => console.log("firestoreWhereOrderLimit failed, error" + err));
}

public firestoreDelete(): void {
firebase.firestore().collection("dogs").doc("fave")
.delete()
Expand Down
76 changes: 47 additions & 29 deletions src/firebase.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,9 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
add: document => firebase.firestore.add(collectionPath, document),
get: () => firebase.firestore.get(collectionPath),
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value)
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef)
};

} catch (ex) {
Expand Down Expand Up @@ -2479,6 +2481,39 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
});
};

firebase.firestore._getQuery = (collectionPath: string, query: com.google.firebase.firestore.Query): firestore.Query => {
return {
get: () => new Promise((resolve, reject) => {
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
onComplete: task => {
if (!task.isSuccessful()) {
const ex = task.getException();
reject(ex && ex.getReason ? ex.getReason() : ex);
} else {
const result: com.google.firebase.firestore.QuerySnapshot = task.getResult();
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
for (let i = 0; i < result.size(); i++) {
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
docSnapshots.push({
id: documentSnapshot.getId(),
exists: true,
data: () => firebase.toJsObject(documentSnapshot.getData())
});
}
const snap = new QuerySnapshot();
snap.docSnapshots = docSnapshots;
resolve(snap);
}
}
});
query.get().addOnCompleteListener(onCompleteListener);
}),
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query),
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query)
};
};

firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any, query?: com.google.firebase.firestore.Query): firestore.Query => {
try {
if (typeof(com.google.firebase.firestore) === "undefined") {
Expand All @@ -2504,39 +2539,22 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
return null;
}

return {
get: () => new Promise((resolve, reject) => {
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
onComplete: task => {
if (!task.isSuccessful()) {
const ex = task.getException();
reject(ex && ex.getReason ? ex.getReason() : ex);
} else {
const result: com.google.firebase.firestore.QuerySnapshot = task.getResult();
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
for (let i = 0; i < result.size(); i++) {
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
docSnapshots.push({
id: documentSnapshot.getId(),
exists: true,
data: () => firebase.toJsObject(documentSnapshot.getData())
});
}
const snap = new QuerySnapshot();
snap.docSnapshots = docSnapshots;
resolve(snap);
}
}
});
query.get().addOnCompleteListener(onCompleteListener);
}),
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query)
};
return firebase.firestore._getQuery(collectionPath, query);

} catch (ex) {
console.log("Error in firebase.firestore.where: " + ex);
return null;
}
};

firebase.firestore.orderBy = (collectionPath: string, fieldPath: string, direction: firestore.OrderByDirection, query: com.google.firebase.firestore.Query): firestore.Query => {
query = query.orderBy(fieldPath, direction === "desc" ? com.google.firebase.firestore.Query.Direction.DESCENDING : com.google.firebase.firestore.Query.Direction.ASCENDING);
return firebase.firestore._getQuery(collectionPath, query);
};

firebase.firestore.limit = (collectionPath: string, limit: number, query: com.google.firebase.firestore.Query): firestore.Query => {
query = query.limit(limit);
return firebase.firestore._getQuery(collectionPath, query);
};

module.exports = firebase;
5 changes: 5 additions & 0 deletions src/firebase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ export namespace dynamicLinks {
export namespace firestore {
export type DocumentData = { [field: string]: any };
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>';
export type OrderByDirection = 'desc' | 'asc';

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

where(fieldPath: string, opStr: WhereFilterOp, value: any): Query;

orderBy(fieldPath: string, directionStr: firestore.OrderByDirection): Query;

limit(limit: number): Query;
}

export interface CollectionReference extends Query {
Expand Down
68 changes: 43 additions & 25 deletions src/firebase.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,9 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
doc: (documentPath?: string) => firebase.firestore.doc(collectionPath, documentPath),
add: document => firebase.firestore.add(collectionPath, document),
get: () => firebase.firestore.get(collectionPath),
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value)
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, fIRCollectionReference),
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, fIRCollectionReference)
};

} catch (ex) {
Expand Down Expand Up @@ -2456,6 +2458,35 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
});
};

firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firestore.Query => {
return {
get: () => new Promise((resolve, reject) => {
query.getDocumentsWithCompletion((snapshot: FIRQuerySnapshot, error: NSError) => {
if (error) {
reject(error.localizedDescription);
} else {
console.log(">> .where, snapshot: " + snapshot);
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
docSnapshots.push({
id: document.documentID,
exists: true,
data: () => firebase.toJsObject(document.data())
});
}
const snap = new QuerySnapshot();
snap.docSnapshots = docSnapshots;
resolve(snap);
}
});
}),
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query)
};
};

firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any, query?: FIRQuery): firestore.Query => {
try {
if (typeof(FIRFirestore) === "undefined") {
Expand All @@ -2480,37 +2511,24 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
return null;
}

return {
get: () => new Promise((resolve, reject) => {
query.getDocumentsWithCompletion((snapshot: FIRQuerySnapshot, error: NSError) => {
if (error) {
reject(error.localizedDescription);
} else {
console.log(">> .where, snapshot: " + snapshot);
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
docSnapshots.push({
id: document.documentID,
exists: true,
data: () => firebase.toJsObject(document.data())
});
}
const snap = new QuerySnapshot();
snap.docSnapshots = docSnapshots;
resolve(snap);
}
});
}),
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query)
};
return firebase.firestore._getQuery(collectionPath, query);

} catch (ex) {
console.log("Error in firebase.firestore.where: " + ex);
return null;
}
};

firebase.firestore.orderBy = (collectionPath: string, fieldPath: string, direction: firestore.OrderByDirection, query: FIRQuery): firestore.Query => {
query = query.queryOrderedByFieldDescending(fieldPath, direction === "desc");
return firebase.firestore._getQuery(collectionPath, query);
};

firebase.firestore.limit = (collectionPath: string, limit: number, query: FIRQuery): firestore.Query => {
query = query.queryLimitedTo(limit);
return firebase.firestore._getQuery(collectionPath, query);
};

// see https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate?language=objc
class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNotificationCenterDelegate {
public static ObjCProtocols = [];
Expand Down

0 comments on commit 5825dc2

Please sign in to comment.