Cloud Databases
What if you could build an app that shares high scores with friends across the world, instantly? Today, you'll learn the secret: connecting your app to a brain in the cloud!
Ever played a game and worried about losing your score if you got a new phone? Poof! All that progress, gone! π¨ Unless... you save it in the cloud! Apps that only save data on the phone are stuck there. To let our data travel, we use Cloud Databases.
Pick a pizza! π
π Pan's Pro Tip: The Filing Cabinet in the Sky
Think of a Cloud Database as a giant, magical filing cabinet in the sky. When your app needs to save something important, it sends a paper airplane π up to the cabinet where it's kept safe and can be grabbed from any other device!
π§ Brain Game! (Part 1)
If you save your game score to a Cloud Database, where is it *really* stored?
π§ Brain Game! (Part 2)
What is the BIGGEST advantage of using a cloud database for a multiplayer game?
π·οΈ Build-a-Profile: Let's Make a Player Tag!
That "filing cabinet" doesn't store paper; it stores data in a special format that looks like a list of secrets, called JSON (JavaScript Object Notation). Itβs how computers organize information with `key: value` pairs. Let's build one right now!
Type in the boxes below and watch the Player Tag update live!
Databases like Firebase that store data this way are often called NoSQL or document databases. Instead of rigid tables, they use flexible 'documents'βjust like the Player Tag you're building!
That Player Tag you just built? The 'Vibe' you're about to send to our live database is a simple JSON object, just like that! It's the language of the cloud. Every time you click "Share My Vibe" below, you're creating a little JSON file like this:
{
"text": "Feeling creative! π¨",
"timestamp": "a special cloud time"
}
π Live Challenge: Let's Build a Vibe Board... Together!
Time to use a real cloud database! Below is our team's shared Vibe Board, powered by Firebase. Add your current vibe and watch as messages from other TomorrowHub explorers appear in real-time. This isn't a simulation!
Live Vibe Board:
π§ββοΈ Curious about the magic spell? Click to peek!
That feels like magic, right? It's just two key pieces of code working together. Let's look at the spellbook:
// 1. SEND: We find the 'vibes' collection and .add() a new JSON object.
db.collection('vibes').add({
text: vibeText, // Your message
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
// 2. LISTEN: .onSnapshot() is a magic ear that listens for ANY change in the 'vibes' collection and runs our code to update the screen.
db.collection('vibes').onSnapshot((snapshot) => {
// Code to display all the vibes goes here...
});
π‘οΈ Safety Check: Be a Data Guardian!
Our Vibe Board is anonymous for a reason! We are never saving personal info like your full name or email. When you build apps, think like a "Data Guardian." Your job is to protect your users' information. Always ask: "Do I really need this information?" before you save it.
π Advanced Challenge: Add an Upvote Feature!
Ready to level up? We've remixed the Vibe Board code on Glitch. Your mission is to add an "upvote" button next to each vibe, so people can vote for their favorites. This is what real developers do: add new features to existing code!
- Fork the Glitch project to make your own copy.
- In the `script.js` file, find the `addVibe()` function. Inside the `.add({...})` part, add a new line: `upvotes: 0,` to the data being saved.
- In `index.html`, find where the vibes are displayed. Next to the vibe text, add a `
π€ Level Up Your Thinking: Data Modeling
Think like a product designer. Our 'vibe' object now has `text`, a `timestamp`, and `upvotes`. What other key:value pairs could you add to make the Vibe Board cooler? What about a `username`? Or an `emojiIcon`? Planning your data structure like this is called data modeling.
π¨βπ©βπ§ Parent Corner: From Our Screen to Yours
Your child's creation doesn't have to stay on their computer! This is a great time to share their work with family and friends safely, without using a public App Store. Tools like Thunkable let you generate a simple web link for an app, and Apple's TestFlight lets you invite family members to test a more advanced app.
This is also a perfect opportunity to discuss data privacy. A great conversation starter: "If we shared your app with Grandma, what info would be safe to show her (like a game score), and what should we always keep private (like our last name)?"