Skip to content

Instantly share code, notes, and snippets.

@msenel
Last active December 21, 2016 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msenel/5e4179b2e78c20968b7102b2bc95f645 to your computer and use it in GitHub Desktop.
Save msenel/5e4179b2e78c20968b7102b2bc95f645 to your computer and use it in GitHub Desktop.
Firebase SDK javascript write examples
var database = firebase.database();
//var channel = new Channel('First Channel', 'First Channel Description', '#myfirstchannel', 'true', 'hQcWCU0smxVUNNDf9YHMl5f65sr1');
//var channel2 = new Channel('Second Channel', 'Second Channel Description', '#mysecondchannel', 'true', 'hQcWCU0smxVUNNDf9YHMl5f65sr1');
var user = new User('Mike', 'mike@example.com');
var userId = createUser(user);
var channel = new Channel(user.displayName + '\'s Channel', 'Channel Description', '#' + user.displayName + 'channel', 'true', userId);
saveChannel(channel);
function saveChannel(channel) {
// Get a key for a new Post.
var newChannelKey = database.child('channels').push().key;
var myChannels = {};
myChannels[newChannelKey] = true;
// Write the new post's data simultaneously in the channels list and the user's myChannels list.
var updates = {};
updates['/channels/' + newChannelKey] = channel;
updates['/users/' + channel.ownerId + '/myChannels'] = myChannels;
database().ref().update(updates)
.then(function() {
console.log('Synchronization succeeded');
})
.catch(function(error) {
console.log('Synchronization failed');
});;
}
saveUser(user) {
var userListRef = database.ref('users').push();
userListRef.set(user)
.then(function() {
console.log('Synchronization succeeded');
})
.catch(function(error) {
console.log('Synchronization failed');
});;
return userListRef.key;
}
function Channel(title, description, hashtag, type, ownerId) {
this.title = title;
this.description = description;
this.hashtag = hashtag;
this.public = type;
this.ownerId = ownerId;
}
function User(displayName, email) {
this.displayName = displayName;
this.email = email;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment