-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathlists-of-data.js
93 lines (75 loc) · 2.71 KB
/
lists-of-data.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
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
function socialPush() {
// [START rtdb_social_push]
const { getDatabase, ref, push, set } = require("firebase/database");
// Create a new post reference with an auto-generated id
const db = getDatabase();
const postListRef = ref(db, 'posts');
const newPostRef = push(postListRef);
set(newPostRef, {
// ...
});
// [END rtdb_social_push]
}
function socialListenChildren() {
const postElement = document.querySelector("#post");
const postId = "1234";
function addCommentElement(el, key, text, author) {}
function setCommentValues(el, key, text, author) {};
function deleteComment(el, key) {};
// [START rtdb_social_listen_children]
const { getDatabase, ref, onChildAdded, onChildChanged, onChildRemoved } = require("firebase/database");
const db = getDatabase();
const commentsRef = ref(db, 'post-comments/' + postId);
onChildAdded(commentsRef, (data) => {
addCommentElement(postElement, data.key, data.val().text, data.val().author);
});
onChildChanged(commentsRef, (data) => {
setCommentValues(postElement, data.key, data.val().text, data.val().author);
});
onChildRemoved(commentsRef, (data) => {
deleteComment(postElement, data.key);
});
// [END rtdb_social_listen_children]
}
function socialListenValue() {
// [START rtdb_social_listen_value]
const { getDatabase, ref, onValue } = require("firebase/database");
const db = getDatabase();
const dbRef = ref(db, '/a/b/c');
onValue(dbRef, (snapshot) => {
snapshot.forEach((childSnapshot) => {
const childKey = childSnapshot.key;
const childData = childSnapshot.val();
// ...
});
}, {
onlyOnce: true
});
// [END rtdb_social_listen_value]
}
function socialMostStarred() {
// [START rtdb_social_most_starred]
const { getDatabase, ref, query, orderByChild } = require("firebase/database");
const { getAuth } = require("firebase/auth");
const db = getDatabase();
const auth = getAuth();
const myUserId = auth.currentUser.uid;
const topUserPostsRef = query(ref(db, 'user-posts/' + myUserId), orderByChild('starCount'));
// [END rtdb_social_most_starred]
}
function socialMostViewed() {
// [START rtdb_social_most_viewed]
const { getDatabase, ref, query, orderByChild } = require("firebase/database");
const db = getDatabase();
const mostViewedPosts = query(ref(db, 'posts'), orderByChild('metrics/views'));
// [END rtdb_social_most_viewed]
}
function socialRecent() {
// [START rtdb_social_recent]
const { getDatabase, ref, query, limitToLast } = require("firebase/database");
const db = getDatabase();
const recentPostsRef = query(ref(db, 'posts'), limitToLast(100));
// [END rtdb_social_recent]
}