NoSQL 기반 데이터 베이스
await getDocs()
import { collection, getDocs } from "firebase/firestore";
const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
});();
await addDoc()
import { collection, addDoc } from "firebase/firestore";
// Add a new document with a generated id.
const docRef = await addDoc(collection(db, "cities"), {
name: "Tokyo",
country: "Japan"
});
console.log("Document written with ID: ", docRef.id);
await setDoc(doc(db, "컬렉션 명", "id"), 데이터)
import { doc, setDoc } from "firebase/firestore";
const cityRef = doc(db, 'cities', 'BJ');
setDoc(cityRef, { capital: true }, { merge: true });
id
값을 가진 도큐먼트값을 데이터
로 수정await updateDoc()
import { doc, updateDoc } from "firebase/firestore";
const washingtonRef = doc(db, "cities", "DC");
// Set the "capital" field of the city 'DC'
await updateDoc(washingtonRef, {
capital: true
});
await deleteDoc()
import { doc, deleteDoc } from "firebase/firestore";
await deleteDoc(doc(db, "cities", "DC"));
// Firebase SDK 라이브러리 가져오기
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-app.js";
import { getStorage } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-storage.js"
import { firebaseConfig } from "./config.js"
// Firebase 인스턴스 초기화
const app = initializeApp(firebaseConfig);
export const storage = getStorage(app);
getStorage()
const storage = getStorage(app);
ref(storage, file.name)
const storageRef = ref(storage, file.name);
file.name
은 storage 내부의 위치를 가리키도록 설정getDownloadURL(ref).then(url => {})
getDownloadURL(ref(storage, imgUrl))
.then((url) => {
$("#testimg").attr("src", url);
})
url
은 다운로드한 파일에 대한 주소uploadBytes(ref, file).then(snapshot => {})
uploadBytes(storageRef, file).then((snapshot) => {
console.log(snapshot);
});
file
데이터를 ref
로 업데이트snapshot
은 file
에 대한 메타데이터deleteObject(desertRef).then(() => {})
import { getStorage, ref, deleteObject } from "firebase/storage";
const storage = getStorage();
// Create a reference to the file to delete
const desertRef = ref(storage, 'images/desert.jpg');
// Delete the file
deleteObject(desertRef).then(() => {
// File deleted successfully
}).catch((error) => {
// Uh-oh, an error occurred!
});
```