-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathread-and-write.js
189 lines (162 loc) · 4.9 KB
/
read-and-write.js
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
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
function writeUserData_wrapped() {
// [START rtdb_write_new_user]
const { getDatabase, ref, set } = require("firebase/database");
function writeUserData(userId, name, email, imageUrl) {
const db = getDatabase();
set(ref(db, 'users/' + userId), {
username: name,
email: email,
profile_picture : imageUrl
});
}
// [END rtdb_write_new_user]
}
function writeUserDataWithCompletion(userId, name, email, imageUrl) {
// [START rtdb_write_new_user_completion]
const { getDatabase, ref, set } = require("firebase/database");
const db = getDatabase();
set(ref(db, 'users/' + userId), {
username: name,
email: email,
profile_picture : imageUrl
})
.then(() => {
// Data saved successfully!
})
.catch((error) => {
// The write failed...
});
// [END rtdb_write_new_user_completion]
}
function socialListenStarCount() {
const postElement = document.querySelector('#post');
const postId = "1234";
function updateStarCount(a, b) {
// ...
}
// [START rtdb_social_listen_star_count]
const { getDatabase, ref, onValue } = require("firebase/database");
const db = getDatabase();
const starCountRef = ref(db, 'posts/' + postId + '/starCount');
onValue(starCountRef, (snapshot) => {
const data = snapshot.val();
updateStarCount(postElement, data);
});
// [END rtdb_social_listen_star_count]
}
function socialSingleValueRead() {
// [START rtdb_social_single_value_read]
const { getDatabase, ref, onValue } = require("firebase/database");
const { getAuth } = require("firebase/auth");
const db = getDatabase();
const auth = getAuth();
const userId = auth.currentUser.uid;
return onValue(ref(db, '/users/' + userId), (snapshot) => {
const username = (snapshot.val() && snapshot.val().username) || 'Anonymous';
// ...
}, {
onlyOnce: true
});
// [END rtdb_social_single_value_read]
}
function writeNewPost_wrapped() {
// [START rtdb_social_write_fan_out]
const { getDatabase, ref, child, push, update } = require("firebase/database");
function writeNewPost(uid, username, picture, title, body) {
const db = getDatabase();
// A post entry.
const postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
// Get a key for a new Post.
const newPostKey = push(child(ref(db), 'posts')).key;
// Write the new post's data simultaneously in the posts list and the user's post list.
const updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return update(ref(db), updates);
}
// [END rtdb_social_write_fan_out]
}
function socialCompletionCallback() {
const userId = "123";
const email = "test@example.com";
const imageUrl = "https://example.com/image.png";
// [START rtdb_social_completion_callback]
const { getDatabase, ref, set } = require("firebase/database");
const db = getDatabase();
set(ref(db, 'users/' + userId), {
username: name,
email: email,
profile_picture : imageUrl
})
.then(() => {
// Data saved successfully!
})
.catch((error) => {
// The write failed...
});
// [END rtdb_social_completion_callback]
}
function toggleStar_wrapped() {
// [START rtdb_social_star_transaction]
const { getDatabase, ref, runTransaction } = require("firebase/database");
function toggleStar(uid) {
const db = getDatabase();
const postRef = ref(db, '/posts/foo-bar-123');
runTransaction(postRef, (post) => {
if (post) {
if (post.stars && post.stars[uid]) {
post.starCount--;
post.stars[uid] = null;
} else {
post.starCount++;
if (!post.stars) {
post.stars = {};
}
post.stars[uid] = true;
}
}
return post;
});
}
// [END rtdb_social_star_transaction]
}
/**
* @param {string} uid
* @param {string} key
*/
// [START rtdb_social_star_increment]
function addStar(uid, key) {
const { getDatabase, increment, ref, update } = require("firebase/database");
const dbRef = ref(getDatabase());
const updates = {};
updates[`posts/${key}/stars/${uid}`] = true;
updates[`posts/${key}/starCount`] = increment(1);
updates[`user-posts/${key}/stars/${uid}`] = true;
updates[`user-posts/${key}/starCount`] = increment(1);
update(dbRef, updates);
}
// [END rtdb_social_star_increment]
function readOnceWithGet(userId) {
// [START rtdb_read_once_get]
const { getDatabase, ref, child, get } = require("firebase/database");
const dbRef = ref(getDatabase());
get(child(dbRef, `users/${userId}`)).then((snapshot) => {
if (snapshot.exists()) {
console.log(snapshot.val());
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
// [END rtdb_read_once_get]
}