This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
items.component.ts
276 lines (237 loc) · 8.2 KB
/
items.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { Component, NgZone, OnInit } from "@angular/core";
import { firestore } from "nativescript-plugin-firebase";
import { Observable } from "rxjs/Observable";
import { City } from "../model/City";
const firebase = require("nativescript-plugin-firebase/app");
const firebaseWebApi = require("nativescript-plugin-firebase/app");
// import { AngularFireModule } from 'angularfire2';
@Component({
selector: "ns-items",
moduleId: module.id,
templateUrl: "./items.component.html",
})
export class ItemsComponent implements OnInit {
private listenerUnsubscribe: () => void;
public myCity$: Observable<City>;
public myCities$: Observable<Array<City>>;
private city: City;
private cities: Array<City> = [];
constructor(private zone: NgZone) {
// AngularFireModule.initializeApp({});
}
ngOnInit(): void {
firebase.initializeApp({
persist: false
}).then(() => {
console.log("Firebase initialized");
});
}
public loginAnonymously(): void {
firebase.auth().signInAnonymously()
.then(() => console.log("Logged in"))
.catch(err => console.log("Login error: " + JSON.stringify(err)));
}
public firestoreAdd(): void {
firebase.firestore().collection("dogs").add({name: "Fido"})
.then((docRef: firestore.DocumentReference) => {
console.log("Fido added, ref: " + docRef.id);
})
.catch(err => console.log("Adding Fido failed, error: " + err));
}
public firestoreSet(): void {
firebase.firestore().collection("dogs").doc("fave")
.set({name: "Woofie", last: "lastofwoofie", date: new Date()}, {merge: true})
.then(() => {
console.log("Woofie set");
})
.catch(err => console.log("Setting Woofie failed, error: " + err));
// example from https://firebase.google.com/docs/firestore/query-data/get-data
const citiesCollection = firebase.firestore().collection("cities");
citiesCollection.doc("SF").set({
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860000
});
citiesCollection.doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA",
capital: false,
population: 3900000
});
citiesCollection.doc("SAC").set({
name: "Sacramento",
state: "CA",
country: "USA",
capital: true,
population: 500000
});
citiesCollection.doc("DC").set({
name: "Washington, D.C.",
state: "WA",
country: "USA",
capital: true,
population: 680000
});
citiesCollection.doc("TOK").set({
name: "Tokyo",
state: null,
country: "Japan",
capital: true,
population: 9000000
});
citiesCollection.doc("BJ").set({
name: "Beijing",
state: null,
country: "China",
capital: true,
population: 21500000
});
}
public firestoreSetByAutoID(): void {
firebase.firestore().collection("dogs").doc()
.set({name: "Woofie", last: "lastofwoofie", date: new Date()})
.then(() => {
console.log("Woofie set");
})
.catch(err => console.log("Setting Woofie failed, error: " + err));
}
public firestoreUpdate(): void {
firebase.firestore().collection("dogs").doc("fave")
.update({name: "Woofieupdate", last: "updatedwoofie"})
.then(() => {
console.log("Woofie updated");
})
.catch(err => console.log("Updating Woofie failed, error: " + JSON.stringify(err)));
}
public firestoreGet(): void {
const collectionRef: firestore.CollectionReference = firebase.firestore().collection("dogs");
collectionRef.get()
.then((querySnapshot: firestore.QuerySnapshot) => {
querySnapshot.forEach(doc => {
console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
});
})
.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("BJ");
docRef.get().then((doc: firestore.DocumentSnapshot) => {
if (doc.exists) {
console.log("Document data:", JSON.stringify(doc.data()));
} else {
console.log("No such document!");
}
}).catch(function (error) {
console.log("Error getting document:", error);
});
}
public firestoreGetNested(): void {
const mainStreetInSFDocRef: firestore.DocumentReference =
firebase.firestore()
.collection("cities")
.doc("SF")
.collection("streets")
.doc("QZNrg22tkN8W71YC3qCb"); // id of 'main st.'
// .doc("doesntexist");
mainStreetInSFDocRef.get().then((doc: firestore.DocumentSnapshot) => {
if (doc.exists) {
console.log("Document data:", JSON.stringify(doc.data()));
} else {
console.log("No such document!");
}
}).catch(function (error) {
console.log("Error getting document:", error);
});
}
firestoreDocumentObservable(): void {
this.myCity$ = Observable.create(subscriber => {
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
docRef.onSnapshot((doc: firestore.DocumentSnapshot) => {
this.zone.run(() => {
this.city = <City>doc.data();
subscriber.next(this.city);
});
});
});
}
firestoreCollectionObservable(): void {
this.myCities$ = Observable.create(subscriber => {
const colRef: firestore.CollectionReference = firebase.firestore().collection("cities");
colRef.onSnapshot((snapshot: firestore.QuerySnapshot) => {
this.zone.run(() => {
this.cities = [];
snapshot.forEach(docSnap => this.cities.push(<City>docSnap.data()));
subscriber.next(this.cities);
});
});
});
}
public firestoreListen(): void {
if (this.listenerUnsubscribe !== undefined) {
console.log("Already listening ;)");
return;
}
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
this.listenerUnsubscribe = docRef.onSnapshot((doc: firestore.DocumentSnapshot) => {
if (doc.exists) {
console.log("Document data:", JSON.stringify(doc.data()));
} else {
console.log("No such document!");
}
});
}
public firestoreStopListening(): void {
if (this.listenerUnsubscribe === undefined) {
console.log("Please start listening first ;)");
return;
}
this.listenerUnsubscribe();
this.listenerUnsubscribe = undefined;
}
public firestoreWhere(): void {
const query: firestore.Query = firebase.firestore().collection("cities")
.where("state", "==", "CA")
.where("population", "<", 550000);
query
.get()
.then((querySnapshot: firestore.QuerySnapshot) => {
querySnapshot.forEach(doc => {
console.log(`Relatively small Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
});
})
.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(`Large Californian city: ${doc.id} => ${JSON.stringify(doc.data())}`);
});
})
.catch(err => console.log("firestoreWhereOrderLimit failed, error" + err));
}
public firestoreDelete(): void {
firebase.firestore().collection("dogs").doc("fave")
.delete()
.then(() => {
console.log("Woofie deleted");
})
.catch(err => console.log("Delete failed, error" + err));
}
public doWebGetValueForCompanies(): void {
const path = "/companies";
firebaseWebApi.database().ref(path)
.once("value")
.then(result => {
console.log(`${result.key} => ${JSON.stringify(result.val())}`);
})
.catch(error => console.log("doWebGetValueForCompanies error: " + error));
}
}