diff --git a/database-next/emulator-suite.js b/database-next/emulator-suite.js index cdcf4896..c19f6829 100644 --- a/database-next/emulator-suite.js +++ b/database-next/emulator-suite.js @@ -11,22 +11,22 @@ const firebaseApp = initializeApp({ function onDocumentReady() { // [START rtdb_emulator_connect] - const { getDatabase } = require("firebase/database"); + const { getDatabase, useDatabaseEmulator } = require("firebase/database"); const db = getDatabase(firebaseApp); if (location.hostname === "localhost") { // Point to the RTDB emulator running on localhost. - db.useEmulator("localhost", 9000); + useDatabaseEmulator(db, "localhost", 9000); } // [END rtdb_emulator_connect] } function flushRealtimeDatabase() { // [START rtdb_emulator_flush] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, set } = require("firebase/database"); // With a database Reference, write null to clear the database. const db = getDatabase(firebaseApp); - db.ref().set(null); + set(ref(db), null); // [END rtdb_emulator_flush] } diff --git a/database-next/lists-of-data.js b/database-next/lists-of-data.js index b19371e4..8e47c1dd 100644 --- a/database-next/lists-of-data.js +++ b/database-next/lists-of-data.js @@ -11,13 +11,13 @@ const firebaseApp = initializeApp({ function socialPush() { // [START rtdb_social_push] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, push, set } = require("firebase/database"); // Create a new post reference with an auto-generated id const db = getDatabase(firebaseApp); - const postListRef = db.ref('posts'); - const newPostRef = postListRef.push(); - newPostRef.set({ + const postListRef = ref(db, 'posts'); + const newPostRef = push(postListRef); + set(newPostRef, { // ... }); // [END rtdb_social_push] @@ -31,19 +31,19 @@ function socialListenChildren() { function deleteComment(el, key) {}; // [START rtdb_social_listen_children] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onChildAdded, onChildChanged, onChildRemoved } = require("firebase/database"); const db = getDatabase(firebaseApp); - const commentsRef = db.ref('post-comments/' + postId); - commentsRef.on('child_added', (data) => { + const commentsRef = ref(db, 'post-comments/' + postId); + onChildAdded(commentsRef, (data) => { addCommentElement(postElement, data.key, data.val().text, data.val().author); }); - commentsRef.on('child_changed', (data) => { + onChildChanged(commentsRef, (data) => { setCommentValues(postElement, data.key, data.val().text, data.val().author); }); - commentsRef.on('child_removed', (data) => { + onChildRemoved(commentsRef, (data) => { deleteComment(postElement, data.key); }); // [END rtdb_social_listen_children] @@ -52,48 +52,50 @@ function socialListenChildren() { function socialListenValue() { // [START rtdb_social_listen_value] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onValue } = require("firebase/database"); const db = getDatabase(firebaseApp); - const ref = db.ref('/a/b/c'); + const dbRef = ref(db, '/a/b/c'); - ref.once('value', (snapshot) => { + 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 } = require("firebase/database"); + const { getDatabase, ref, query, orderByChild } = require("firebase/database"); const { getAuth } = require("firebase/auth"); const db = getDatabase(firebaseApp); const auth = getAuth(firebaseApp); const myUserId = auth.currentUser.uid; - const topUserPostsRef = db.ref('user-posts/' + myUserId).orderByChild('starCount'); + const topUserPostsRef = query(ref(db, 'user-posts/' + myUserId), orderByChild('starCount')); // [END rtdb_social_most_starred] } function socialMostViewed() { // [START rtdb_social_most_viewed] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, query, orderByChild } = require("firebase/database"); const db = getDatabase(firebaseApp); - const mostViewedPosts = db.ref('posts').orderByChild('metrics/views'); + const mostViewedPosts = query(ref(db, 'posts'), orderByChild('metrics/views')); // [END rtdb_social_most_viewed] } function socialRecent() { // [START rtdb_social_recent] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, query, limitToLast } = require("firebase/database"); const db = getDatabase(firebaseApp); - const recentPostsRef = db.ref('posts').limitToLast(100); + const recentPostsRef = query(ref(db, 'posts'), limitToLast(100)); // [END rtdb_social_recent] } diff --git a/database-next/offline.js b/database-next/offline.js index 461f3854..49009268 100644 --- a/database-next/offline.js +++ b/database-next/offline.js @@ -11,23 +11,23 @@ const firebaseApp = initializeApp({ function onDisconnectSimple() { // [START rtdb_ondisconnect_simple] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onDisconnect } = require("firebase/database"); const db = getDatabase(firebaseApp); - const presenceRef = db.ref("disconnectmessage"); + const presenceRef = ref(db, "disconnectmessage"); // Write a string when this client loses connection - presenceRef.onDisconnect().set("I disconnected!"); + onDisconnect(presenceRef).set("I disconnected!"); // [END rtdb_ondisconnect_simple] } function onDisconnectCallback() { - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onDisconnect } = require("firebase/database"); const db = getDatabase(firebaseApp); - const presenceRef = db.ref("disconnectmessage"); + const presenceRef = ref(db, "disconnectmessage"); // [START rtdb_ondisconnect_callback] - presenceRef.onDisconnect().remove((err) => { + onDisconnect(presenceRef).remove().catch((err) => { if (err) { console.error("could not establish onDisconnect event", err); } @@ -36,13 +36,13 @@ function onDisconnectCallback() { } function onDisconnectCancel() { - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onDisconnect } = require("firebase/database"); const db = getDatabase(firebaseApp); - const presenceRef = db.ref("disconnectmessage"); + const presenceRef = ref(db, "disconnectmessage"); // [START rtdb_ondisconnect_cancel] - const onDisconnectRef = presenceRef.onDisconnect(); + const onDisconnectRef = onDisconnect(presenceRef); onDisconnectRef.set("I disconnected"); // some time later when we change our minds onDisconnectRef.cancel(); @@ -51,11 +51,11 @@ function onDisconnectCancel() { function detectConnectionState() { // [START rtdb_detect_connection_state] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onValue } = require("firebase/database"); const db = getDatabase(firebaseApp); - const connectedRef = db.ref(".info/connected"); - connectedRef.on("value", (snap) => { + const connectedRef = ref(db, ".info/connected"); + onValue(connectedRef, (snap) => { if (snap.val() === true) { console.log("connected"); } else { @@ -67,21 +67,21 @@ function detectConnectionState() { function setServerTimestamp() { // [START rtdb_set_server_timestamp] - const { getDatabase, ServerValue } = require("firebase/database"); + const { getDatabase, ref, onDisconnect, serverTimestamp } = require("firebase/database"); const db = getDatabase(firebaseApp); - const userLastOnlineRef = db.ref("users/joe/lastOnline"); - userLastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP); + const userLastOnlineRef = ref(db, "users/joe/lastOnline"); + onDisconnect(userLastOnlineRef).set(serverTimestamp()); // [END rtdb_set_server_timestamp] } function estimateClockSkew() { // [START rtdb_estimate_clock_skew] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onValue } = require("firebase/database"); const db = getDatabase(firebaseApp); - const offsetRef = db.ref(".info/serverTimeOffset"); - offsetRef.on("value", (snap) => { + const offsetRef = ref(db, ".info/serverTimeOffset"); + onValue(offsetRef, (snap) => { const offset = snap.val(); const estimatedServerTimeMs = new Date().getTime() + offset; }); @@ -90,31 +90,31 @@ function estimateClockSkew() { function samplePresenceApp() { // [START rtdb_sample_presence_app] - const { getDatabase, ServerValue } = require("firebase/database"); + const { getDatabase, ref, onValue, push, onDisconnect, set, serverTimestamp } = require("firebase/database"); // Since I can connect from multiple devices or browser tabs, we store each connection instance separately // any time that connectionsRef's value is null (i.e. has no children) I am offline const db = getDatabase(firebaseApp); - const myConnectionsRef = db.ref('users/joe/connections'); + const myConnectionsRef = ref(db, 'users/joe/connections'); // stores the timestamp of my last disconnect (the last time I was seen online) - const lastOnlineRef = db.ref('users/joe/lastOnline'); + const lastOnlineRef = ref(db, 'users/joe/lastOnline'); - const connectedRef = db.ref('.info/connected'); - connectedRef.on('value', (snap) => { + const connectedRef = ref(db, '.info/connected'); + onValue(connectedRef, (snap) => { if (snap.val() === true) { // We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect) - const con = myConnectionsRef.push(); + const con = push(myConnectionsRef); // When I disconnect, remove this device - con.onDisconnect().remove(); + onDisconnect(con).remove(); // Add this device to my connections list // this value could contain info about the device or a timestamp too - con.set(true); + set(con, true); // When I disconnect, update the last time I was seen online - lastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP); + onDisconnect(lastOnlineRef).set(serverTimestamp()); } }); // [END rtdb_sample_presence_app] diff --git a/database-next/read-and-write.js b/database-next/read-and-write.js index 6d890183..6e3ac273 100644 --- a/database-next/read-and-write.js +++ b/database-next/read-and-write.js @@ -11,11 +11,11 @@ const firebaseApp = initializeApp({ function writeUserData_wrapped() { // [START rtdb_write_new_user] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, set} = require("firebase/database"); function writeUserData(userId, name, email, imageUrl) { const db = getDatabase(firebaseApp); - db.ref('users/' + userId).set({ + set(ref(db, 'users/' + userId), { username: name, email: email, profile_picture : imageUrl @@ -27,19 +27,19 @@ function writeUserData_wrapped() { function writeUserDataWithCompletion(userId, name, email, imageUrl) { // [START rtdb_write_new_user_completion] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, set } = require("firebase/database"); const db = getDatabase(firebaseApp); - db.ref('users/' + userId).set({ + set(ref(db, 'users/' + userId), { username: name, email: email, profile_picture : imageUrl - }, (error) => { - if (error) { - // The write failed... - } else { - // Data saved successfully! - } + }) + .then(() => { + // Data saved successfully! + }) + .catch((error) => { + // The write failed... }); // [END rtdb_write_new_user_completion] } @@ -52,11 +52,11 @@ function socialListenStarCount() { } // [START rtdb_social_listen_star_count] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onValue} = require("firebase/database"); const db = getDatabase(firebaseApp); - const starCountRef = db.ref('posts/' + postId + '/starCount'); - starCountRef.on('value', (snapshot) => { + const starCountRef = ref(db, 'posts/' + postId + '/starCount'); + onValue(starCountRef, (snapshot) => { const data = snapshot.val(); updateStarCount(postElement, data); }); @@ -65,22 +65,24 @@ function socialListenStarCount() { function socialSingleValueRead() { // [START rtdb_social_single_value_read] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, onValue } = require("firebase/database"); const { getAuth } = require("firebase/auth"); const db = getDatabase(firebaseApp); const auth = getAuth(firebaseApp); const userId = auth.currentUser.uid; - return db.ref('/users/' + userId).once('value').then((snapshot) => { + 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() { - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, child, push, update } = require("firebase/database"); // [START rtdb_social_write_fan_out] function writeNewPost(uid, username, picture, title, body) { @@ -97,14 +99,14 @@ function writeNewPost_wrapped() { }; // Get a key for a new Post. - const newPostKey = db.ref().child('posts').push().key; + 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 db.ref().update(updates); + return update(ref(db), updates); } // [END rtdb_social_write_fan_out] } @@ -115,32 +117,32 @@ function socialCompletionCallback() { const imageUrl = "https://example.com/image.png"; // [START rtdb_social_completion_callback] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, set } = require("firebase/database"); const db = getDatabase(firebaseApp); - db.ref('users/' + userId).set({ + set(ref(db, 'users/' + userId), { username: name, email: email, profile_picture : imageUrl - }, (error) => { - if (error) { - // The write failed... - } else { - // Data saved successfully! - } + }) + .then(() => { + // Data saved successfully! + }) + .catch((error) => { + // The write failed... }); // [END rtdb_social_completion_callback] } function toggleStar_wrapped() { // [START rtdb_social_star_transaction] - const { getDatabase } = require("firebase/database"); + const { getDatabase, ref, runTransaction } = require("firebase/database"); function toggleStar(uid) { const db = getDatabase(firebaseApp); - const postRef = db.ref('/posts/foo-bar-123'); + const postRef = ref(db, '/posts/foo-bar-123'); - postRef.transaction((post) => { + runTransaction(postRef, (post) => { if (post) { if (post.stars && post.stars[uid]) { post.starCount--; diff --git a/snippets/database-next/emulator-suite/rtdb_emulator_connect.js b/snippets/database-next/emulator-suite/rtdb_emulator_connect.js index c16f0210..b666d9b1 100644 --- a/snippets/database-next/emulator-suite/rtdb_emulator_connect.js +++ b/snippets/database-next/emulator-suite/rtdb_emulator_connect.js @@ -4,11 +4,11 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_emulator_connect_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, useDatabaseEmulator } from "firebase/database"; const db = getDatabase(firebaseApp); if (location.hostname === "localhost") { // Point to the RTDB emulator running on localhost. - db.useEmulator("localhost", 9000); + useDatabaseEmulator(db, "localhost", 9000); } // [END rtdb_emulator_connect_modular] \ No newline at end of file diff --git a/snippets/database-next/emulator-suite/rtdb_emulator_flush.js b/snippets/database-next/emulator-suite/rtdb_emulator_flush.js index 66f8b282..5f0e3f5c 100644 --- a/snippets/database-next/emulator-suite/rtdb_emulator_flush.js +++ b/snippets/database-next/emulator-suite/rtdb_emulator_flush.js @@ -4,9 +4,9 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_emulator_flush_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, set } from "firebase/database"; // With a database Reference, write null to clear the database. const db = getDatabase(firebaseApp); -db.ref().set(null); +set(ref(db), null); // [END rtdb_emulator_flush_modular] \ No newline at end of file diff --git a/snippets/database-next/lists-of-data/rtdb_social_listen_children.js b/snippets/database-next/lists-of-data/rtdb_social_listen_children.js index 5aefb875..a527edeb 100644 --- a/snippets/database-next/lists-of-data/rtdb_social_listen_children.js +++ b/snippets/database-next/lists-of-data/rtdb_social_listen_children.js @@ -4,19 +4,19 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_listen_children_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onChildAdded, onChildChanged, onChildRemoved } from "firebase/database"; const db = getDatabase(firebaseApp); -const commentsRef = db.ref('post-comments/' + postId); -commentsRef.on('child_added', (data) => { +const commentsRef = ref(db, 'post-comments/' + postId); +onChildAdded(commentsRef, (data) => { addCommentElement(postElement, data.key, data.val().text, data.val().author); }); -commentsRef.on('child_changed', (data) => { +onChildChanged(commentsRef, (data) => { setCommentValues(postElement, data.key, data.val().text, data.val().author); }); -commentsRef.on('child_removed', (data) => { +onChildRemoved(commentsRef, (data) => { deleteComment(postElement, data.key); }); // [END rtdb_social_listen_children_modular] \ No newline at end of file diff --git a/snippets/database-next/lists-of-data/rtdb_social_listen_value.js b/snippets/database-next/lists-of-data/rtdb_social_listen_value.js index 3958a0d9..7250cc86 100644 --- a/snippets/database-next/lists-of-data/rtdb_social_listen_value.js +++ b/snippets/database-next/lists-of-data/rtdb_social_listen_value.js @@ -4,16 +4,18 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_listen_value_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onValue } from "firebase/database"; const db = getDatabase(firebaseApp); -const ref = db.ref('/a/b/c'); +const dbRef = ref(db, '/a/b/c'); -ref.once('value', (snapshot) => { +onValue(dbRef, (snapshot) => { snapshot.forEach((childSnapshot) => { const childKey = childSnapshot.key; const childData = childSnapshot.val(); // ... }); +}, { + onlyOnce: true }); // [END rtdb_social_listen_value_modular] \ No newline at end of file diff --git a/snippets/database-next/lists-of-data/rtdb_social_most_starred.js b/snippets/database-next/lists-of-data/rtdb_social_most_starred.js index 8c20de7a..96354b52 100644 --- a/snippets/database-next/lists-of-data/rtdb_social_most_starred.js +++ b/snippets/database-next/lists-of-data/rtdb_social_most_starred.js @@ -4,12 +4,12 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_most_starred_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, query, orderByChild } from "firebase/database"; import { getAuth } from "firebase/auth"; const db = getDatabase(firebaseApp); const auth = getAuth(firebaseApp); const myUserId = auth.currentUser.uid; -const topUserPostsRef = db.ref('user-posts/' + myUserId).orderByChild('starCount'); +const topUserPostsRef = query(ref(db, 'user-posts/' + myUserId), orderByChild('starCount')); // [END rtdb_social_most_starred_modular] \ No newline at end of file diff --git a/snippets/database-next/lists-of-data/rtdb_social_most_viewed.js b/snippets/database-next/lists-of-data/rtdb_social_most_viewed.js index 2f5c03f8..3d661562 100644 --- a/snippets/database-next/lists-of-data/rtdb_social_most_viewed.js +++ b/snippets/database-next/lists-of-data/rtdb_social_most_viewed.js @@ -4,8 +4,8 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_most_viewed_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, query, orderByChild } from "firebase/database"; const db = getDatabase(firebaseApp); -const mostViewedPosts = db.ref('posts').orderByChild('metrics/views'); +const mostViewedPosts = query(ref(db, 'posts'), orderByChild('metrics/views')); // [END rtdb_social_most_viewed_modular] \ No newline at end of file diff --git a/snippets/database-next/lists-of-data/rtdb_social_push.js b/snippets/database-next/lists-of-data/rtdb_social_push.js index e3c7f81c..313ff67c 100644 --- a/snippets/database-next/lists-of-data/rtdb_social_push.js +++ b/snippets/database-next/lists-of-data/rtdb_social_push.js @@ -4,13 +4,13 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_push_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, push, set } from "firebase/database"; // Create a new post reference with an auto-generated id const db = getDatabase(firebaseApp); -const postListRef = db.ref('posts'); -const newPostRef = postListRef.push(); -newPostRef.set({ +const postListRef = ref(db, 'posts'); +const newPostRef = push(postListRef); +set(newPostRef, { // ... }); // [END rtdb_social_push_modular] \ No newline at end of file diff --git a/snippets/database-next/lists-of-data/rtdb_social_recent.js b/snippets/database-next/lists-of-data/rtdb_social_recent.js index 58fa2e38..a946817f 100644 --- a/snippets/database-next/lists-of-data/rtdb_social_recent.js +++ b/snippets/database-next/lists-of-data/rtdb_social_recent.js @@ -4,8 +4,8 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_recent_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, query, limitToLast } from "firebase/database"; const db = getDatabase(firebaseApp); -const recentPostsRef = db.ref('posts').limitToLast(100); +const recentPostsRef = query(ref(db, 'posts'), limitToLast(100)); // [END rtdb_social_recent_modular] \ No newline at end of file diff --git a/snippets/database-next/offline/rtdb_detect_connection_state.js b/snippets/database-next/offline/rtdb_detect_connection_state.js index 3756f5be..b2bedd44 100644 --- a/snippets/database-next/offline/rtdb_detect_connection_state.js +++ b/snippets/database-next/offline/rtdb_detect_connection_state.js @@ -4,11 +4,11 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_detect_connection_state_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onValue } from "firebase/database"; const db = getDatabase(firebaseApp); -const connectedRef = db.ref(".info/connected"); -connectedRef.on("value", (snap) => { +const connectedRef = ref(db, ".info/connected"); +onValue(connectedRef, (snap) => { if (snap.val() === true) { console.log("connected"); } else { diff --git a/snippets/database-next/offline/rtdb_estimate_clock_skew.js b/snippets/database-next/offline/rtdb_estimate_clock_skew.js index cdb19491..0d7fea48 100644 --- a/snippets/database-next/offline/rtdb_estimate_clock_skew.js +++ b/snippets/database-next/offline/rtdb_estimate_clock_skew.js @@ -4,11 +4,11 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_estimate_clock_skew_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onValue } from "firebase/database"; const db = getDatabase(firebaseApp); -const offsetRef = db.ref(".info/serverTimeOffset"); -offsetRef.on("value", (snap) => { +const offsetRef = ref(db, ".info/serverTimeOffset"); +onValue(offsetRef, (snap) => { const offset = snap.val(); const estimatedServerTimeMs = new Date().getTime() + offset; }); diff --git a/snippets/database-next/offline/rtdb_ondisconnect_callback.js b/snippets/database-next/offline/rtdb_ondisconnect_callback.js index 6af1ee61..6b7f2950 100644 --- a/snippets/database-next/offline/rtdb_ondisconnect_callback.js +++ b/snippets/database-next/offline/rtdb_ondisconnect_callback.js @@ -4,7 +4,7 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_ondisconnect_callback_modular] -presenceRef.onDisconnect().remove((err) => { +onDisconnect(presenceRef).remove().catch((err) => { if (err) { console.error("could not establish onDisconnect event", err); } diff --git a/snippets/database-next/offline/rtdb_ondisconnect_cancel.js b/snippets/database-next/offline/rtdb_ondisconnect_cancel.js index 7af11312..f5c3c972 100644 --- a/snippets/database-next/offline/rtdb_ondisconnect_cancel.js +++ b/snippets/database-next/offline/rtdb_ondisconnect_cancel.js @@ -4,7 +4,7 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_ondisconnect_cancel_modular] -const onDisconnectRef = presenceRef.onDisconnect(); +const onDisconnectRef = onDisconnect(presenceRef); onDisconnectRef.set("I disconnected"); // some time later when we change our minds onDisconnectRef.cancel(); diff --git a/snippets/database-next/offline/rtdb_ondisconnect_simple.js b/snippets/database-next/offline/rtdb_ondisconnect_simple.js index eedfab7f..838ab744 100644 --- a/snippets/database-next/offline/rtdb_ondisconnect_simple.js +++ b/snippets/database-next/offline/rtdb_ondisconnect_simple.js @@ -4,10 +4,10 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_ondisconnect_simple_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onDisconnect } from "firebase/database"; const db = getDatabase(firebaseApp); -const presenceRef = db.ref("disconnectmessage"); +const presenceRef = ref(db, "disconnectmessage"); // Write a string when this client loses connection -presenceRef.onDisconnect().set("I disconnected!"); +onDisconnect(presenceRef).set("I disconnected!"); // [END rtdb_ondisconnect_simple_modular] \ No newline at end of file diff --git a/snippets/database-next/offline/rtdb_sample_presence_app.js b/snippets/database-next/offline/rtdb_sample_presence_app.js index bef04145..e093238e 100644 --- a/snippets/database-next/offline/rtdb_sample_presence_app.js +++ b/snippets/database-next/offline/rtdb_sample_presence_app.js @@ -4,31 +4,31 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_sample_presence_app_modular] -import { getDatabase, ServerValue } from "firebase/database"; +import { getDatabase, ref, onValue, push, onDisconnect, set, serverTimestamp } from "firebase/database"; // Since I can connect from multiple devices or browser tabs, we store each connection instance separately // any time that connectionsRef's value is null (i.e. has no children) I am offline const db = getDatabase(firebaseApp); -const myConnectionsRef = db.ref('users/joe/connections'); +const myConnectionsRef = ref(db, 'users/joe/connections'); // stores the timestamp of my last disconnect (the last time I was seen online) -const lastOnlineRef = db.ref('users/joe/lastOnline'); +const lastOnlineRef = ref(db, 'users/joe/lastOnline'); -const connectedRef = db.ref('.info/connected'); -connectedRef.on('value', (snap) => { +const connectedRef = ref(db, '.info/connected'); +onValue(connectedRef, (snap) => { if (snap.val() === true) { // We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect) - const con = myConnectionsRef.push(); + const con = push(myConnectionsRef); // When I disconnect, remove this device - con.onDisconnect().remove(); + onDisconnect(con).remove(); // Add this device to my connections list // this value could contain info about the device or a timestamp too - con.set(true); + set(con, true); // When I disconnect, update the last time I was seen online - lastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP); + onDisconnect(lastOnlineRef).set(serverTimestamp()); } }); // [END rtdb_sample_presence_app_modular] \ No newline at end of file diff --git a/snippets/database-next/offline/rtdb_set_server_timestamp.js b/snippets/database-next/offline/rtdb_set_server_timestamp.js index f9319e07..14850121 100644 --- a/snippets/database-next/offline/rtdb_set_server_timestamp.js +++ b/snippets/database-next/offline/rtdb_set_server_timestamp.js @@ -4,9 +4,9 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_set_server_timestamp_modular] -import { getDatabase, ServerValue } from "firebase/database"; +import { getDatabase, ref, onDisconnect, serverTimestamp } from "firebase/database"; const db = getDatabase(firebaseApp); -const userLastOnlineRef = db.ref("users/joe/lastOnline"); -userLastOnlineRef.onDisconnect().set(ServerValue.TIMESTAMP); +const userLastOnlineRef = ref(db, "users/joe/lastOnline"); +onDisconnect(userLastOnlineRef).set(serverTimestamp()); // [END rtdb_set_server_timestamp_modular] \ No newline at end of file diff --git a/snippets/database-next/read-and-write/rtdb_social_completion_callback.js b/snippets/database-next/read-and-write/rtdb_social_completion_callback.js index 5251b5c4..cb22a488 100644 --- a/snippets/database-next/read-and-write/rtdb_social_completion_callback.js +++ b/snippets/database-next/read-and-write/rtdb_social_completion_callback.js @@ -4,18 +4,18 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_completion_callback_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, set } from "firebase/database"; const db = getDatabase(firebaseApp); -db.ref('users/' + userId).set({ +set(ref(db, 'users/' + userId), { username: name, email: email, profile_picture : imageUrl -}, (error) => { - if (error) { - // The write failed... - } else { - // Data saved successfully! - } +}) +.then(() => { + // Data saved successfully! +}) +.catch((error) => { + // The write failed... }); // [END rtdb_social_completion_callback_modular] \ No newline at end of file diff --git a/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js b/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js index 186afb4f..3a7c7e85 100644 --- a/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js +++ b/snippets/database-next/read-and-write/rtdb_social_listen_star_count.js @@ -4,11 +4,11 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_listen_star_count_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onValue} from "firebase/database"; const db = getDatabase(firebaseApp); -const starCountRef = db.ref('posts/' + postId + '/starCount'); -starCountRef.on('value', (snapshot) => { +const starCountRef = ref(db, 'posts/' + postId + '/starCount'); +onValue(starCountRef, (snapshot) => { const data = snapshot.val(); updateStarCount(postElement, data); }); diff --git a/snippets/database-next/read-and-write/rtdb_social_single_value_read.js b/snippets/database-next/read-and-write/rtdb_social_single_value_read.js index b5bfc662..5e68d8c1 100644 --- a/snippets/database-next/read-and-write/rtdb_social_single_value_read.js +++ b/snippets/database-next/read-and-write/rtdb_social_single_value_read.js @@ -4,15 +4,17 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_single_value_read_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, onValue } from "firebase/database"; import { getAuth } from "firebase/auth"; const db = getDatabase(firebaseApp); const auth = getAuth(firebaseApp); const userId = auth.currentUser.uid; -return db.ref('/users/' + userId).once('value').then((snapshot) => { +return onValue(ref(db, '/users/' + userId), (snapshot) => { const username = (snapshot.val() && snapshot.val().username) || 'Anonymous'; // ... +}, { + onlyOnce: true }); // [END rtdb_social_single_value_read_modular] \ No newline at end of file diff --git a/snippets/database-next/read-and-write/rtdb_social_star_transaction.js b/snippets/database-next/read-and-write/rtdb_social_star_transaction.js index 9c905d2c..9f0679a8 100644 --- a/snippets/database-next/read-and-write/rtdb_social_star_transaction.js +++ b/snippets/database-next/read-and-write/rtdb_social_star_transaction.js @@ -4,13 +4,13 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_social_star_transaction_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, runTransaction } from "firebase/database"; function toggleStar(uid) { const db = getDatabase(firebaseApp); - const postRef = db.ref('/posts/foo-bar-123'); + const postRef = ref(db, '/posts/foo-bar-123'); - postRef.transaction((post) => { + runTransaction(postRef, (post) => { if (post) { if (post.stars && post.stars[uid]) { post.starCount--; diff --git a/snippets/database-next/read-and-write/rtdb_social_write_fan_out.js b/snippets/database-next/read-and-write/rtdb_social_write_fan_out.js index 0c2973c4..d51a23eb 100644 --- a/snippets/database-next/read-and-write/rtdb_social_write_fan_out.js +++ b/snippets/database-next/read-and-write/rtdb_social_write_fan_out.js @@ -18,13 +18,13 @@ function writeNewPost(uid, username, picture, title, body) { }; // Get a key for a new Post. - const newPostKey = db.ref().child('posts').push().key; + 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 db.ref().update(updates); + return update(ref(db), updates); } // [END rtdb_social_write_fan_out_modular] \ No newline at end of file diff --git a/snippets/database-next/read-and-write/rtdb_write_new_user.js b/snippets/database-next/read-and-write/rtdb_write_new_user.js index c6a84358..85f0ad83 100644 --- a/snippets/database-next/read-and-write/rtdb_write_new_user.js +++ b/snippets/database-next/read-and-write/rtdb_write_new_user.js @@ -4,11 +4,11 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_write_new_user_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, set} from "firebase/database"; function writeUserData(userId, name, email, imageUrl) { const db = getDatabase(firebaseApp); - db.ref('users/' + userId).set({ + set(ref(db, 'users/' + userId), { username: name, email: email, profile_picture : imageUrl diff --git a/snippets/database-next/read-and-write/rtdb_write_new_user_completion.js b/snippets/database-next/read-and-write/rtdb_write_new_user_completion.js index cd10104d..41dffc81 100644 --- a/snippets/database-next/read-and-write/rtdb_write_new_user_completion.js +++ b/snippets/database-next/read-and-write/rtdb_write_new_user_completion.js @@ -4,18 +4,18 @@ // To make edits to the snippets in this file, please edit the source // [START rtdb_write_new_user_completion_modular] -import { getDatabase } from "firebase/database"; +import { getDatabase, ref, set } from "firebase/database"; const db = getDatabase(firebaseApp); -db.ref('users/' + userId).set({ +set(ref(db, 'users/' + userId), { username: name, email: email, profile_picture : imageUrl -}, (error) => { - if (error) { - // The write failed... - } else { - // Data saved successfully! - } +}) +.then(() => { + // Data saved successfully! +}) +.catch((error) => { + // The write failed... }); // [END rtdb_write_new_user_completion_modular] \ No newline at end of file