Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase update vs set

As the title says, I can't get the differences between update and set. Also the docs can't help me, as the update example works exactly the same if I use set instead.

The update example from the docs:

function writeNewPost(uid, username, title, body) {

    var postData = {
        author: username,
        uid: uid,
        body: body,
        title: title,
        starCount: 0
    };

    var newPostKey = firebase.database().ref().child('posts').push().key;

    var updates = {};
    updates['/posts/' + newPostKey] = postData;
    updates['/user-posts/' + uid + '/' + newPostKey] = postData;

    return firebase.database().ref().update(updates);
}

The same example using set

function writeNewPost(uid, username, title, body) {

    var postData = {
        author: username,
        uid: uid,
        body: body,
        title: title,
        starCount: 0
    };

    var newPostKey = firebase.database().ref().child('posts').push().key;

    firebase.database().ref().child('/posts/' + newPostKey).set(postData);
    firebase.database().ref().child('/user-posts/' + uid + '/' + newPostKey).set(postData);
}

So maybe the example from the docs should be updated, because now it looks like update and set do the exact same thing.

Kind regards, Bene

like image 707
Bene Avatar asked Aug 12 '16 17:08

Bene


People also ask

What is the difference between set and update in Firebase?

In RESTful terms “set” is like an PUT, and an “update” is like a PATCH, but there is a bit more interesting distinctions under the covers. Firebase… pirate…. PATCH… get it?

Which method is used to update Firebase data?

4. Which method used to update the Firebase data? Explanation: We can update the Firebase data using update command. 5.

How does Firebase realtime updates work?

Instead of typical HTTP requests, the Firebase Realtime Database uses data synchronization—every time data changes, any connected device receives that update within milliseconds. Provide collaborative and immersive experiences without thinking about networking code.


1 Answers

Atomicity

One big difference between the two samples you've given is in the number of write operations they send to the Firebase servers.

In the first case, you're sending a single update() command. That entire command will either succeed or fail. For example: if the user has permission to post to /user-posts/' + uid, but doesn't have permission to post to /posts, the entire operation will fail.

In the second case, you're sending two separate commands. With the same permissions, the write to /user-posts/' + uid will now succeed, while the write to /posts will fail.

Partial update vs complete overwrite

Another difference is not immediately visible in this example. But say that you're updating the title and body of an existing post, instead of writing a new post.

If you'd use this code:

firebase.database().ref().child('/posts/' + newPostKey)
        .set({ title: "New title", body: "This is the new body" });

You'd be replacing the entire existing post. So the original uid, author and starCount fields would be gone and there'll just be the new title and body.

If on the other hand you use an update:

firebase.database().ref().child('/posts/' + newPostKey)
        .update({ title: "New title", body: "This is the new body" });

After executing this code, the original uid, author and starCount will still be there as well as the updated title and body.

like image 192
Frank van Puffelen Avatar answered Oct 12 '22 08:10

Frank van Puffelen