Last active
December 25, 2015 17:39
-
-
Save ebeeson/7014450 to your computer and use it in GitHub Desktop.
Example of building a basic "index" in Firebase.
This file contains hidden or 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 users = firebase.child('users'); | |
var usersByEmailIndex = firebase.child('indexes/users/email'); | |
var createUser = function(username, emails) { | |
var newUserRef = users.push({ | |
username: username, | |
emails: emails | |
}); | |
var userId = newUserRef.name(); | |
emails.forEach(function(email) { | |
usersByEmailIndex.child(firebaseCodec.encode(email)).set(userId); | |
}); | |
return userId; | |
}; | |
var getUserByEmail = function(email, callback) { | |
usersByEmailIndex.child(firebaseCodec.encode(email)).once('value', function(snapshot) { | |
var userId = snapshot.val(); | |
users.child(userId).once('value', function(snapshot) { | |
callback(snapshot.val()); | |
}); | |
}); | |
}; | |
createUser('Erik Beeson', ['[email protected]', '[email protected]']); | |
getUserByEmail('[email protected]', function(user) { | |
console.log(user); // {username: 'Erik Beeson', emails: ['[email protected]', '[email protected]']} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment