- The Firebase Realtime Database is a cloud-hosted NoSQL.
- Firestore CRUD Collection + real-time updates
- Firebase Auth signup
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
https://firebase.google.com/docs/web/setup#available-libraries
// Import the functions you need from the SDKs you need
import {initializeApp} from "firebase/app";
import {getAnalytics} from "firebase/analytics";
import {
getFirestore, collection, getDocs, addDoc, deleteDoc, doc, updateDoc,
onSnapshot,
query,
where,
orderBy,
getDoc,
serverTimestamp
} from 'firebase/firestore';
import {
getAuth,
createUserWithEmailAndPassword,
onAuthStateChanged
} from 'firebase/auth';
const firebaseConfig = {
apiKey: "AIzaSyDWaL05RsCwiOaTY7XJ22Yfky14l1zEAG4",
authDomain: "fir-9-app-c602d.firebaseapp.com",
projectId: "fir-9-app-c602d",
storageBucket: "fir-9-app-c602d.appspot.com",
messagingSenderId: "967892818152",
appId: "1:967892818152:web:b4464527b8b7072204115d",
measurementId: "G-2WS9D6EHMS"
};
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore();
const auth = getAuth();
// collection ref
const colRef = collection(db, 'users');
getDocs(colRef).then((snapshot) => {
let users = [];
snapshot.docs.forEach(doc => users.push({...doc.data(), id: doc.id}));
console.log('getDocs (on init)', users);
}).catch(err => console.log(err.message));
const unsubCol = onSnapshot(colRef, (snapshot) => {
let users = [];
snapshot.docs.forEach(doc => users.push({...doc.data(), id: doc.id}));
console.log('onSnapshot (real time)', users);
});
- !!! orderBy - should have index !!! https://console.firebase.google.com/u/0/project/fir-9-app-c602d/firestore/indexes
const q = query(colRef, where("age", "==", "444"), orderBy('createdAt'));
const unsubCol2 = onSnapshot(q, (snapshot) => {
let users = [];
snapshot.docs.forEach(doc => users.push({...doc.data(), id: doc.id}));
console.log('onSnapshot (real time) query where ', users);
});
addDoc(colRef, {
email: 'example@gmail.com',
age: 23,
createdAt: serverTimestamp()
}).then(() => {});
const docRef = doc(db, 'users', deleteUserForm.id.value)
deleteDoc(docRef).then(() => {})
const updateUserForm = document.querySelector('.update');
const docRef = doc(db, 'users', updateUserForm.id.value)
updateDoc(docRef, {email: 'example@gmail.com'})
.then(() => {
// reset
});
const docRef = doc(db, 'users', '3Oa8ufFb3To2qrBF5wlB')
getDocs(docRef).then(doc => console.log(doc))
onSnapshot(docRef, (doc => console.log(doc)));
createUserWithEmailAndPassword(auth, 'example@gmail.com', 'pass123')
.then(cred => console.log('user created:', cred.user))
.catch(err => console.log(err))
const unsubAuth = onAuthStateChanged(auth, user => console.log('user status changed:', user));
const unsubButton = document.querySelector('.unsub');
unsubButton.addEventListener('click', () => {
console.log('unsubscribing');
unsubCol();
unsubCol2();
unsubAuth();
});