Skip to content

Instantly share code, notes, and snippets.

@menuka94
Last active January 31, 2017 19:32
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 menuka94/12673b0ff9af209ed96e8b376f960e9a to your computer and use it in GitHub Desktop.
Save menuka94/12673b0ff9af209ed96e8b376f960e9a to your computer and use it in GitHub Desktop.
Firebase vs SQL
{
"users": {
"1": {
"name": "Max",
"bio": "Hello, I'm a programmer",
"profileImg": "https://..."
},
"9": {
"name": "Alice",
"bio": "I work as a business analyst",
"profileImg": "https://..."
}
},
"events": {
"fm": {
"name": "Firebase Meetup",
"date": 983275265320,
}
},
"eventAttendees": {
"fm": {
"1": "David",
"9": "Alice"
}
}
}
import * as firebase from 'firebase';
{
"users": {
"1": {
"name": "David",
"email": "david@email.com",
"age": 99,
"location": "SF",
"age_location": "99_SF"
},
"9": {
"name": "Alice",
"email": "alice@email.com",
"age": 28,
"location": "Berlin",
"age_location": "28_Berlin"
}
}
}
const rootRef = firebase.database().ref();
// 1. Select a user by UID
SELECT * FROM Users WHERE UID = 1;
const oneRef = rootRef.child('users').child('1');
// 2. Find a s user by email address
SELECT * FROM Users WHERE Email = 'alice@gmail.com';
const twoRef = rootRef.child('users').orderByChild('email').equalTo('alice@gmail.com');
// 3. Limit to 10 users
SELECT * FROM Users LIMIT 10;
const threeRef = rootRef.child('users').liminToFirst(10);
// 4. Get all users names that start with 'D'
SELECT * FROM Users WHERE Name LIKE 'D%';
const fourRef = rootRef.child('users').orderByChild('name').startAt('D').endAt('D\uf8ff');
// 5. Get all users who are less than 50
SELECT * FROM Users WHERE Age < 50;
const fiveRef = rootRef.child('users').orderByChild('age').endAt(49);
// 6. Get all users who are greater than 50
SELECT * FROM Users WHERE Age > 50;
const sixRef = rootRef.child('users').orderByChild('age').startAt(51);
// 7. Get all users who are between 20 and 100
SELECT * FROM Users WHERE Age >= 20 && Age <= 100;
const sevenRef = rootRef.child('users').orderByChild('age').startAt(20).endAt(100);
// 8. Get all users who are 28 and live in Berlin
SELECT * FROM Users WHERE Age = 28 && Location ='Berlin';
const eightRef = rootRef.child('users').orderByChlid('age_location').equalTo('28_Berlin');
SELECT event.Name as EventName
,event.Date as EventDate
,user.Name as AttendeeName
FROM Events as event
INNER JOIN Attendees as a
ON e.I === a.EventId
INNER JOIN Users as user
ON u.UId = a.UId
WHERE e.Id == 4;
attendees.on('child_added', snap => {
// append attendees to list
// '1' => 'Max'
// '9' => 'Alice'
let userRef = db.child('users/' + snap.key);
// once() will not listen in real-time
userRef.once('value').then(userSnap => {
// userSnap returns all details of the user
});
});
// real time listener implementation
let handles = [];
attendees.on('child_added', snap => {
let userRef = db.child('users/' + snap.key);
let fn = userRef.on('value', userSnap => {
});
handles.push(fn);
});
handles.forEach(fn => userRef.off('value', fn));
@menuka94
Copy link
Author

Firebase SDK doesn't support multiple orderByChild() calls in one query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment