[Firebase] FireStore ๋ฌธ์„œ CRUD

Chloe Kยท2023๋…„ 8์›” 28์ผ
post-thumbnail

๐Ÿ“Œ ํ•ด๋‹น ๊ธ€์€ ๋ฆฌ์•กํŠธ ๋„ค์ดํ‹ฐ๋ธŒ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ ์กฐํšŒ(Read)

const getFolders = async (user: TUser): Promise<IFolder[]> => {
  const querySnapshot = await foldersCollection
    .where("creatorId", "==", user?.uid)
    .orderBy("createdAt", "asc")
    .get();

  const data: IFolder[] = querySnapshot.docs.map((documentSnapshot) => {
    const docData = documentSnapshot.data();
    return {
      id: documentSnapshot.id,
      createdAt: docData.createdAt,
      creatorId: docData.creatorId,
      name: docData.name,
    };
  });

  return data;
};

querySnapshot

  • ํŒŒ์ด์–ด๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋กœ ์–ป์–ด์ง€๋Š” ๊ฐ์ฒด์ด๋‹ค. (์ฟผ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ํ•„ํ„ฐ๋งํ•˜๋Š” ์ž‘์—…)
  • ์ฟผ๋ฆฌ์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์„œ์˜ ์Šค๋ƒ…์ƒท์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค. (์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค)
  • ์Šค๋ƒ…์ƒท์€ ๋ฌธ์„œ์˜ ๋ฐ์ดํ„ฐ์™€ ํ•ด๋‹น ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.
  • querySnapshot.docs(): ์ฟผ๋ฆฌ์Šค๋ƒ…์ƒท์— ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ์ด๋‹ค.
  • querySnapshot.forEach() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์—ด๊ฑฐํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
    const userUid = "user123"; // ์˜ˆ์‹œ ์‚ฌ์šฉ์ž UID
    
    const querySnapshot = await memosCollection.where("creatorId", "==", userUid).get();
    
    querySnapshot.forEach((documentSnapshot) => {
      const memoData = documentSnapshot.data();
      console.log("Memo Title:", memoData.title);
    });
    
    // ์ฝ˜์†” 1
    // ์ฝ˜์†” 2
    // ์ฝ˜์†” 3
    // ์ฝ˜์†” 4
  • ๋ฌธ์„œ ์ฆ‰ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด docs() ์†์„ฑ์„ ํ†ตํ•ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜๋ฐ›๊ฑฐ๋‚˜ forEach() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

documentSnapshot

  • querySnapshot์„ ํ†ตํ•ด ์–ป์€ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ๋ณ„ ๋ฌธ์„œ์— ๋Œ€ํ•œ ์Šค๋ƒ…์ƒท์ด๋‹ค.
  • ๊ฐ ๋ฌธ์„œ์—๋Š” ๋ฐ์ดํ„ฐ์™€ ํ•ด๋‹น ๋ฌธ์„œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
  • documentSnapshot.data() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผํ•œ๋‹ค.
    • ํ•ด๋‹น ๋ฌธ์„œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
    • ์Šค๋ƒ…์ƒท์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋“ฑ์„ ์ œ์™ธํ•œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

โ“์ฟผ๋ฆฌ(ํ•„ํ„ฐ๋ง)์—†์ด documentSnapshot๋งŒ์„ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด

const getSingleDocumentSnapshot = async (documentId: string) => {
  const documentSnapshot = await memosCollection.doc(documentId).get();

  if (documentSnapshot.exists) {
    const data = documentSnapshot.data();
    // ์ด์ œ data๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    console.log("Document Data:", data);
  } else {
    console.log("Document not found");
  }
};
// ํŠน์ • ์ปฌ๋ ‰์…˜๊ณผ ๋ฌธ์„œ ID๋กœ DocumentReference ์ƒ์„ฑ
const documentRef = firestore.collection("collectionName").doc("documentId");

// ๋ฌธ์„œ์˜ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    const data = documentSnapshot.data();
    console.log("Document Data:", data);
  } else {
    console.log("Document not found");
  }
});

