3

I'm writing to several parts/nodes of my Firebase database in one update. Is it possible to set the priority of a node when doing this? Example:

firebaseRef.update([
    "/some/node/": "value",
    "/some/other/node": "other value"
])

What if I want to set the priority of the node at "/some/other/node" at the same time. Is that possible? Something like:

firebaseRef.update([
    "/some/node/": "value",
    "/some/other/node": {
        value: "other value",
        priority:  0 - Date.now()
    }
])
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
rodskagg
  • 3,827
  • 4
  • 27
  • 46

2 Answers2

4

You almost got it, the "magic" property is named .priority:

firebaseRef.update([
    "/some/node/": "value",
    "/some/other/node": {
        value: "other value",
        ".priority":  0 - Date.now()
    }
]

Note that there aren't really a lot of reasons to use priorities anymore in Firebase. Adding a normally named property (e.g. "reversedTimestamp": 0 - Date.now()) will accomplish the same and is less hidden/magic.

Update

It seems you want to update the node's value, not a child named value. To set the (primitive) value and priority in one go, use:

firebaseRef.update([
    "/some/node/": "value",
    "/some/other/node": {
        ".value": "other value",
        ".priority":  0 - Date.now()
    }
]);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Regarding not using priorities, I think it's a convenient way to fix a default sort order that isn't based on the name of the node – rodskagg Jan 27 '16 at 19:06
  • I have noticed a problem with this approach. If I include the ".priority" property, my validation rule for that node fails. The value is a boolean, and my validation rules is "newData.isBoolean()". This fails if I include .priority. If I just do "/some/other/nodes": true everything works fine. – rodskagg Jan 27 '16 at 21:33
0

I recommend using:

".priority": Firebase.ServerValue.TIMESTAMP

It creates a timestamp from the server. It makes sure that our data is in the order it was created in, even if our clients are in different time zones.

jakobinn
  • 1,832
  • 1
  • 20
  • 20