๐Ÿ”ฅ Firebase CRUD

Yeonnยท2024๋…„ 11์›” 8์ผ
0

Backend ๐Ÿ‘€

๋ชฉ๋ก ๋ณด๊ธฐ
7/10
post-thumbnail

โœ”๏ธย firebase ์ดˆ๊ธฐํ™”

  • getFirestore(): firestore๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜

โœ”๏ธย Create

๊ธฐ๋ŠฅaddDocsetDoc
๋ฌธ์„œ ID ์ƒ์„ฑfirestore๊ฐ€ ์ž๋™์œผ๋กœ ๋ฌธ์„œ ID๋ฅผ ์ƒ์„ฑ์‚ฌ์šฉ์ž ์ง€์ • ๋ฌธ์„œ ID๋ฅผ ์ง์ ‘ ์ƒ์„ฑ
๊ธฐ์กด ๋ฌธ์„œ ์ฒ˜๋ฆฌ์ƒˆ ๋ฌธ์„œ ์ƒ์„ฑ, ๊ธฐ์กด ๋ฌธ์„œ์™€ ์ถฉ๋Œ ์—†์Œ๊ธฐ์กด ๋ฌธ์„œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ๋ฎ์–ด์”€( merge option )
์‚ฌ์šฉ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฌธ์„œ ID๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์„ ๋•ŒํŠน์ • ๋ฌธ์„œ ID๋ฅผ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ๋ฌธ์„œ๋ฅผ ์—…๋ฐ์ดํŠธ
ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹addDoc(collectionRef, data)setDoc(docRef, data)

๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ: ๋ฐ์ดํ„ฐ๊ฐ€ ์ผ๊ด€๋˜๊ณ  ์ •ํ™•ํ•œ์ง€์— ๋Œ€ํ•œ ์ •๋„
firebase๋Š” ์ปฌ๋ ‰์…˜์ด ์—†๋”๋ผ๋„ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…์„ ์กฐ์ž‘ํ•˜๋ฉด ์ปฌ๋ ‰์…˜์ด ์ƒ์„ฑ๋˜๋ฉฐ ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์ฒ˜๋ฆฌ๋จ
โ†’ ๋ฌด๊ฒฐ์„ฑ โฌ‡๏ธ

โœ”๏ธย Read

ํ•ญ๋ชฉgetDocsonSnapshot
๊ธฐ๋Šฅ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);
    }
  },
};

0๊ฐœ์˜ ๋Œ“๊ธ€