Swap the in-memory store for a MySQL reviews table (S5)
2026-10-17
Note
This is the session where the app becomes persistent. Everything you have built so far — the React review UI (Session 3) and the two review routes (Session 4) — reads from an in‑memory store that vanishes on every restart. Today the data source swaps from that in‑memory store to a real MySQL reviews table. The frontend and the route signatures are UNCHANGED — the only thing that moves is where the data comes from: an in‑memory Map → the reviews table. The route handlers, their paths, and their { data, meta } / { data } envelopes stay identical, so the React UI now renders DB‑backed reviews with no frontend change.
Map store. Today you make it persistent: a real MySQL reviews table — and you wire the full capstone (React → Express → MySQL).reviews table (8 fields + product_id); (3) seed it with the reviews; (4) swap the in‑memory helpers for pool.query; (5) confirm the frontend now renders DB‑backed reviews with no frontend change.The table adds a product_id join column (a foreign key) but the API still exposes the 8‑field shape; the route handlers, paths, and envelopes are unchanged — only the helper bodies change.
The app already talks to MySQL through a connection pool in backend/src/db.js. You do not invent a new one — you reuse the pool the app already creates, exactly the way the existing catalog routes (GET /api/products, GET /api/categories) already do. The review routes will use this same pool.
// backend/src/db.js — a mysql2/promise connection POOL
import mysql from 'mysql2/promise';
import dotenv from 'dotenv';
dotenv.config();
const pool = mysql.createPool({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT ? Number(process.env.DB_PORT) : 3306,
user: process.env.DB_USER || 'appuser',
password: process.env.DB_PASSWORD || 'apppassword',
database: process.env.DB_NAME || 'minimal_app_db',
waitForConnections: true,
connectionLimit: 10,
connectTimeout: 10000, // 10 seconds
});
export default pool;Pool vs a single connection. Opening a MySQL connection is expensive: a TCP handshake plus authentication on every request. Creating a new connection per request wastes that time and quickly exhausts the server’s connection limit. A pool keeps a small set of reusable, already‑open connections: a request acquires one, runs its query, and releases it; the next request reuses it. That is what connectionLimit bounds and what waitForConnections queues.
Why a pool matters here. The catalog already runs concurrent pool.query calls (list + pagination + LEFT JOIN) on this same pool without opening a fresh connection per hit. The review routes simply join that same pool — no new connection logic, no new file:
pool.query resolves to an array whose first element is the rows (for a SELECT). The ? placeholders are filled from the second argument — that is parameterization, and it is what keeps you safe from SQL injection.
One rule decides whether the SQL you write today is safe: values travel as parameters, never inside the query string. The practice session demands it in every task and names the violation first in its mistake table — an interpolated WHERE product_id = ${id} lets a crafted URL value change the query’s meaning.
// ❌ interpolation — the value is part of the SQL text
const [bad] = await pool.query(`SELECT … FROM reviews WHERE product_id = ${productId}`);
// ✅ placeholder — the value travels separately, in the second argument
const [ok] = await pool.query('SELECT … FROM reviews WHERE product_id = ?', [productId]);The canonical treatment — payloads and defenses — lives in webdev_04_L_NodeExpress.qmd. Today, only the habit: ? + values array.
The reviews table stores the same 8 review fields the API and frontend expose, plus one product_id column that links each review to its product. This is the DDL from the course guide, reused verbatim:
CREATE TABLE reviews (
review_id VARCHAR(64) PRIMARY KEY,
product_id INT NOT NULL, -- FK -> products.id (join detail, not part of the 8-field shape)
source VARCHAR(32) NOT NULL, -- Amazon | BestBuy | Walmart
author VARCHAR(100) NOT NULL,
rating INT NOT NULL CHECK (rating BETWEEN 1 AND 5),
title VARCHAR(255),
body TEXT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
verified_purchase BOOLEAN NOT NULL DEFAULT FALSE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
);
-- Index for the dominant query pattern: "all reviews for product X" (WHERE product_id = ?)
CREATE INDEX idx_reviews_product ON reviews(product_id);Read ||--o{ as a crow’s-foot: one product has zero or more reviews (and a review belongs to exactly one product). The text block is the schema at a glance: the products ||--o{ reviews relationship followed by each table’s typed column list, with the PK and the FK marked.
product_id is a join column, not a review fieldWhy product_id is a JOIN column, not a review field. The 8 review fields describe the review itself (review_id, source, author, rating, title, body, created_at, verified_purchase). The extra product_id does not describe the review — it links the review to the product it belongs to, so the review routes can run WHERE product_id = ?. That is exactly why GET /api/products/:id/reviews can filter by the product in the URL. Because product_id is a join detail and not part of the review’s data shape, the SELECT in GET /:id/reviews returns the 8 fields — not product_id.
(The rating CHECK keeps the 1–5 integer contract; ON DELETE CASCADE means deleting a product removes its reviews; the index serves the WHERE product_id = ? pattern.)
The practice session never asks you to paste this DDL into a mysql> prompt: you append it to db/init.sql — the migration/seed script the MySQL container runs on first start — after the catalog tables and seed. That file is this course’s migration: schema and seed as versioned SQL, committed with the code, applied in order.
Why not run it by hand once? A hand-run schema lives only inside one running container — a fresh clone or rebuilt volume comes up without the reviews table, and the review routes fail on a missing table while the catalog stays green. A committed db/init.sql is reproducible and reviewable: every environment runs the same file in the same order — the idea behind “migrations”, without the tooling machinery.
product_idSeeding is how the reviews table gets its first rows. The data is the same reviews the in-memory store (backend/src/data/reviews.js) held in Session 4 — you port every entry of that file, so the exact row count is whatever your S4 store defines. The only difference is that each row now also carries its product_id (the join column that the in-memory Map key expressed implicitly). Once these rows are in MySQL, the data persists across restarts: stop the Node process, start it again, and the reviews are still there, because they live in the database, not in a Map.
The seed can live in a db seed file (a .sql script run during setup, alongside the catalog seed) or a one-off script you run once. The key point is not where it runs — it is that the data now persists in MySQL across restarts.
-- Representative seed rows: reviews ported from the S4 in-memory store,
-- shown with the extra product_id column (port every entry your S4 store holds).
INSERT INTO reviews
(review_id, product_id, source, author, rating, title, body, created_at, verified_purchase)
VALUES
('AMZ_0001', 1, 'Amazon', 'J. Rivera', 4, 'Great value', 'Solid build, ships fast.', '2026-08-20 09:30:00', TRUE),
('BB_0002', 1, 'BestBuy', 'M. Chen', 5, 'Love it', 'Battery lasts as advertised.', '2026-08-21 14:10:00', TRUE),
('WM_0003', 1, 'Walmart', 'P. Okafor', 3, 'Okay', 'Does the job, packaging was thin.', '2026-08-22 08:00:00', FALSE),
('AMZ_0010', 2, 'Amazon', 'S. Diallo', 2, 'Underwhelming', 'Smaller than expected.', '2026-08-23 11:45:00', TRUE),
('WM_0020', 3, 'Walmart', 'L. Fontaine', 4, 'Good for the price', 'Works well, feels solid.', '2026-08-24 16:20:00', TRUE);The column list is the 8 review fields plus product_id — exactly the table shape from the DDL above. These rows are illustrative of the shape: port the actual values from your backend/src/data/reviews.js, keeping each entry’s string review_id (e.g. AMZ_0001) and using the Map key it lived under as the product_id.
This is the whole session in one move: replace the body of each in-memory helper with a pool.query call. The function names, the returned shape, and the routes do not change — only where the data comes from.
getProductReviews — same name, new bodygetProductReviews — before (S4, in-memory) vs after (S5, MySQL). Same name, same returned shape (an array of 8-field objects); only the data source changes:
// S4 — in-memory (what the store gave you)
function getProductReviews(productId) {
return reviewsByProduct.get(String(productId)) ?? [];
}
// S5 — MySQL (the ONLY thing that changed is the body + `async`)
async function getProductReviews(productId) {
const [rows] = await pool.query(
'SELECT review_id, source, author, rating, title, body, created_at, verified_purchase FROM reviews WHERE product_id = ?',
[productId]
);
return rows;
}Two things to notice: (1) the function is now async, because pool.query returns a promise; (2) the SELECT returns the 8 fields — product_id is in the WHERE clause (the filter), not in the SELECT list (the shape). That is the join detail staying out of the data shape.
aggregateProductReviews — identical loop, DB-backedaggregateProductReviews — becomes async too. It fetches the rows with the same SELECT, then runs the identical histogram / per_source / overall_avg / count logic over them — the exact S4 loop, now DB-backed:
async function aggregateProductReviews(productId) {
const [rows] = await pool.query(
'SELECT review_id, source, author, rating, title, body, created_at, verified_purchase FROM reviews WHERE product_id = ?',
[productId]
);
const reviews = rows;
if (reviews.length === 0) {
return { overall_avg: null, per_source: {}, histogram: { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 }, count: 0 };
}
const histogram = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 };
const bySource = {};
let sum = 0;
for (const r of reviews) {
sum += r.rating;
histogram[r.rating] += 1;
(bySource[r.source] ??= { sum: 0, count: 0 });
bySource[r.source].sum += r.rating;
bySource[r.source].count += 1;
}
const per_source = Object.fromEntries(
Object.entries(bySource).map(([src, v]) => [src, Number((v.sum / v.count).toFixed(2))])
);
return {
overall_avg: Number((sum / reviews.length).toFixed(2)),
per_source,
histogram,
count: reviews.length,
};
}The empty case is preserved: when a product has no rows, reviews.length === 0 short-circuits to overall_avg: null, per_source: {} (empty), histogram all zeros, and count: 0 — exactly the S4 empty result, now sourced from an empty SELECT.
awaitThe route handler — a one-line change. Because the helpers are now async, the call site gains an await. That is the only diff in the handler — the 404-early check and the res.json envelope are untouched:
// GET /api/products/:id/reviews — only the marked line changed (an added `await`)
router.get('/:id/reviews', async (req, res, next) => {
try {
const [productRows] = await pool.query('SELECT id FROM products WHERE id = ?', [req.params.id]);
if (productRows.length === 0) {
return res.status(404).json({ error: 'Product not found' });
}
const data = await getProductReviews(req.params.id); // ← the only change: added `await`
res.json({ data, meta: { count: data.length } });
} catch (err) {
next(err);
}
});(The /:id/aggregate handler changes the same way: res.json({ data: await aggregateProductReviews(req.params.id) }).)
Note
Nothing on the frontend changes. The route paths (GET /:id/reviews, GET /:id/aggregate), the 404-early logic, and the { data, meta } / { data } envelopes are byte-identical to Session 4. The React UI renders exactly the same JSON it always did — it has simply started arriving from MySQL instead of an in-memory Map.
product_id with no matching row in products fails the FK — the INSERT is rejected, the review is not stored. The example seed assumes products 1, 2, 3 exist; porting your S4 store onto a different catalog bites here first.db/init.sql has run and every read fails on a nonexistent reviews table while the catalog stays healthy — hence the practice checkpoint.pool.query rejects, the handler’s catch forwards to next(err), and the request fails through the S4 error path — an error, not reviews.connectTimeout: 10000: a brand-new pooled connection gets 10 s for its handshake, so a dead host fails in about 10 s instead of hanging the request indefinitely.With the swap in place, the 18 h capstone is wired end-to-end. Trace the path one request takes:
ReviewList, ReviewStats, SourceBreakdown) call GET /api/products/:id/reviews and GET /api/products/:id/aggregate through VITE_BACKEND_URL.await.pool.query reads the reviews table with WHERE product_id = ? and returns the 8-field rows.{ data, meta } / { data } envelopes; the React UI renders DB-backed reviews with NO frontend change.That is the whole story: React → Express → MySQL → Express → React, with the data shape unchanged at every boundary.
Verification checklist (run these before you call the capstone done):
# 1. The list route — data now comes from the reviews table
curl http://localhost:4000/api/products/1/reviews
# → { data: [ …8-field reviews… ], meta: { count: N } } # N = the rows seeded for product 1
# 2. The aggregate route — same SELECT, DB-backed stats
curl http://localhost:4000/api/products/1/aggregate
# → { data: { overall_avg, per_source, histogram, count } }
# 3. Prove it is MySQL, not memory: change a row, re-curl, see it move
# (WM_0003 is one of the example seed rows above — substitute an id from your own seed)
mysql -u appuser -p minimal_app_db \
-e "UPDATE reviews SET rating = 5 WHERE review_id = 'WM_0003';"
curl http://localhost:4000/api/products/1/reviews # rating for WM_0003 is now 5
# 4. Confirm the UI renders it: open the product page on :5173
# → the review list + histogram + per-source table render the DB-backed rowsIf step 3 moves the value through the API and the UI, the data source is genuinely MySQL — the in-memory store is gone.
Honest gaps: things the capstone touches implicitly but this session does not teach. ### Not in the 18 hours
| Gap | What S5 does instead |
|---|---|
| N+1 queries | Every route reads one product (WHERE product_id = ?); a page showing reviews for many products issues one query per product. No batch or cross-product endpoint exists or is taught. |
| Transactions | Each review query is a single SELECT, run on its own. Nothing is atomic across statements — a multi-row fetch-and-insert (home stretch) can half-succeed; no ROLLBACK path is built here. |
| SQL-side aggregation | aggregateProductReviews pulls the rows and computes histogram, per_source and overall_avg in JavaScript. GROUP BY / AVG / COUNT would do the same work in MySQL; not taught here. |
| Gap | Where it waits |
|---|---|
| Migration tooling | The course applies committed SQL (db/init.sql) on first start. Versioned, ordered, per-environment runners are beyond this course. |
POST /:id/fetch |
On-demand scraping is the home stretch — deliberately not part of the 18 h in-book capstone. |
backend/src/db.js); each request acquires, queries, and releases a pooled connection.reviews table = the 8 fields + a product_id join column (FK): the table stores the canonical 8-field review plus product_id (FK → products.id) so the routes can filter.product_id is a join detail that is dropped from the SELECT: it lives in the WHERE clause, never in the returned 8-field shape.await), not the routes or the frontend: getProductReviews / aggregateProductReviews get a pool.query body; the route handlers gain a single await.{ data, meta } / { data } envelopes are the stable contract: lists return { data, meta }, the aggregate returns { data } — unchanged from S4.The 18 h capstone is now complete and persistent: the reviews table, the review routes, and the React review UI are wired end-to-end, and the data survives a restart. The home stretch adds the piece that was deliberately left unwired in-book — on-demand backfill, using the scraper-service/ shipped in fullstack-minimal-app. That folder is a small standalone mock review source: it listens on http://localhost:5000 and answers GET http://localhost:5000/api/scrape/reviews/:productId with generated reviews for the product id. It is not part of the Express app — the app core is catalog-only plus this separate service — and nothing calls it yet; you run it on its own.
POST /:id/fetch — fetch, normalize, persistThe app-side piece you build is POST /api/products/:id/fetch: it calls the scraper-service for that product, normalizes each returned review into the canonical 8-field schema (string review_id such as AMZ_0001, source one of Amazon | BestBuy | Walmart, author, integer rating 1–5, title, body, created_at, verified_purchase), inserts the rows into the reviews table with the URL’s product_id, and returns a clear result — how many reviews were fetched and stored. Because the mock regenerates its reviews on every call, the fetch handler needs a collision strategy for the review_id primary key: either generate fresh unique string ids per fetch, or use INSERT IGNORE / ON DUPLICATE KEY UPDATE.
Once it is wired, the “live” story begins: instead of only seeding, the app fetches on demand, normalizes, and persists into MySQL — and the same two GET routes serve the new rows with no frontend change.
E. Bruno - Database Integration — Lecture