- SQL database
- ์ค๋๋
- ๋ง์ ๊ท์น๊ณผ ๊ด์ต
- ๊ทธ๋ ๋ค ๋ณด๋ ์ข ๋ ์์ ๋ก์
- ๊ด๊ณ ๋งบ๊ณ ์๋ ๋ฐ์ดํฐ๊ฐ ์์ฃผ ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ NoSQL์์๋ ์ฌ๋ฌ Collection์ ์์ ํด์ผ ํ๋ค. ๋ช ํํ ์คํค๋ง๋ฅผ ๋ณด์ฅํ ๊ฒฝ์ฐ SQL์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
- NoSQL database
- ์ ์ฐํ๊ณ ๊ท์น์ด ๋ง์ด ์์ด์ ์ฌ์ฉํ๊ธฐ ์ฌ์
- ๊ท์น์ด ๋ง์ด ์๊ธฐ ๋๋ฌธ์ ์ ํ๋๋ ๋ถ๋ถ์ด ์์
- ๋ถ์ฐํ ๊ตฌ์กฐ๋ก ์ฌ๋ฌ ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ฐ ์ ์ฅํ์ฌ ํน์ ์๋ฒ์ ์ฅ์ ๋ฐ์ํด๋ ์๋น์ค๊ฐ ์ค์ง๋์ง ์๊ฒ ํ๋ค.
- ๋ช ํํ์ง ์์ ํ ์ด๋ธ ์คํค๋ง๋ฅผ ๊ฐ์ง๋ค. ์ ํํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ์ ์๊ฑฐ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๊ฐ ๋ณ๊ฒฝ, ํ์ฅ ๋ ๊ฒฝ์ฐ NoSQL์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
- ํ์ด์ค๋ถ, ํธ์ํฐ ๊ฐ์ ์์ ๋ฏธ๋์ด์ ๊ฒฝ์ฐ ๊ฒ์๊ธ์ ์ ์ฅํ๋๋ฐ NoSQL database๋ฅผ ์ฌ์ฉํ๋ค.
Cloud Firestore์ DB๋ NoSQL database์ด๋ค.
๋ง์ ๊ฒ์ ํ๋ก๊ทธ๋จํ์ง ์์๋ ๋๊ณ ์ ์ฐํ๋ค.
๊ทธ๋ ๋ค๋ณด๋ ๋ช ๊ฐ์ง ์ ํ ์ฌํญ์ด ์๋ค.
collection(๊ธฐ๋ณธ์ ์ผ๋ก ํด๋ ๊ฐ์ ๊ฐ๋
), document(์ปดํจํฐ์ ์๋ ๋ฌธ์ ๊ฐ์ ๊ฐ๋
)๋ฅผ ๊ฐ์ง๋ค.
๋ฐ๋ผ์ ํ๋์ Database๋ collection๋ค์ ๊ฐ์ง๊ณ ์๊ณ , ๊ฐ๊ฐ์ collection์ document๋ค์ ๊ฐ์ง๊ณ ์๋ค.
๋ชฝ๊ณ ๋๋น๊ฐ ์๊ฐ๋๋ ๊ตฌ๋จผ! ๋ชฝ๊ณ ๋๋น๋ document db ์์ ใ ใ !
Cloud Firestore์ admin panel ์์ + ์ปฌ๋ ์
์์์ ํด๋ฆญํ์ฌ ์ปฌ๋ ์
์ ๋ง๋ค์ด ๋ณด์.

tweets ์ปฌ๋ ์
์ ๋ง๋ ๋ค.

๋คํ๋จผํธ ID๋ฅผ ์ ํ์. ์๋ ID๋ฅผ ๋๋ฅด๋ฉด ์๋์ผ๋ก ์ ํด์ค๋ค.

์ด๋ ๊ฒ ์ด๋๋ฏผ ํจ๋์์ ์ปฌ๋ ์
๊ณผ ์ปฌ๋ ์
์ ๋คํ๋จผํธ๋ฅผ ์ฝ๊ฒ ๋ง๋ค ์ ์๋ค.

