ํ๋ก์ ํธ์ Firebase ์ค์น ๋ฐ ์ด๊ธฐํ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
npm install firebase
firebaseConfig๋ Firebase Console์์ ๋ณต์ฌํ ๊ฐ์ ์ฌ์ฉํ์ธ์.
// firebase.js
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
Firestore์ ํน์ ์ปฌ๋ ์ ์ ๋ฌธ์๋ฅผ ์ถ๊ฐํฉ๋๋ค.
import { collection, addDoc } from "firebase/firestore";
import { db } from "./firebase";
async function addData() {
try {
const docRef = await addDoc(collection(db, "users"), {
name: "John Doe",
age: 30,
email: "johndoe@example.com"
});
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
}
ํน์ ์ปฌ๋ ์ ์ ๋ชจ๋ ๋ฌธ์๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
import { collection, getDocs } from "firebase/firestore";
import { db } from "./firebase";
async function getData() {
const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
console.log(`${doc.id} =>`, doc.data());
});
}
addDoc: Firestore ์ปฌ๋ ์
์ ์๋ก์ด ๋ฌธ์๋ฅผ ์ถ๊ฐ.getDocs: Firestore ์ปฌ๋ ์
์ ๋ชจ๋ ๋ฌธ์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ.updateDoc, deleteDoc ๋ฑ ๋ค์ํ ๊ธฐ๋ฅ๋ ์ ๊ณตํ๋ ํ์์ ๋ฐ๋ผ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์.