// ๋ฌธ์„œ ์—…๋ฐ์ดํŠธ
documentRef.update({ field: newValue }).then(() => {
  console.log("Document updated successfully");
});

// ๋ฌธ์„œ ์‚ญ์ œ
documentRef.delete().then(() => {
  console.log("Document deleted successfully");
});
  • ์ปฌ๋ ‰์…˜์— ์ ‘๊ทผํ•˜์—ฌ doc๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํŠน์ • ID๋ฅผ ๊ฐ–๋Š” ๋ฌธ์„œ์— ์ ‘๊ทผํ•œ๋‹ค. (get)
  • ์ดํ›„ ๊ฐœ๋ณ„ ๋ฌธ์„œ (single document)์˜ data๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ๋ฐ ์ƒ์„ฑ (Create)

const addNote = async (newNote: INote) => {
  await memosCollection.add(newNote);
};
  • add() ๋ฉ”์†Œ๋“œ์— ์ถ”๊ฐ€ํ•  ๋‚ด์šฉ(๊ฐ์ฒด)๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

๋ฐ์ดํ„ฐ ์ˆ˜์ • ๋ฐ ์—…๋ฐ์ดํŠธ (Update)

const updateNote = async ({ noteId, updated }: IUpdateNote) => {
  await memosCollection.doc(noteId).update(updated);
};
  • update() ๋ฉ”์†Œ๋“œ์— ์—…๋ฐ์ดํŠธํ•  ๋‚ด์šฉ(๊ฐ์ฒด)๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.
  • doc() : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์˜ ํŠน์ • ๋ฌธ์„œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ฐธ์กฐ(reference)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ฌธ์„œ๋‚˜ ID๋‚˜ ๊ฒฝ๋กœ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์„œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    • ํ•ด๋‹น ๋ฌธ์„œ์˜ ๊ณ ์œ  ID๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผํ•œ๋‹ค.

๋ฐ์ดํ„ฐ ์‚ญ์ œ (Delete)

const deleteNote = async (noteId: undefined | string) => {
  await memosCollection.doc(noteId).delete();
};
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ doc๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ํŠน์ • ๋ฌธ์„œ์— ์ ‘๊ทผํ•˜์—ฌ ์‚ญ์ œ

์ผ๊ด„ ์‚ญ์ œ

export const deleteNotesFromFolder = async (user: TUser, name: string) => {
  const querySnapshot = await memosCollection
    .where("creatorId", "==", user?.uid)
    .where("folder", "==", name)
    .get();
  querySnapshot.forEach((documentSnapshot) => {
    documentSnapshot.ref.delete();
  });
};
  • querySnapshot์˜ ๋ฉ”์†Œ๋“œ์ธ forEach๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ณ„ ๋ฌธ์„œ์— ์ ‘๊ทผํ•œ๋‹ค.
  • documentSnapshot.ref = DocumentReference
    • documentSnapshot์— ํฌํ•จ๋œ ๋ฌธ์„œ์˜ DocumentReference
    • ํŠน์ • ๋ฌธ์„œ ์Šค๋ƒ…์ƒท์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ์ด๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํŠน์ • ๋ฌธ์„œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์–ป๊ณ ๋‚˜์„œ forEach๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ์‚ญ์ œํ•œ๋‹ค.

๐Ÿ“Œย Reference ์ฐธ์กฐ๋ž€?

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์‹๋ณ„ํ•˜๊ณ  ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์ฆ‰, ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ ์ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
Firestore์—์„œ DocumentReference์™€ CollectionReference๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด์ด๋‹ค. DocumentReference๋Š” ํŠน์ • ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜๋ฉฐ, CollectionReference๋Š” ํŠน์ • ์ปฌ๋ ‰์…˜์„ ์ฐธ์กฐํ•œ๋‹ค.

References

React-Native Firebase Docs
Firebase - FireStore ๊ณต์‹ ๋ฌธ์„œ

profile
Frontend Developer

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