-
DB에 쓰기 [
firebase.database.Reference.set(),
firebase.database.Reference.push() ]
/* 1. set() 메소드 */
firebase.database().ref('[키Refs]').set('[값]');
// JSON으로 여러 값을 한번에 입력도 가능합니다
firebase.database().ref('[키Refs]').set( {'[키]':'[값]', '[키]':'[값]', '[키]':'[값]', .. } );
/* 2. push() 메소드 (자동으로 unique key를 만들어서 주입) */
firebase.database().ref('[키Refs]').push([값]);
// push key 사용 방법
var messageListRef = firebase.database().ref('message_list');
var newMessageRef = messageListRef.push();
newMessageRef.set( {'user_id': 'ada', 'text': 'The Analytical Engine...'} );
-
DB에서 삭제 [
firebase.database.Reference.remove() ]
/* firebase.database.Reference.remove() */
firebase.database().ref('[키Refs]').remove();
-
DB에 업데이트 [
firebase.database.Reference.update() ]
/* firebase.database.Reference.update() */
firebase.database().ref('[키Refs]').update( {'[키]':'[값]', '[키]':'[값]', '[키]':'[값]', .. } );
-
DB에서 한번만 읽기 [
firebase.database.Reference.once() ]
/* firebase.database.Reference.once() */
firebase.database().ref('[키Refs]').once('[값]').then(function(snapshot) {
// console.log( snapshot.val() );
});
-
DB에서 변화 감지 [
firebase.database.Reference.on() ]
/* value : 모든 데이터 변화 감지 */
var starCountRef = firebase.database().ref('posts/' + postId + '/starCount');
starCountRef.on('value', function(snapshot) {
updateStarCount(postElement, snapshot.val());
});
var commentsRef = firebase.database().ref('post-comments/' + postId);
/* child_added : 자식 노드가 추가됨을 감지 */
commentsRef.on('child_added', function(data) {
addCommentElement(postElement, data.key, data.val().text, data.val().author);
});
/* child_added : 자식 노드의 값이 변화를 감지 */
commentsRef.on('child_changed', function(data) {
setCommentValues(postElement, data.key, data.val().text, data.val().author);
});
/* child_removed : 자식 노드의 위치 변화를 감지 (미리 정렬 되어 있어야 함) */
commentsRef.on('child_removed', function(data) {
deleteComment(postElement, data.key);
});
-
데이터 객체 [
DataSnapshot ]
/* 데이터의 예 */
{
"users": {
"ada": {
"first": "Ada",
"last": "Lovelace"
},
"alan": {
"first": "Alan",
"last": "Turing"
}
}
}
/* forEach() 메소드 사용의 예 */
var query = firebase.database().ref("users").orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
});
});
-
정렬
- orderByChild()
- orderByKey()
- orderByValue()
-
필터링
- limitToFirst()
- limitToLast()
- startAt()
- endAt()
- equalTo()
-
타임 스템프 [
firebase.database.ServerValue.TIMESTAMP ]
// 단독으로 사용은 불가능하고 DB에 값을 넣을때만 사용 가능
firebase.database().ref("접속시간").set( firebase.database.ServerValue.TIMESTAMP );
-
접속 끊김 감지 [
firebase.database.OnDisconnect ]
// 서버에서 리스너가 장착되고 뒤에 수행할 메소드를 덧붙이는 방식
firebase.database().ref("접속종료").onDisconnect().set( firebase.database.ServerValue.TIMESTAMP );
- SQL 변환의 예