디스코드 클론코딩을 하면서 사용한 Firebase Cloudstore 에 대해 정리가 필요할 것 같아 블로그를 작성
참조 - https://firebase.google.com/docs/firestore?hl=ko
Cloud Firestore는 모바일 앱 개발을 위한 Firebase의 최신 데이터베이스로서 실시간 데이터베이스의 성공을 바탕으로 더욱 직관적인 새로운 데이터 모델을 이며, 실시간 데이터베이스보다 풍부하고 빠른 쿼리와 원활한 확장성을 제공한다





npm install firebase@9.21.0 --save
// firebase.ts
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: ...,
authDomain: ...,
projectId: ...,
storageBucket: ...,
messagingSenderId: ...,
appId: ...,
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Cloud Firestore and get a reference to the service
const db = getFirestore(app);
export { app, db };
collection(firestore, path, ...pathSegments)
collection(db, "channels")
collection(db, "channels", channelId, "messages")
doc(collection(db, "channels"), 'channel1')
query(collection(db, "channels")
getDoc(doc(collection(db, "channels"), 'channel1'))
addDoc(reference, data)
const addChannel = async () => {
const newChannel = prompt("채널명을 입력해주세요");
try{
if (newChannel && (newChannel as string).length > 0) {
await addDoc(collection(db, "channels"), {
channelName: newChannel,
});
} catch(error) {
console.error("Error adding document: ", error);
}
}
};
setDoc(doc(collection(db, 'channels'), 'channel1'), {
channelName : newChannel
})
updateDoc(doc(collection(db, 'channels'), 'channel1'), {
channelName : newChannel
})
deleteDoc(doc(db, "channels", channelId));
const useCollection = (data: string) => {
const [documents, setDocuments] = useState<Channel[]>([]);
useEffect(() => {
onSnapshot(query(collection(db, data)), (snapshot) => {
const results: Channel[] = [];
snapshot.docs.forEach((doc) => {
results.push({
id: doc.id,
channel: doc.data(),
});
});
setDocuments(results);
});
}, []);
return { documents };
};