Skip to content

Instantly share code, notes, and snippets.

@brunoguerra
Created September 16, 2016 02:01
Show Gist options
  • Save brunoguerra/d1593baffddb7529365b9df49bce6bf8 to your computer and use it in GitHub Desktop.
Save brunoguerra/d1593baffddb7529365b9df49bce6bf8 to your computer and use it in GitHub Desktop.
Learning firebase
import firebase from 'firebase';
firebase.initializeApp({
serviceAccount: `${__dirname}/../../server/config/firebase.json`,
databaseURL: 'https://pontuali-dev.firebaseio.com/',
});
const db = firebase.database();
const dataRef = db.ref('/data');
dataRef.on('child_added', (snapshot) => {
console.log('addedded, child', snapshot.val());
});
console.log('dataRef', dataRef);
(async () => {
const allData = (await dataRef.child('new').once('value')).val();
console.log('allData', allData);
})();
// writing data
const blogRef = db.ref('data/saving-data/fireblog');
const usersRef = blogRef.child('users');
usersRef.set({
alanisawesome: {
date_of_birth: 'June 23, 1912',
full_name: 'Alan Turing',
},
gracehop: {
age: 24,
},
});
usersRef.child('gracehop').set({
date_of_birth: 'December 9, 1906',
full_name: 'Grace Hopper',
age: 27,
});
usersRef.child('gracehop/childs/1/updating').set({ age: 1 });
const hopperRef = usersRef.child('gracehop/childs/1/updating');
hopperRef.update({
nickname: 'Amazing Grace',
});
const postsRef = blogRef.child('posts');
postsRef.push().set({
author: 'alanisawesome',
title: 'The Turing Machine',
});
postsRef.push({
author: 'eilanhe1amazing',
title: 'The Turing Machine',
});
blogRef.on('value', (snapshot) => {
console.log('blogRef on value', snapshot.val());
}, (errorObject) => {
console.log(`The read failed: ${errorObject.code}`);
});
// reading
postsRef.orderByChild('status').equalTo(null).on('child_added', (snap) => {
console.log('by status null', snap.val(), snap);
postsRef.child(snap.key).update({
status: true,
});
});
export default db;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment