Created
February 25, 2025 20:42
-
-
Save dulimarta/aac56b8d6be33b81c690b8c4c65906d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type State = { | |
// abbreviation: string | |
name: string, | |
capital: string, | |
population: number, | |
nationalParks: Array<string> | |
} | |
export type City = { | |
name: string, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the functions you need from the SDKs you need | |
import { initializeApp } from "firebase/app"; | |
// TODO: Add SDKs for Firebase products that you want to use | |
// https://firebase.google.com/docs/web/setup#available-libraries | |
import { cs371_demo_config } from "./my_fb_config"; | |
// Initialize Firebase | |
const app = initializeApp(cs371_demo_config); | |
console.info("My Firebase Web App", app); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { initializeApp } from "firebase/app"; | |
import { cs371_demo_config } from "./my_fb_config"; | |
import { State } from "./00-types"; | |
import { | |
addDoc, | |
collection, | |
doc, | |
getFirestore, | |
setDoc, | |
} from "firebase/firestore"; | |
// Initialize Firebase (Do this only once through your web app) | |
const app = initializeApp(cs371_demo_config); | |
const db = getFirestore(); | |
const mich: State = { | |
name: "Michigan", | |
population: 8_000_000, | |
capital: "Lansing", | |
nationalParks: ["Pictured Rocks National Lakeshore", "Hoffmaster State Park"], | |
}; | |
const flor: State = { | |
name: "Florida", | |
population: 20_200_000, | |
capital: "Tallahassee", | |
nationalParks: ["Everglades National Park"], | |
}; | |
// Option #1: Add a new document to a (new or existing) collection | |
// Side Effect: this always creates a new document every time you | |
// run this code | |
const stateColl = collection(db, "states"); | |
const docRef = await addDoc(stateColl, mich); | |
console.debug("A new document added", docRef.id); | |
// Option #2: Set the content of a new (or existing) document | |
// Side Effect: this will create or overwrite the document | |
const michDocRef = doc(db, "states/MI"); | |
const miOut = await setDoc(michDocRef, mich); | |
const florDocRef = doc(db, "states/FL"); | |
await setDoc(florDocRef, flor); | |
console.debug("Done adding/overwriting a (new) document"); | |
// Don't call process.exit() in a real application! | |
process.exit(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { initializeApp } from "firebase/app"; | |
import { cs371_demo_config } from "./my_fb_config"; | |
import { State } from "./00-types"; | |
import { | |
collection, | |
doc, | |
getDoc, | |
getDocs, | |
getFirestore, | |
} from "firebase/firestore"; | |
// Initialize Firebase (Do this only once through your web app) | |
const app = initializeApp(cs371_demo_config); | |
const db = getFirestore(); | |
// Option #1: Read a specific document | |
// requires the DocID: "MI" in the snippet below | |
const michDocRef = doc(db, "states/MI"); | |
const miSnapshot = await getDoc(michDocRef); | |
if (miSnapshot.exists()) { | |
const miState = miSnapshot.data() as State; | |
console.debug("Retrieved document ID", miSnapshot.id); | |
console.debug("Doc content", miState); | |
} | |
// Option #2: Fetch all the documents in a collection | |
const stateColl = collection(db, "states"); | |
const querySnapshot = await getDocs(stateColl); | |
querySnapshot.forEach((qds) => { | |
if (qds.exists()) { | |
const aState = qds.data() as State; | |
console.debug(`DocID: ${qds.id} Name: ${aState.name}`); | |
} | |
}); | |
// Without a forced exit, NodeJS waits too long | |
process.exit(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { initializeApp } from "firebase/app"; | |
import { cs371_demo_config } from "./my_fb_config"; | |
import { | |
arrayUnion, | |
doc, | |
getFirestore, | |
increment, | |
setDoc, | |
updateDoc, | |
} from "firebase/firestore"; | |
// Initialize Firebase (Do this only once through your web app) | |
const app = initializeApp(cs371_demo_config); | |
const db = getFirestore(); | |
// Option #1: Update a specific field in an existing document | |
// requires the DocID: "MI" in the snippet below | |
const michDocRef = doc(db, "states/MI"); | |
await updateDoc(michDocRef, { | |
population: 10_500_000, | |
}); | |
// Option #1a: Increment numeric fields | |
await updateDoc(michDocRef, { | |
population: increment(999), | |
}); | |
// Option #1b: Append new value to an array field | |
await updateDoc(michDocRef, { | |
// Use arrayRemove to remove an item from an array field | |
nationalParks: arrayUnion( | |
"Pictured Rocks Natl. Park", | |
"HoffMaster State Park" | |
), | |
}); | |
// Option #2: Add a new field(s) to an existing document | |
await setDoc( | |
michDocRef, | |
{ | |
area: 96_716, // sq-miles | |
nickName: "Wolverine State", | |
}, | |
{ merge: true } | |
); | |
// Without a forced exit, NodeJS waits too long | |
process.exit(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { initializeApp } from "firebase/app"; | |
import { cs371_demo_config } from "./my_fb_config"; | |
import { | |
arrayRemove, | |
arrayUnion, | |
collection, | |
deleteDoc, | |
deleteField, | |
doc, | |
getDocs, | |
getFirestore, | |
QueryDocumentSnapshot, | |
QuerySnapshot, | |
setDoc, | |
updateDoc, | |
} from "firebase/firestore"; | |
// Initialize Firebase (Do this only once through your web app) | |
const app = initializeApp(cs371_demo_config); | |
const db = getFirestore(); | |
// Option #1: Delete a specific document | |
// requires the DocID: "MI" in the snippet below | |
const michDocRef = doc(db, "states/MI"); | |
await deleteDoc(michDocRef); | |
// Option #2: Delete a specific field | |
const flDoc = doc(db, "states/FL"); | |
await updateDoc(flDoc, { | |
population: deleteField(), | |
}); | |
// Option #3: Delete an item from an array field | |
await updateDoc(flDoc, { | |
// Use arrayRemove to remove an item from an array field | |
nationalParks: arrayRemove("Everglades National Park"), | |
}); | |
// Option #4: Delete ALL documents in a collection | |
const stateColl = collection(db, "states"); | |
const qs = await getDocs(stateColl); | |
// Iterate over each document | |
qs.forEach(async (qd: QueryDocumentSnapshot) => { | |
console.info("Attempt to delete", qd.id); | |
// deleteDoc(doc(stateColl, qd.id)); | |
}); | |
// Cannot call process.exit(), otherwise the removal is incomplete | |
// process.exit(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const cs371_demo_config = { | |
apiKey: "AIzaSyAPznyattumDBIhy2fHtgqq5J0gqTHlW1Y", | |
authDomain: "cs371-demo.firebaseapp.com", | |
databaseURL: "https://cs371-demo.firebaseio.com", | |
projectId: "cs371-demo", | |
storageBucket: "cs371-demo.appspot.com", | |
messagingSenderId: "80758575596", | |
appId: "1:80758575596:web:2ca3294ca7dc68a17830c9", | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment