Re: [Firebase] Update when using REST API?

206 views
Skip to first unread message

Anant Narayanan

unread,
Dec 30, 2012, 9:04:09 PM12/30/12
to fireba...@googlegroups.com
Hi Sarah,

Properties of an object are considered children, and you can reference
them directly by URL. For example, if I have an object like:

foo: {
bar: 20,
baz: 30
}

and I only want to update "baz", then I will do a PUT on
example.firebaseio.com/foo/baz.json instead of
example.firebaseio.com/foo.json

Is this what you were looking for? Let me know!

Cheers,
-Anant

On Sun, Dec 30, 2012 at 5:57 PM, Sarah Reichelt
<sarah.r...@gmail.com> wrote:
> Hi,
>
> I am using the REST API to add data to a Firebase database and that is
> working fine.
> My problem is when I want to update a single attribute in an existing tree.
> Using PUT deletes the entire record and replaces it with the new data.
>
> I guess what I need is a REST equivalent for the 'update' command.
> Is there such a thing?
>
> If not, is there a workaround?
>
> Cheers,
> Sarah
>
> --
> You received this message because you are subscribed to the Google Groups
> "Firebase Google Group" group.
> To post to this group, send email to fireba...@googlegroups.com.
>
>

Anant Narayanan

unread,
Dec 30, 2012, 10:20:05 PM12/30/12
to fireba...@googlegroups.com
Hi Sarah,

You're certainly on the right track - unfortunately it is not
currently possible to update the priority of an object without setting
the entire object again. This is a feature request we are actively
tracking and are working on. I'll let you know as soon as it is
available!

In the interim, would it be possible to share a bit more about why you
want to update the priority of the parent object when a child is
added? There might be other ways to structure your data so we can
achieve the end-goal. For instance, we could create a separate object
called "priorities" at the top-level which only contains a list of
object names with their priorities:

{
"priorities": {
"parent1": { ".priority": 2, "is": true},
"parent2": { ".priority": 1, "is": true}
},
"parent1": {
"foo": "foo_val"
},
"parent2": {
"bar": "bar_val"
}
}

When you add a child to, say parent1, you can update the priorities
object with a PUT to /priorities/parent1. Then, when you need to read
the data in order, first enumerate /priorities then fetch each parent
in the priority order.

Hope this helps!

-Anant

On Sun, Dec 30, 2012 at 6:57 PM, Sarah Reichelt
<sarah.r...@gmail.com> wrote:
> Thanks for the speedy reply Anant.
>
> I see now that this is the way to do it, but I can't work out exactly how to
> :-(
>
> What I am actually trying to do is to update the priority of an object when
> a new child is added to it.
> I add the child without any problem, but once that has worked, I want to set
> the .priority property of the parent object.
>
> I am using PHP as this will be running on a server without node.js
> installed.
>
> So here is how I add the child - I worked out that I had to send the object
> as a string version of JSON object.
>
> $url = 'https://my_db.firebaseio.com/' . $parentID . '/comms/' . $timeStamp
> . '.json';
> $fields = '{ ".priority" : ' . $timeStamp . ',
> "name" : "' . $name . '",
> "comment" : "' . $comment . '" }' ;
> $result = doPut($url, $fields);
>
> Then when I want to update the .priority of the parent object, I see that I
> need to PUT the data to this URL:
>
> $url = 'https://my_db.firebaseio.com/' . $parentID . '/.priority.json';
>
> But what exactly do I PUT?
> I don't have a key:value pair - I just have a value which is a UNIX
> timestamp stored in the $timeStamp variable.
>
> I have tried putting:
> $timeStamp; // replaces the contents of the parent object with the time
> stamp
> '"' . $timeStamp . '"'; // replaces the contents of the parent object with
> the time stamp
> '{' . $timeStamp . '}'; // does nothing
>
>
> I am sure I am on the right track, I am just missing something...
>
> Cheers,
> Sarah

Andrew Lee

unread,
Dec 30, 2012, 10:32:38 PM12/30/12
to fireba...@googlegroups.com
Sarah -

Sorry for the inconvenience! 

You could also download the current data and then write it back along with the new priority. Here is an example of setting an entire object with a priority using the REST API (though maybe you already found this):

curl -X PUT -d "{\"x\":1, \".priority\":7}" https://wishery.firebaseio.com/a.json

Again -- sorry about this! We'll have this feature soon.

-Andrew
Andrew Lee
Founder, Firebase
http://twitter.com/startupandrew
Reply all
Reply to author
Forward
0 new messages