๐ ํด๋น ๊ธ์ ๋ฆฌ์กํธ ๋ค์ดํฐ๋ธ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ์ต๋๋ค.
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.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
// ์ฝ์ 4docs() ์์ฑ์ ํตํด ๋ฐฐ์ด๋ก ๋ฐํ๋ฐ๊ฑฐ๋ forEach() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ด๊ฑฐํ ์ ์๋ค.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");
});
get)const addNote = async (newNote: INote) => {
await memosCollection.add(newNote);
};
add() ๋ฉ์๋์ ์ถ๊ฐํ ๋ด์ฉ(๊ฐ์ฒด)๋ฅผ ๋ฃ์ผ๋ฉด ๋๋ค.const updateNote = async ({ noteId, updated }: IUpdateNote) => {
await memosCollection.doc(noteId).update(updated);
};
update() ๋ฉ์๋์ ์
๋ฐ์ดํธํ ๋ด์ฉ(๊ฐ์ฒด)๋ฅผ ๋ฃ์ผ๋ฉด ๋๋ค.doc() : ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์ ํน์ ๋ฌธ์์ ์ ๊ทผํ๊ธฐ ์ํ ์ฐธ์กฐ(reference)๋ฅผ ์์ฑํ๋ ์ญํ ์ ํ๋ค. ์ด ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌ๋ ๋ฌธ์๋ ID๋ ๊ฒฝ๋ก์ ํด๋นํ๋ ๋ฌธ์์ ๋ํ ์ฐธ์กฐ๋ฅผ ์์ฑํ๋ค.const deleteNote = async (noteId: undefined | string) => {
await memosCollection.doc(noteId).delete();
};
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();
});
};
documentSnapshot.ref = DocumentReference๐ย Reference ์ฐธ์กฐ๋?
๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ํน์ ๋ฐ์ดํฐ๋ฅผ ์๋ณํ๊ณ ์ ๊ทผํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ ๊ฒ์ด๋ค.
์ฆ, ๋ฐ์ดํฐ์ ์์น๋ฅผ ์๋ ค์ฃผ๋ ์ญํ ์ ํ๋ฉฐ ์ด๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์กฐํ, ์์ , ์ญ์ ํ๊ฑฐ๋ ๋ค์ํ ์์ ์ ์ํํ ์ ์๋ค.
Firestore์์DocumentReference์CollectionReference๋ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ด๋ค.DocumentReference๋ ํน์ ๋ฌธ์๋ฅผ ์ฐธ์กฐํ๋ฉฐ,CollectionReference๋ ํน์ ์ปฌ๋ ์ ์ ์ฐธ์กฐํ๋ค.
React-Native Firebase Docs
Firebase - FireStore ๊ณต์ ๋ฌธ์