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

Commit 398e493

Browse files
Firestore anytime soon? #507
1 parent 2a3f320 commit 398e493

File tree

6 files changed

+174
-12
lines changed

6 files changed

+174
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## 5.0.0 (work in progress)
88

99
### New
10+
- [#507](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/507) Firestore anytime soon?
1011
- [#547](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/547) Add a 'getValue' function to mimic the Web API's 'once'
1112
- [#548](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/548) Compatibility with the Firebase Web API
1213
- [#550](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/550) Add an Angular demo app

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,50 @@ export class ItemsComponent implements OnInit {
3939
console.log("Woofie set");
4040
})
4141
.catch(err => console.log("Setting Woofie failed, error: " + err));
42+
43+
44+
// example from https://firebase.google.com/docs/firestore/query-data/get-data
45+
const citiesRef = firebase.firestore().collection("cities");
46+
47+
citiesRef.doc("SF").set({
48+
name: "San Francisco",
49+
state: "CA",
50+
country: "USA",
51+
capital: false,
52+
population: 860000
53+
});
54+
55+
citiesRef.doc("LA").set({
56+
name: "Los Angeles",
57+
state: "CA",
58+
country: "USA",
59+
capital: false,
60+
population: 3900000
61+
});
62+
63+
citiesRef.doc("DC").set({
64+
name: "Washington, D.C.",
65+
state: null,
66+
country: "USA",
67+
capital: true,
68+
population: 680000
69+
});
70+
71+
citiesRef.doc("TOK").set({
72+
name: "Tokyo",
73+
state: null,
74+
country: "Japan",
75+
capital: true,
76+
population: 9000000
77+
});
78+
79+
citiesRef.doc("BJ").set({
80+
name: "Beijing",
81+
state: null,
82+
country: "China",
83+
capital: true,
84+
population: 21500000
85+
});
4286
}
4387

4488
public firestoreSetByAutoID(): void {
@@ -68,6 +112,19 @@ export class ItemsComponent implements OnInit {
68112
});
69113
})
70114
.catch(err => console.log("Get failed, error" + err));
115+
116+
// example from https://firebase.google.com/docs/firestore/query-data/get-data
117+
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
118+
119+
docRef.get().then((doc: firestore.DocumentSnapshot) => {
120+
if (doc.exists) {
121+
console.log("Document data:", JSON.stringify(doc.data()));
122+
} else {
123+
console.log("No such document!");
124+
}
125+
}).catch(function (error) {
126+
console.log("Error getting document:", error);
127+
});
71128
}
72129

73130
public firestoreDelete(): void {

src/firebase-common.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ export const firebase: any = {
187187
}
188188
};
189189

190+
export class DocumentSnapshot implements firestore.DocumentSnapshot {
191+
public id: string;
192+
public exists: boolean;
193+
public docSnapshot: firestore.DocumentSnapshot;
194+
195+
data(): firestore.DocumentData {
196+
return this.docSnapshot;
197+
}
198+
}
199+
190200
export class QuerySnapshot implements firestore.QuerySnapshot {
191201
public docSnapshots: firestore.DocumentSnapshot[];
192202

src/firebase.android.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22222222
id: docRef.getId(),
22232223
collection: cp => firebase.firestore.collection(cp),
22242224
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2225+
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
22252226
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
22262227
delete: () => firebase.firestore.delete(collectionPath, docRef.getId())
22272228
};
@@ -2249,6 +2250,7 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
22492250
id: documentReference.getId(),
22502251
collection: cp => firebase.firestore.collection(cp),
22512252
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, documentReference.getId(), data, options),
2253+
get: () => firebase.firestore.getDocument(collectionPath, documentReference.getId()),
22522254
update: (data: any) => firebase.firestore.update(collectionPath, documentReference.getId(), data),
22532255
delete: () => firebase.firestore.delete(collectionPath, documentReference.getId())
22542256
});
@@ -2374,7 +2376,7 @@ firebase.firestore.delete = (collectionPath: string, documentPath: string): Prom
23742376
});
23752377
};
23762378

2377-
firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapshot> => {
2379+
firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.QuerySnapshot> => {
23782380
return new Promise((resolve, reject) => {
23792381
try {
23802382

@@ -2397,6 +2399,7 @@ firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapsh
23972399
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
23982400
docSnapshots.push({
23992401
id: documentSnapshot.getId(),
2402+
exists: true,
24002403
data: () => firebase.toJsObject(documentSnapshot.getData())
24012404
});
24022405
}
@@ -2419,7 +2422,57 @@ firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapsh
24192422
.addOnFailureListener(onFailureListener);
24202423

24212424
} catch (ex) {
2422-
console.log("Error in firebase.firestore.add: " + ex);
2425+
console.log("Error in firebase.firestore.getCollection: " + ex);
2426+
reject(ex);
2427+
}
2428+
});
2429+
};
2430+
2431+
firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapshot> => {
2432+
return firebase.firestore.getCollection(collectionPath);
2433+
};
2434+
2435+
firebase.firestore.getDocument = (collectionPath: string, documentPath: string): Promise<firestore.DocumentSnapshot> => {
2436+
return new Promise((resolve, reject) => {
2437+
try {
2438+
2439+
if (typeof(com.google.firebase.firestore) === "undefined") {
2440+
reject("Make sure firebase-firestore is in the plugin's include.gradle");
2441+
return;
2442+
}
2443+
2444+
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
2445+
2446+
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
2447+
onComplete: task => {
2448+
if (!task.isSuccessful()) {
2449+
const ex = task.getException();
2450+
reject(ex && ex.getReason ? ex.getReason() : ex);
2451+
} else {
2452+
const result: com.google.firebase.firestore.DocumentSnapshot = task.getResult();
2453+
resolve({
2454+
id: result ? result.getId() : null,
2455+
exists: !!result,
2456+
data: () => result ? firebase.toJsObject(result.getData()) : null
2457+
});
2458+
}
2459+
}
2460+
});
2461+
2462+
const onFailureListener = new com.google.android.gms.tasks.OnFailureListener({
2463+
onFailure: exception => {
2464+
reject(exception.getMessage());
2465+
}
2466+
});
2467+
2468+
db.collection(collectionPath)
2469+
.document(documentPath)
2470+
.get()
2471+
.addOnCompleteListener(onCompleteListener)
2472+
.addOnFailureListener(onFailureListener);
2473+
2474+
} catch (ex) {
2475+
console.log("Error in firebase.firestore.getDocument: " + ex);
24232476
reject(ex);
24242477
}
24252478
});

