firebase ์ด๊ธฐํ
getFirestore()
: firestore๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํ ํจ์Create
๊ธฐ๋ฅ | addDoc | setDoc |
---|---|---|
๋ฌธ์ ID ์์ฑ | firestore๊ฐ ์๋์ผ๋ก ๋ฌธ์ ID๋ฅผ ์์ฑ | ์ฌ์ฉ์ ์ง์ ๋ฌธ์ ID๋ฅผ ์ง์ ์์ฑ |
๊ธฐ์กด ๋ฌธ์ ์ฒ๋ฆฌ | ์ ๋ฌธ์ ์์ฑ, ๊ธฐ์กด ๋ฌธ์์ ์ถฉ๋ ์์ | ๊ธฐ์กด ๋ฌธ์๊ฐ ์์ ๊ฒฝ์ฐ ๋ฎ์ด์( merge option ) |
์ฌ์ฉ ์๋๋ฆฌ์ค | ๋ฌธ์ ID๋ฅผ ์๋์ผ๋ก ์์ฑํ๊ณ ์ถ์ ๋ | ํน์ ๋ฌธ์ ID๋ฅผ ์ง์ ํ๊ฑฐ๋ ๊ธฐ์กด ๋ฌธ์๋ฅผ ์ ๋ฐ์ดํธ |
ํจ์ ํธ์ถ ๋ฐฉ์ | addDoc(collectionRef, data) | setDoc(docRef, data) |
๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ: ๋ฐ์ดํฐ๊ฐ ์ผ๊ด๋๊ณ ์ ํํ์ง์ ๋ํ ์ ๋
firebase๋ ์ปฌ๋ ์ ์ด ์๋๋ผ๋ ๋ฐ์ดํฐ ์ฝ์ ์ ์กฐ์ํ๋ฉด ์ปฌ๋ ์ ์ด ์์ฑ๋๋ฉฐ ์๋์ผ๋ก ๋ฐ์ดํฐ ์ฝ์ ์ฒ๋ฆฌ๋จ
โ ๋ฌด๊ฒฐ์ฑ โฌ๏ธ
Read
ํญ๋ชฉ | getDocs | onSnapshot |
---|---|---|
๊ธฐ๋ฅ | firestore์์ ํ ๋ฒ์ ๋ฐ์ดํฐ ์กฐํ | ์ค์๊ฐ ๋ฐ์ดํฐ ๋ณ๊ฒฝ ๊ฐ์ง ๋ฐ ๋ฐ์ |
๋ฐํ ๊ฐ | ์ ์ ๋ฐ์ดํฐ ์ค๋ ์ท( QuerySnapshot ) | ๋ฐ์ดํฐ ๊ตฌ๋ ํด์ ํจ์( Unsubscribe ) |
์ค์๊ฐ ๋ฐ์ | ์๋์ผ๋ก ๋ฐ์ดํฐ ๊ฐฑ์ ํ์ | ์ค์๊ฐ์ผ๋ก ๋ฐ์ดํฐ ์๋ ๋ฐ์ |
์ฌ์ฉ ์ฌ๋ก | ํน์ ์์ ์ ๋ฐ์ดํฐ ์กฐํ๊ฐ ํ์ํ ๋ | ์ค์๊ฐ UI ์ ๋ฐ์ดํธ๊ฐ ํ์ํ ๋ |
์ฅ์ | ๋จ์ผ ์์ฒญ์ผ๋ก ์ฑ๋ฅ ํจ์จ์ | ๋ฐ์ดํฐ ๋ณ๊ฒฝ์ด ์ฆ์ ๋ฐ์๋จ |
๋จ์ | ์ค์๊ฐ ๋ฐ์์ด ์ด๋ ค์ | ์ง์์ ์ธ ๊ตฌ๋ ์ผ๋ก ๋ฆฌ์์ค ์ฌ์ฉ ์ฆ๊ฐ |
collection(store ์ธ์คํด์ค, ์ปฌ๋ ์
์ด๋ฆ)
: ์ปฌ๋ ์
์กฐํ ํจ์getDocs(collection ์ฐธ์กฐ ๊ฐ์ฒด)
: ๋ฌธ์๋ค ์กฐํ ํจ์, ํ๋ก๋ฏธ์ค ๋ฐํgetDoc()
: ๋ฌธ์ ์กฐํ ํจ์snapshots.docs
: ๋ฌธ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํonSnapshot()
: ์ค์๊ฐ ๋ฐ์ดํฐ ๋ณ๊ฒฝ ๊ฐ์ง ๋ฐ ๋ฐ์Update
updateDoc(collectionRef, data)
: ๋ฌธ์ ์ฐธ์กฐ ๊ฐ์ฒด๋ฅผ ํ์ฉํ ๋ฌธ์ ์
๋ฐ์ดํธ
doc(firestore ์ฐธ์กฐ ๊ฐ์ฒด, ์ปฌ๋ ์ ์ด๋ฆ, ๋ฌธ์ ID)
: ๋ฌธ์ ID๋ก ํน์ ๋ฌธ์ ๊ฐ์ ธ์ค๊ธฐ
Delete
deleteDoc(docRef)
: ๋ฌธ์ ์ฐธ์กฐ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํด ํน์ ๋ฌธ์ ์ญ์ import {
collection,
getDocs,
addDoc,
setDoc,
doc,
updateDoc,
deleteDoc,
query,
where,
orderBy,
} from "firebase/firestore";
import { db } from "../config/firebaseConfig";
export const firebaseFirestoreService = {
// ๋ฐ์ดํฐ ์กฐํ( todoList ์ปฌ๋ ์
์กฐํ ): READ
async getTodoListCollection() {
const collectionRef = collection(db, "todoList");
const todoSnapshots = await getDocs(collectionRef);
const data = todoSnapshots.docs.map((doc) => ({
id: doc.id,
...(doc.data() as { taskNum: number; name: string }),
}));
return data;
},
// ๋ฐ์ดํฐ ์์ฑ( todo ์ปฌ๋ ์
๋ด์ ๋ฌธ์ ์ถ๊ฐ ): CREATE
async createTodoCollectionDoc(targetTodo: { name: string; taskNum: number }) {
try {
const collectionRef = collection(db, "todoList");
await addDoc(collectionRef, { targetTodo });
} catch (e) {
console.error("โ createTodoCollectionDoc error: ", e);
}
},
// ๋ฐ์ดํฐ ์์ : UPDATE
async createTodo(targetTodo: { name: string; taskNum: number }) {
try {
const docRef = doc(db, "todoList", "todoList");
await setDoc(docRef, {
name: "todo",
taskNum: 1,
description: "test",
});
await setDoc(
docRef,
{
name: "todo",
taskNum: 1,
},
{
merge: true, // ๋ฎ์ด์ฐ๊ธฐ X, ๋ณํฉํ๋ ์ต์
}
);
} catch (e) {
console.error("โ createTodo error: ", e);
}
},
// ๋ฐ์ดํฐ ์์ : UPDATE
async updateTod() {
try {
const docRef = doc(db, "todoList", "todoList");
await updateDoc(docRef, {
name: "todoUpdate",
});
} catch (e) {
console.error("updateUser error", e);
}
},
// ๋ฐ์ดํฐ ์ญ์ : DELETE
async deleteTodo() {
try {
const docRef = doc(db, "todoList", "todoList");
await deleteDoc(docRef);
} catch (e) {
console.error("โ delete todo error: ", e);
}
},
// query ์ฌ์ฉ: ๋ฐ์ดํฐ๋ฅผ ํํฐ๋ง ํ์ฌ ์กฐํ: GET
async getTasksByName(targetName: string) {
try {
const collectionRef = collection(db, "todoList");
const querystring = query(collectionRef, where("name", "==", targetName));
const snapshot = await getDocs(querystring);
console.log(snapshot.size, snapshot.empty);
if (snapshot.empty) {
console.log("No matching documents for the provided name.");
} else {
console.log(`Found ${snapshot.size} documents.`);
}
} catch (e) {
console.error("โ getTasksByName: ", e);
}
},
// ์ ๋ ฌํ๊ธฐ: GET
// 1๋ฒ ์ธ์๋ง ์ฃผ๋ฉด ์ค๋ฆ์ฐจ์, 2๋ฒ์ธ์์ 'desc' ์ฃผ๋ฉด ๋ด๋ฆผ์ฐจ์
async orderTasksByName() {
try {
const collectionRef = collection(db, "todoList");
const querystring = query(collectionRef, orderBy("name"));
const snapshot = await getDocs(querystring);
} catch (e) {
console.error("โ getTasksByName: ", e);
}
},
async orderTasksByNameDesc() {
try {
const collectionRef = collection(db, "todoList");
const querystring = query(collectionRef, orderBy("name", "desc"));
const snapshot = await getDocs(querystring);
} catch (e) {
console.error("โ getTasksByName: ", e);
}
},
// ๋ณตํฉ query: GET
async multiQuery() {
try {
const collectionRef = collection(db, "todoList");
const querystring = query(
collectionRef,
orderBy("createdAt", "desc"),
where("name", "==", "a")
);
const snapshot = await getDocs(querystring);
});
} catch (e) {
console.error("โ getTasksByName: ", e);
}
},
};