Created
December 3, 2014 22:20
-
-
Save asciimike/6edf257062f433f89976 to your computer and use it in GitHub Desktop.
Mirror a Firebase to a second Firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Firebase = require('firebase'); | |
var fromRef = new Firebase('https://<FROM-FIREBASE>.firebaseio.com'); | |
var toRef = new Firebase('https://<TO-FIREBASE>.firebaseio.com'); | |
function main(){ | |
mirrorRef(fromRef,toRef); | |
} | |
function mirrorRef(fromReference, toReference){ | |
fromReference.on('child_added', function(snapshot){ | |
if (snapshot.hasChildren()){ | |
mirrorRef(fromReference.child(snapshot.key()),toReference.child(snapshot.key())); | |
} else { | |
snapshot.ref().on('value', function(child_snap){ | |
toReference.child(child_snap.key()).set(child_snap.val()); | |
}); | |
} | |
}); | |
fromReference.on('child_removed', function(snapshot){ | |
if (snapshot.hasChildren()){ | |
mirrorRef(fromReference.child(snapshot.key()),toReference.child(snapshot.key())); | |
} else { | |
toReference.child(snapshot.key()).remove(); | |
snapshot.ref().off(); | |
} | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment