forked from firebase/snippets-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-and-write.js
155 lines (141 loc) · 4.16 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
// These samples are intended for Web so this import would normally be
// done in HTML however using modules here is more convenient for
// ensuring sample correctness offline.
import firebase from "firebase/app";
import "firebase/database";
// [START rtdb_write_new_user]
function writeUserData(userId, name, email, imageUrl) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
});
}
// [END rtdb_write_new_user]
function writeUserDataWithCompletion(userId, name, email, imageUrl) {
// [START rtdb_write_new_user_completion]
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
}, (error) => {
if (error) {
// The write failed...
} else {
// Data saved successfully!
}
});
// [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]
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', (snapshot) => {
const data = snapshot.val();
updateStarCount(postElement, data);
});
// [END rtdb_social_listen_star_count]
}
function socialSingleValueRead() {
// [START rtdb_social_single_value_read]
var userId = firebase.auth().currentUser.uid;
return firebase.database().ref('/users/' + userId).once('value').then((snapshot) => {
var username = (snapshot.val() && snapshot.val().username) || 'Anonymous';
// ...
});
// [END rtdb_social_single_value_read]
}
// [START rtdb_social_write_fan_out]
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);
}
// [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]
firebase.database().ref('users/' + userId).set({
username: name,
email: email,
profile_picture : imageUrl
}, (error) => {
if (error) {
// The write failed...
} else {
// Data saved successfully!
}
});
// [END rtdb_social_completion_callback]
}
/**
* @param {firebase.database.Reference} postRef
* @param {string} uid
*/
// [START rtdb_social_star_transaction]
function toggleStar(postRef, uid) {
postRef.transaction((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 updates = {};
updates[`posts/${key}/stars/${uid}`] = true;
updates[`posts/${key}/starCount`] = firebase.database.ServerValue.increment(1);
updates[`user-posts/${key}/stars/${uid}`] = true;
updates[`user-posts/${key}/starCount`] = firebase.database.ServerValue.increment(1);
firebase.database().ref().update(updates);
}
// [END rtdb_social_star_increment]
function readOnceWithGet(userId) {
// [START rtdb_read_once_get]
const dbRef = firebase.database().ref();
dbRef.child("users").child(userId).get().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]
}