Basis Data & Fungsi Awan
Gunakan basis data waktu nyata, seperti Firestore, dan terapkan fungsi awan nirserver untuk logika backend khusus.
Basis Data & Fungsi Awan adalah pelajaran Indie Hacker Mobile Apps gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Indie Hacker Mobile Apps, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Indie Hacker Mobile Apps mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Cloud Databases Unveiled
Welcome! Today, we'll explore cloud databases and serverless functions, essential tools for scalable mobile apps. They provide robust, managed backends without the hassle of server maintenance.
We'll focus on Firestore, a popular choice for real-time data, and Cloud Functions for custom backend logic.
Firestore's Data Model
Cloud Firestore is a flexible, scalable NoSQL document database. Unlike traditional relational databases with tables and rows, Firestore organizes data into collections and documents.
Each document contains key-value pairs, known as fields. Documents live within collections, and collections can even contain subcollections.
Structuring Your App Data
Understanding collections and documents is key to structuring your app's data efficiently. Imagine a simple e-commerce app:
users(collection)user_alice_id(document)name: "Alice"email: "alice@example.com"products(collection)product_xyz_id(document)name: "Cool Gadget"price: 99.99
Adding Data with Ease
Adding data to Firestore is straightforward. You can use add() to let Firestore generate a unique document ID, or set() to specify your own ID or overwrite an existing document.
Here's how to add a new user to a 'users' collection:
db.collection("users").add({
name: "Bob",
email: "bob@example.com",
status: "active"
})
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
console.error("Error adding document: ", error);
});Fetching Data on Demand
To retrieve a document or a collection of documents once, you use the get() method. This is useful for data that doesn't require constant, real-time updates.
Let's fetch a specific user's data using their document ID:
db.collection("users").doc("bob_doc_id").get()
.then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
console.log("No such document!");
}
})
.catch((error) => {
console.error("Error getting document:", error);
});Keeping Data Live with Snapshots
Firestore's real-time capabilities are powerful. By using onSnapshot(), you can listen for changes to documents or collections. Whenever data changes on the backend, your app receives instant updates.
This snippet demonstrates listening to a single document for real-time changes:
db.collection("products").doc("prod_xyz").onSnapshot((doc) => {
if (doc.exists) {
console.log("Current product data:", doc.data());
} else {
console.log("Product removed or never existed!");
}
});
// This listener keeps your app updated automaticallyServerless Logic with Functions
Cloud Functions are serverless pieces of code that run in response to specific events. They let you execute custom backend logic without needing to manage or provision any servers.
They're perfect for extending your BaaS (like Firebase) with custom features, security rules, or integrations.
What Makes a Function Run?
Cloud Functions are 'event-driven,' meaning they only run when a specific event occurs. These events are called triggers. Common types include:
- HTTP triggers: For creating API endpoints.
- Firestore triggers: React to document creation, updates, or deletions.
- Authentication triggers: Respond to user sign-ups or deletions.
- Scheduled triggers: Run functions at specific times (e.g., daily cleanup).
Your First HTTP Cloud Function
Let's create a simple Cloud Function that responds to an HTTP request. This is like building a tiny, serverless API endpoint. It's a great way to handle custom logic that your mobile app might need.
This example will return a basic "Hello from Firebase!" message.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloHttp = functions.https.onRequest((request, response) => {
// Set CORS headers for web access, if needed
response.set('Access-Control-Allow-Origin', '*');
response.set('Access-Control-Allow-Methods', 'GET, POST');
response.status(200).send("Hello from Firebase!");
});Automating with Functions + DB
Cloud Functions and Firestore are a powerful combination. Functions can automatically react to changes in your Firestore database, allowing you to:
- Update aggregate counts (e.g., total likes on a post).
- Perform data validation or sanitization.
- Send notifications or emails after a user action.
This keeps your app's logic robust and efficient, handling backend tasks seamlessly.
Cloud Logic Check
Test your understanding of cloud databases and serverless functions.
Databases & Functions Summary
You've explored the core concepts of Cloud Firestore for scalable, real-time data storage using a flexible NoSQL document model. You also learned about Cloud Functions, which enable you to deploy serverless backend logic in response to various triggers.
Mastering these tools is fundamental for building dynamic, robust, and efficient indie mobile apps!
Belajar Indie Hacker Mobile Apps dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Basis Data & Fungsi Awan” gratis?
Ya — teks lengkap “Basis Data & Fungsi Awan” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Indie Hacker Mobile Apps, upgrade ke CoddyKit PRO. Kursus Indie Hacker Mobile Apps mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Basis Data & Fungsi Awan”?
Gunakan basis data waktu nyata, seperti Firestore, dan terapkan fungsi awan nirserver untuk logika backend khusus. Kamu berlatih Indie Hacker Mobile Apps dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Indie Hacker Mobile Apps?
Tidak diperlukan pengalaman sebelumnya. Indie Hacker Mobile Apps di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “Basis Data & Fungsi Awan” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Indie Hacker Mobile Apps ini?
Ya. Setiap pelajaran Indie Hacker Mobile Apps menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengantar Platform BaaS
- Autentikasi & Keamanan Pengguna
- Basis Data & Fungsi Awan
- Data Waktu Nyata dan Notifikasi Dorong dengan BaaS