-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataaccess.ts
243 lines (216 loc) · 6.91 KB
/
dataaccess.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
import * as firebase from 'firebase';
const CONS = {
path: {
users: 'users',
categories: 'categories',
article: 'article'
}
};
/*
https://firebase.google.com/docs/database/web/structure-data
Avoid nesting data:
Because the Firebase Realtime Database allows nesting data up to 32 levels deep,
you might be tempted to think that this should be the default structure.
However, when you fetch data at a location in your database, you also retrieve all of its child nodes.
In addition, when you grant someone read or write access at a node in your database,
you also grant them access to all data under that node.
Therefore, in practice, it's best to keep your data structure as flat as possible.
****** Example of Flatten data structures ******
{
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
},
"two": { ... },
"three": { ... }
},
// Conversation members are easily accessible
// and stored by chat conversation ID
"members": {
// we'll talk about indices like this below
"one": {
"ghopper": true,
"alovelace": true,
"eclarke": true
},
"two": { ... },
"three": { ... }
},
// Messages are separate from data we may want to iterate quickly
// but still easily paginated and queried, and organized by chat
// conversation ID
"messages": {
"one": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": { ... },
"m3": { ... }
},
"two": { ... },
"three": { ... }
}
}
*/
export class DataAccess {
private _userInfo: any;
//Firebase User
createUser(email: string, password: string) {
return firebase.auth().createUserWithEmailAndPassword(email, password).then((result: any) => {return result;});
}
login(provider: string = 'email', email: string = '', password: string = '') {
let _provider: any = null
switch (provider) {
case "google":
_provider = new firebase.auth.GoogleAuthProvider();
break;
case "facebook":
_provider = new firebase.auth.FacebookAuthProvider();
break;
}
if (_provider) {
return firebase.auth().signInWithPopup(_provider).then((result: any) => {
return result;
});
} else {
return firebase.auth().signInWithEmailAndPassword(email, password).then((result: any) => {return result;});
}
}
checkLoggedIn() {
return new Promise(resolve => {
let unsubscribe = firebase.auth().onAuthStateChanged(function(user: any) {
if (user) {
this._userInfo = user;
resolve(user);
} else {
resolve(null);
}
unsubscribe();
});
});
}
getUserInfo() {
if (!this._userInfo) {
this._userInfo = firebase.auth().currentUser;
}
return this._userInfo;
}
isLoggedIn() {
return this.getUserInfo();
}
getUserId() {
let user = this.getUserInfo();
if (user) {
return user.uid;
}
return null;
}
logout() {
return firebase.auth().signOut().then((result:any) => {return result;});
}
sendPasswordResetEmail(email: string) {
return firebase.auth().sendPasswordResetEmail(email).then((result:any) => {return result;});
}
getDataOnce(path: string) {
let ref = firebase.database().ref(path);
return ref.once('value').then((snapshot: any) => {return snapshot;});
}
getDataOnceWithArrayAndIndex(path: string) {
return firebase.database().ref(path).once('value').then((snapshot: any) => {return this.snapshotToArrayAndIndex(snapshot);});
}
getPath(pathName: string) {
let path: any = CONS.path;
return path[pathName];
}
getData(path: string) {
return firebase.database().ref(path);
}
setData(path: string, value: any) {
return this.getData(path).set(value);
}
/*
The push() method generates a unique key every time a new child is added to the specified Firebase reference.
By using these auto-generated keys for each new element in the list,
several clients can add children to the same location at the same time without write conflicts.
The unique key generated by push() is based on a timestamp,
so list items are automatically ordered chronologically.
*/
addData(path: string, value: any) {
return this.getData(path).push(value);
}
/*
function writeNewPost(uid, username, picture, title, body) {
// A post entry.
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
*/
updateData(path: string, value: any) {
return this.getData(path).update(value);
}
removeData(path: string) {
return this.getData(path).remove();
}
//TODO: Firebase doesn't provide an array object?
snapshotToArray(s: any) {
let item: any;
let items: Array<any> = [];
s.forEach((sc:any) => {
item = sc.val();
item.id = sc.key;
items.push(item);
});
return items;
}
snapshotToArrayAndIndex(s: any) {
let item: any;
let items: Array<any> = [];
let idxes: any = {};
let idx: number = 0;
s.forEach((sc:any) => {
item = sc.val();
item.id = sc.key;
item.__idx__ = idx;
idxes[item.id] = idx;
items.push(item);
});
return {items:items,indexes:idxes};
}
buildIndex(items: any, idField: string = 'id') {
let idxes: any = {};
let idx: any;
let item: any;
let id: any;
let selectedIds: any = {};
for (idx in items) {
item = items[idx];
id = item[idField];
item.__idx__ = idx;
idxes[id] = idx;
if (item.selected) {
selectedIds[id] = true;
}
items[idx] = item;
}
return {items:items,indexes:idxes,selectedIds:selectedIds};
}
}