Module 04

Databases & APIs

Where data lives and how it moves. Without databases, apps forget everything. Without APIs, they can't talk to anyone. Master the architecture of information.

01 What is a Database?

If variables in JavaScript are short-term memory (RAM), a Database is long-term memory (Hard Drive). It persists data even after the server restarts. There are two main families of databases:

Relational (SQL)

Think "Excel Sheets". Data is stored in strict tables with rows and columns. Best for complex, structured data like financial records. (e.g., MySQL, PostgreSQL).

Non-Relational (NoSQL)

Think "JSON Documents". Data is flexible and can have different shapes. Best for rapid growth and unstructured data. (e.g., MongoDB, Firebase).

02 API Fundamentals

API stands for Application Programming Interface. It is a set of rules that allows one piece of software to talk to another. In web development, we mostly use REST APIs.

The Waiter Analogy (Revisited)

The API is the menu and the waiter. The Frontend (Customer) looks at the Menu (Documentation), tells the Waiter (API) what they want, and the Waiter brings it from the Kitchen (Server/Database).

Common API actions (HTTP Methods):

  • GET: Retrieve data (Reading a blog post).
  • POST: Send new data (Creating a user).
  • PUT/PATCH: Update data (Editing a profile).
  • DELETE: Remove data (Deleting a comment).
03 Real-World Flow

Let's visualize updating your profile picture on a social media app:

  1. Frontend: You select a photo. The app converts it to data and sends a POST request to /api/upload.
  2. API Layer: Receives the image, checks if you are logged in, and validates that the file is actually an image.
  3. Database: The server saves the image file to storage and updates your User Record in the database with the new image URL.
  4. Response: The server sends back "200 OK" and the new image URL. The Frontend updates your screen instantly.
04 Industry Best Practices

Handling data comes with great responsibility. Here is the professional mindset:

Never Trust the Client

Always validate data on the backend. Hackers can bypass frontend checks easily.

Sanitize Inputs

Prevent "SQL Injection" attacks where users type code into input fields to steal data.

Scalability

Design your database assuming you will have 1 million users tomorrow. Use indexing for faster searches.

05 External Learning Resources

Master data with these resources:

Module Summary

You've unlocked the vault. Databases give your applications memory, and APIs give them a voice. You now understand the distinction between SQL and NoSQL, and how the frontend and backend perform a synchronized dance via HTTP requests.