codex-lv4-may-2025

Level Navigation: 1 2 3 (4ℹ️) (5ℹ️) 6 7 8 9 10 11 12 13 14⚡ 15⚡ (16ℹ️) 17 18 19 20 21 22 23 24 25 26⚡ 27⚡ 28⚡ 29 30 31 32 33 34 35 36 37 38 39⚡ 40⚡

Level 16: Manage Data — Create and Persist

The next series of levels introduces working with data through the classic CRUD (Create, Read, Update, Delete) operations. These levels break the CRUD flow into small, repeatable steps that build on each other, allowing you to iterate confidently. Instead of starting with a full SQL database like Supabase’s Postgres, these levels use simple in-memory arrays (declared as module-level globals) so you can focus on understanding route behavior and data manipulation patterns without the complexity of database connections and persistence concerns.

Why Start with In-Memory Data?

When learning to build APIs, it’s common to start with in-memory storage before introducing database complexity. This approach allows you to:

Digging Deeper: In-Memory Arrays as Module-Level Globals

What is a module-level global?

In Node.js, when you declare a variable at the top level of a file (outside of any function), it becomes a module-level variable. This variable is shared across all code within that module (file), and any route handler in that file can access and modify it.

Example:

// src/index.js
import express from 'express';

const app = express();

// This is a module-level global array
// It's declared outside any function, at the "top level" of the module
const items = [
  {name: "gum", brand: "Bubalishous"},
  {name: "toothpaste", brand: "Colgate"},
  {name: "bike", brand: "Huffy"}

];

app.get('/items', (req, res) => {
  // Route handlers can access the items array
  res.json(items);
});

app.post('/items', (req, res) => {
  // Route handlers can modify the items array
  items.push(req.body);
  res.status(201).json(req.body);
});

Why use module-level globals for in-memory data?

  1. Persistence during server session: As long as your server is running, data in the array persists. Multiple requests can read and write to the same array.
  2. Simple and straightforward: No need for complex setup—just declare an array and start using it.

Important limitations to understand:

What comes next:

The upcoming levels will cover how to:

  1. Create, read, update, and delete data using in-memory arrays
  2. Validate and structure your data properly
  3. Handle errors and edge cases
  4. Later, connect to Supabase (PostgreSQL) and use the same patterns with persistent, production-ready storage

Think of in-memory arrays as a training ground—once you understand how to manipulate data with arrays, moving to a database is mostly about learning the syntax, not the concepts.