andlin andlin - 2 months ago 17
Javascript Question

Firebase - set priority when doing an update/fanout to several nodes

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()
}
])

Answer

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()
    }
]);
Comments