src/firebase.d.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -791,20 +791,22 @@ export namespace firestore {
791791
merge?: boolean;
792792
}
793793

794+
export interface DocumentSnapshot {
795+
id: string;
796+
exists: boolean;
797+
798+
data(): DocumentData;
799+
}
800+
794801
export interface DocumentReference {
795802
id: string;
796803
collection: (collectionPath: string) => CollectionReference;
797804
set: (document: any, options?: SetOptions) => Promise<void>;
805+
get: () => Promise<DocumentSnapshot>;
798806
update: (document: any) => Promise<void>;
799807
delete: () => Promise<void>;
800808
}
801809

802-
export interface DocumentSnapshot {
803-
id: string;
804-
805-
data(): DocumentData;
806-
}
807-
808810
export interface Query {
809811
get(): Promise<QuerySnapshot>;
810812
}
@@ -831,7 +833,9 @@ export namespace firestore {
831833

832834
function set(collectionPath: string, documentPath: string, document: any, options?: any): Promise<void>;
833835

834-
function get(collectionPath: string): Promise<QuerySnapshot>;
836+
function getCollection(collectionPath: string): Promise<QuerySnapshot>;
837+
838+
function getDocument(collectionPath: string, documentPath: string): Promise<DocumentSnapshot>;
835839

836840
function update(collectionPath: string, documentPath: string, document: any): Promise<void>;
837841
}

src/firebase.ios.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { firebase, QuerySnapshot } from "./firebase-common";
1+
import { firebase, DocumentSnapshot, QuerySnapshot } from "./firebase-common";
22
import * as application from "tns-core-modules/application";
33
import * as applicationSettings from "tns-core-modules/application-settings";
44
import * as utils from "tns-core-modules/utils/utils";
@@ -2239,6 +2239,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22392239
id: fIRDocumentReference.documentID,
22402240
collection: cp => firebase.firestore.collection(cp),
22412241
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
2242+
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
22422243
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
22432244
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID)
22442245
};
@@ -2268,6 +2269,7 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
22682269
id: fIRDocumentReference.documentID,
22692270
collection: cp => firebase.firestore.collection(cp),
22702271
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
2272+
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
22712273
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
22722274
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID)
22732275
});
@@ -2373,7 +2375,7 @@ firebase.firestore.delete = (collectionPath: string, documentPath: string): Prom
23732375
});
23742376
};
23752377

2376-
firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapshot> => {
2378+
firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.QuerySnapshot> => {
23772379
return new Promise((resolve, reject) => {
23782380
try {
23792381
if (typeof(FIRFirestore) === "undefined") {
@@ -2393,6 +2395,7 @@ firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapsh
23932395
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
23942396
docSnapshots.push({
23952397
id: document.documentID,
2398+
exists: true,
23962399
data: () => firebase.toJsObject(document.data())
23972400
});
23982401
}
@@ -2403,7 +2406,41 @@ firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapsh
24032406
});
24042407

24052408
} catch (ex) {
2406-
console.log("Error in firebase.firestore.get: " + ex);
2409+
console.log("Error in firebase.firestore.getCollection: " + ex);
2410+
reject(ex);
2411+
}
2412+
});
2413+
};
2414+
2415+
firebase.firestore.get = (collectionPath: string): Promise<firestore.QuerySnapshot> => {
2416+
return firebase.firestore.getCollection(collectionPath);
2417+
};
2418+
2419+
firebase.firestore.getDocument = (collectionPath: string, documentPath: string): Promise<firestore.DocumentSnapshot> => {
2420+
return new Promise((resolve, reject) => {
2421+
try {
2422+
if (typeof(FIRFirestore) === "undefined") {
2423+
reject("Make sure 'Firebase/Firestore' is in the plugin's Podfile");
2424+
return;
2425+
}
2426+
2427+
FIRFirestore.firestore()
2428+
.collectionWithPath(collectionPath)
2429+
.documentWithPath(documentPath)
2430+
.getDocumentWithCompletion((snapshot: FIRDocumentSnapshot, error: NSError) => {
2431+
if (error) {
2432+
reject(error.localizedDescription);
2433+
} else {
2434+
resolve({
2435+
id: snapshot ? snapshot.documentID : null,
2436+
exists: !!snapshot,
2437+
data: () => snapshot ? firebase.toJsObject(snapshot.data()) : null
2438+
});
2439+
}
2440+
});
2441+
2442+
} catch (ex) {
2443+
console.log("Error in firebase.firestore.getDocument: " + ex);
24072444
reject(ex);
24082445
}
24092446
});

0 commit comments

Comments
 (0)