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 (where.where)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 27, 2017
1 parent 68a7887 commit 8406397
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
1 change: 1 addition & 0 deletions demo-ng/app/item/items.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<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>
9 changes: 6 additions & 3 deletions demo-ng/app/item/items.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class ItemsComponent implements OnInit {

citiesRef.doc("DC").set({
name: "Washington, D.C.",
state: null,
state: "WA",
country: "USA",
capital: true,
population: 680000
Expand Down Expand Up @@ -114,7 +114,7 @@ export class ItemsComponent implements OnInit {
.catch(err => console.log("Get failed, error" + err));

// examples from https://firebase.google.com/docs/firestore/query-data/get-data
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("BJ");

docRef.get().then((doc: firestore.DocumentSnapshot) => {
if (doc.exists) {
Expand All @@ -125,9 +125,12 @@ export class ItemsComponent implements OnInit {
}).catch(function (error) {
console.log("Error getting document:", error);
});
}

public firestoreWhere(): void {
const query: firestore.Query = firebase.firestore().collection("cities")
.where("capital", "==", true);
.where("state", "==", "CA")
.where("population", "<", 1000000);

query
.get()
Expand Down
22 changes: 8 additions & 14 deletions src/firebase.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2479,28 +2479,26 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
});
};

firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any): firestore.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") {
console.log("Make sure firebase-firestore is in the plugin's include.gradle");
return null;
}

const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
const colRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath);

let query: com.google.firebase.firestore.Query;
query = query || db.collection(collectionPath);

if (opStr === "<") {
query = colRef.whereLessThan(fieldPath, firebase.toValue(value));
query = query.whereLessThan(fieldPath, firebase.toValue(value));
} else if (opStr === "<=") {
query = colRef.whereLessThanOrEqualTo(fieldPath, firebase.toValue(value));
query = query.whereLessThanOrEqualTo(fieldPath, firebase.toValue(value));
} else if (opStr === "==") {
query = colRef.whereEqualTo(fieldPath, firebase.toValue(value));
query = query.whereEqualTo(fieldPath, firebase.toValue(value));
} else if (opStr === ">=") {
query = colRef.whereGreaterThanOrEqualTo(fieldPath, firebase.toValue(value));
query = query.whereGreaterThanOrEqualTo(fieldPath, firebase.toValue(value));
} else if (opStr === ">") {
query = colRef.whereGreaterThan(fieldPath, firebase.toValue(value));
query = query.whereGreaterThan(fieldPath, firebase.toValue(value));
} else {
console.log("Illegal argument for opStr: " + opStr);
return null;
Expand Down Expand Up @@ -2532,11 +2530,7 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
});
query.get().addOnCompleteListener(onCompleteListener);
}),
where: () => {
// TODO check web impl
console.log("in where....");
return this;
}
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query)
};

} catch (ex) {
Expand Down
24 changes: 11 additions & 13 deletions src/firebase.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ firebase.toJsObject = objCObj => {
case 'NSDecimalNumber':
node[key] = Number(String(val));
break;
case 'Date':
node[key] = new Date(val);
break;
default:
console.log("Please report this at https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues: iOS toJsObject is missing a converter for class '" + types.getClass(val) + "'. Casting to String as a fallback.");
node[key] = String(val);
Expand Down Expand Up @@ -2453,26 +2456,25 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
});
};

firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any): firestore.Query => {
firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: firestore.WhereFilterOp, value: any, query?: FIRQuery): firestore.Query => {
try {
if (typeof(FIRFirestore) === "undefined") {
console.log("Make sure 'Firebase/Firestore' is in the plugin's Podfile");
return null;
}

const colRef: FIRCollectionReference = FIRFirestore.firestore().collectionWithPath(collectionPath);
let query: FIRQuery;
query = query || FIRFirestore.firestore().collectionWithPath(collectionPath);

if (opStr === "<") {
query = colRef.queryWhereFieldIsLessThan(fieldPath, value);
query = query.queryWhereFieldIsLessThan(fieldPath, value);
} else if (opStr === "<=") {
query = colRef.queryWhereFieldIsLessThanOrEqualTo(fieldPath, value);
query = query.queryWhereFieldIsLessThanOrEqualTo(fieldPath, value);
} else if (opStr === "==") {
query = colRef.queryWhereFieldIsEqualTo(fieldPath, value);
query = query.queryWhereFieldIsEqualTo(fieldPath, value);
} else if (opStr === ">=") {
query = colRef.queryWhereFieldIsGreaterThanOrEqualTo(fieldPath, value);
query = query.queryWhereFieldIsGreaterThanOrEqualTo(fieldPath, value);
} else if (opStr === ">") {
query = colRef.queryWhereFieldIsGreaterThan(fieldPath, value);
query = query.queryWhereFieldIsGreaterThan(fieldPath, value);
} else {
console.log("Illegal argument for opStr: " + opStr);
return null;
Expand Down Expand Up @@ -2500,11 +2502,7 @@ firebase.firestore.where = (collectionPath: string, fieldPath: string, opStr: fi
}
});
}),
where: () => {
// TODO check web impl
console.log("in where....");
return this;
}
where: (fp: string, os: firestore.WhereFilterOp, v: any) => firebase.firestore.where(collectionPath, fp, os, v, query)
};

} catch (ex) {
Expand Down

0 comments on commit 8406397

Please sign in to comment.