์ด์ ์ด๋๋ฏผ ํจ๋์์ ์ฌ์ฉํ ๊ธฐ๋ฅ์ ๋๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์ฌ ๊ตฌํํด๋ณด์. ์ฝ๋๋ฅผ ์์ฑํ์ฌ ์ปฌ๋ ์
์ ์์ฑํ๊ณ ๋คํ๋จผํธ์ ๋ฐ์ดํฐ๋ฅผ ์์ฑํด ๋ณด์.
๋จผ์ ๐fbase.js์ firestore๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด firebase/firestore์์ getFirestore๋ฅผ ๊ฐ์ ธ์จ๋ค.
๊ทธ๋ฆฌ๊ณ dbService๋ผ๋ ๋ณ์์ด๋ฆ์ผ๋ก getFirestore()๋ฅผ ๋ด๋ณด๋ธ๋ค.
//โ
ํ์ด์ด๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ๊ฐ์ ธ์ค๊ธฐ
import { getFirestore } from "firebase/firestore";
//โ
๋๋น ์๋น์ค ๋ด๋ณด๋ด๊ธฐ
export const dbService = getFirestore();
//โ
Firebase Web v.9
//์ฐธ๊ณ : https://firebase.google.com/docs/web/modular-upgrade#refactor_to_the_modular_style
//Follow this pattern to import other Firebase services
//import { } from 'firebase/<service>';
//์ฑ ์ด๊ธฐํ ๊ฐ์ ธ์ค๊ธฐ
import { initializeApp } from "firebase/app";
//ํ์ด์ด๋ฒ ์ด์ค์์ ์ธ์ฆ ์์คํ
๊ฐ์ ธ์ค๊ธฐ
import { getAuth } from "firebase/auth";
//โ
ํ์ด์ด๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ๊ฐ์ ธ์ค๊ธฐ
import { getFirestore } from "firebase/firestore";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_APP_ID,
};
//ํ์ด์ด๋ฒ ์ด์ค ์ฑ ์ด๊ธฐํ ํ๊ธฐ
const app = initializeApp(firebaseConfig);
//์ด์ค ์๋น์ค ๋ด๋ณด๋ด๊ธฐ
//๊ตณ์ด firebase ์ ์ฒด ๋ค ์๋ด๋ณด๋ด๊ณ authService๋ง exportํ ์ ์๋ค.
//authService ์ฌ์ฉํ๊ณ ์ถ์ ๋ ๋ง๋ค getAuth(app) ํธ์ถํด์ผ ํ๋๊น app.js์์ ๋จ ํ ๋ฒ๋ง ํธ์ถํ๊ณ export ์ํค๋ฉด ๋๋ค.
export const authService = getAuth(app);
//โ
๋๋น ์๋น์ค ๋ด๋ณด๋ด๊ธฐ
export const dbService = getFirestore();
routes/Home.js์ fbase์์ dbService๋ฅผ ๊ฐ์ ธ์จ๋ค.//dbService ๊ฐ์ ธ์ค๊ธฐ
import { dbService } from "fbase";ํธ์ํ๊ธฐ๋ฅผ ๋๋ฅด๋ฉด, ์ฆ submitํ๋ฉด ํธ์ํด์ฑ์ ํ์ด์ด์คํ ์ด์ ์๋ก์ด document๋ฅผ ์์ฑํ ์ ์๋๋ก ์ฝ๋๋ฅผ ์์ฑํ์.firebase/firestore์์ collection, addDoc, serverTimestamp๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ์ ธ์จ๋ค.
import { collection, addDoc, serverTimestamp } from "firebase/firestore";
onSubmitํจ์ ์์ ๋คํ๋จผํธ์ ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
Cloud Firestore๋
Collection์ ์ ์ฅ๋Document์data๋ฅผ ์ ์ฅํ๋ค.
- Cloud Firestore๋ ์ฒ์์ผ๋ก Document์ data๋ฅผ ์ถ๊ฐํ ๋ ์์์ ์ผ๋ก Collection๊ณผ Document๋ฅผ ๋ง๋ ๋ค.
- Collection์ด๋ Document๋ฅผ ๋ช ์์ ์ผ๋ก ๋ง๋ค ํ์๋ ์๋ค.
- ๋ค์ ์์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ์ Collection๊ณผ Document๋ฅผ ๋ง๋ค ์ ์๋ค.
//Web v.9 import { collection, addDoc } from "firebase/firestore"; // try { const docRef = await addDoc(collection(db, "์ปฌ๋ ์ ์ด๋ฆ"), { //data๋ `key: value` ์์ผ๋ก ์ ๋ณด๋ฅผ ์ ์ผ๋ฉด ๋๋ค. first: "Ada", last: "Lovelace", born: 1815 }); console.log("Document written with ID: ", docRef.id); } catch (e) { console.error("Error adding document: ", e); }
- ๋ํ
๊ฐ์ Collection์๋ค๋ฅธ Document๋ ์ถ๊ฐํ ์ ์๋ค.- ์์
Document์๋ ์๋ํค(key)์๊ฐ(value)์ ์์ ํฌํจํด๋ ๋๋ค. ์ฆ, Collection์ Document์๋ ์๋ก ๋ค๋ฅธ ์ ๋ณด ์ธํธ๊ฐ ํฌํจ๋๋ ๋๋ค. (์ด ๋ง์ ์คํค๋ง๊ฐ ๋ช ํํ์ง ์๋ค๋ ๋ป์ธ ๋ฏ ใ ใ ! ํ์ด์ด๋ฒ ์ด์ค๋ ๋ ธ์์คํ์!)//Web v.9 import { collection, addDoc } from "firebase/firestore"; // try { const docRef = await addDoc(collection(db, "์ปฌ๋ ์ ์ด๋ฆ"), { first: "Alan", //์ด๋ฐ ์์ผ๋ก ์์ ๋คํ๋จผํธ์๋ ์๋ ์ ๋ณด ์ธํธ๋ฅผ ํฌํจํ ์ ์์! middle: "Mathison", last: "Turing", born: 1912 }); console.log("Document written with ID: ", docRef.id); } catch (e) { console.error("Error adding document: ", e); }
addDoc(collection(db, "์ปฌ๋ ์
์ด๋ฆ"), { key: value });์ ์ฌ์ฉํ์ฌ collection์ Document์ data๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
const Home = () => {
//ํ์์ ํธ์ ๋ด์ฉ ์์ฑํ๋ ํผ
const [tweet, setTweet] = useState("");
const onSubmit = async (event) => {
event.preventDefault();
//ํธ์ํ๊ธฐ ๋๋ฅด๋ฉด ์๋ก์ด document ์์ฑํ๊ธฐ
try {
const docRef = await addDoc(collection(dbService, "tweets"), {
tweet, // tweet(๋คํ๋จผํธ์ key): tweet(value๋ก tweet state ๊ฐ)
createdAt: serverTimestamp(), //Date.now(),๋ก ํด๋ ๋์ง๋ง ์ด์ ์๋๊ฑฐ ํจ ์จ๋ณด์(ํ์์กด ๋๋ถ์3 = ์์ธ๋ก ์ค์ ๋์ด ์์)
});
console.log("Document written with ID: ", docRef.id);
} catch (error) {
console.error("Error adding document: ", error);
}
//state ๋น์์ form ๋น์ฐ๊ธฐ
setTweet("");
};import React, { useState } from "react";
import { dbService } from "fbase";
import { collection, addDoc, serverTimestamp } from "firebase/firestore";
const Home = () => {
//ํ์์ ํธ์ ๋ด์ฉ ์์ฑํ๋ ํผ
const [tweet, setTweet] = useState("");
const onSubmit = async (event) => {
event.preventDefault();
//ํธ์ํ๊ธฐ ๋๋ฅด๋ฉด ์๋ก์ด document ์์ฑํ๊ธฐ
try {
const docRef = await addDoc(collection(dbService, "tweets"), {
tweet, // tweet(๋คํ๋จผํธ์ key): tweet(value๋ก tweet state ๊ฐ)
createdAt: serverTimestamp(), //Date.now(),๋ก ํด๋ ๋์ง๋ง ์ด์ ์๋๊ฑฐ ํจ ์จ๋ณด์(ํ์์กด ๋๋ถ์3 = ์์ธ๋ก ์ค์ ๋์ด ์์)
});
console.log("Document written with ID: ", docRef.id);
} catch (error) {
console.error("Error adding document: ", error);
}
//state ๋น์์ form ๋น์ฐ๊ธฐ
setTweet("");
};
const onChange = (event) => {
const {
target: { value },
} = event;
setTweet(value);
//console.log(tweet);
};
return (
<div>
<form onSubmit={onSubmit}>
<input
type="text"
placeholder="๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๋์?"
maxLength={120}
value={tweet}
onChange={onChange}
/>
<input type="submit" value="ํธ์ํ๊ธฐ" />
</form>
</div>
);
};
export default Home;