Write a data function that asks the database only for what the tea list needs, and returns only those fields
The tea list page passes what getTeaCards() returns to TeaCards, a Client Component, so all of it is written into the page for the browser. Write getTeaCards() in lib/tea-cards.ts, the one place pages get tea cards from. The starter reads the whole table and hands over whole rows.
Ask findTeas from @/lib/db for the listed teas only (listed: true), and only for the columns the cards need, in one query; don't filter or trim rows after reading them all.
Return one card for each, in the database's order, with exactly four fields, { slug, name, price, inStock }: price in dollars (priceCents: 550 is 5.5), and inStock true when stock is above 0. Nothing else may leave the function: not the cost, the supplier's email, the exact stock, or any column added to the table later.
lib/tea-cards.tsimport "server-only";
import { findTeas } from "@/lib/db";
export type TeaCard = { slug: string; name: string; price: number; inStock: boolean };
export async function getTeaCards() {
const teas = await findTeas();
return teas.filter((tea) => tea.listed);
}
lib/db.ts (read-only)export type TeaRow = {
id: number;
slug: string;
name: string;
description: string;
priceCents: number;
costCents: number;
stock: number;
supplierEmail: string;
listed: boolean;
};
/** The teas table. The tests change it. */
export const teaRows: TeaRow[] = [
{ id: 1, slug: "green", name: "Green tea", description: "Steamed within hours of picking.", priceCents: 400, costCents: 150, stock: 30, supplierEmail: "orders@misty-peak.example", listed: true },
{ id: 2, slug: "mint", name: "Mint tea", description: "Peppermint leaves, nothing else.", priceCents: 350, costCents: 120, stock: 0, supplierEmail: "sales@green-valley.example", listed: true },
{ id: 3, slug: "pu-erh", name: "Pu-erh", description: "Aged for five years.", priceCents: 1200, costCents: 700, stock: 4, supplierEmail: "orders@misty-peak.example", listed: false },
{ id: 4, slug: "oolong", name: "Oolong tea", description: "Rolled by hand and roasted twice.", priceCents: 550, costCents: 210, stock: 3, supplierEmail: "sales@green-valley.example", listed: true },
];
export type TeaQuery<K extends keyof TeaRow> = {
/** Only rows whose columns equal these values. */
where?: Partial<TeaRow>;
/** Only these columns; every column when left out. */
select?: K[];
};
/** Every query the database was asked, in order. */
export const queries: TeaQuery<keyof TeaRow>[] = [];
/** The teas that match `where`, in the table's order, each with only the `select`ed columns. */
export async function findTeas<K extends keyof TeaRow = keyof TeaRow>(query: TeaQuery<K> = {}): Promise<Pick<TeaRow, K>[]> {
queries.push(query);
const { where = {}, select } = query;
return teaRows
.filter((row) => Object.entries(where).every(([column, value]) => row[column as keyof TeaRow] === value))
.map((row) => (select ? Object.fromEntries(select.map((column) => [column, row[column]])) : { ...row }) as Pick<TeaRow, K>);
}
app/teas/tea-cards.tsx (read-only)"use client";
import { useState } from "react";
import type { TeaCard } from "@/lib/tea-cards";
export default function TeaCards({ cards }: { cards: TeaCard[] }) {
const [saved, setSaved] = useState<string[]>([]);
const toggle = (slug: string) => setSaved((current) => (current.includes(slug) ? current.filter((item) => item !== slug) : [...current, slug]));
return (
<ul>
{cards.map((card) => (
<li key={card.slug}>
<a href={`/teas/${card.slug}`}>{card.name}</a>
<p>
${card.price.toFixed(2)}, {card.inStock ? "in stock" : "sold out"}
</p>
<button type="button" aria-pressed={saved.includes(card.slug)} onClick={() => toggle(card.slug)}>
Save {card.name}
</button>
</li>
))}
</ul>
);
}
app/teas/page.tsx (read-only)import { Suspense } from "react";
import { getTeaCards } from "@/lib/tea-cards";
import TeaCards from "./tea-cards";
async function TeaList() {
const cards = await getTeaCards();
return <TeaCards cards={cards} />;
}
export default function TeasPage() {
return (
<main>
<h1>Our teas</h1>
<Suspense fallback={<p>Loading teas…</p>}>
<TeaList />
</Suspense>
</main>
);
}
- returns a card with exactly four fields for each listed tea
- asks the database once, for listed teas and only the columns the cards need
- keeps a column added to the table later out of the cards
- gives the tea list page what its cards show
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…