Skip to the workspace
Pebellslearn by building
The Server Half  ›  Lesson

CRUD from Button to Row

Checkpoint — Simple CRUD Apps 15 minEach create, read, update or delete action maps to one HTTP method, one route and one SQL statement, with a matching success status, and every statement on a single record also matches its owner.

full stack / CRUD from Button to Row

Explore each scene at your own pace.

01 · The rule

The rule

CRUD from Button to Row

Almost every screen in Sprout does one of four things to a plant: add it, show it, change it or remove it. Those four are known as CRUD: create, read, update, delete. Each one has a matching HTTP method, route and SQL statement, and once you see the pattern, most of an app's plumbing becomes predictable.

The rule

One user action: one method, one route, one SQL statement.

User action Method and route SQL Success
Add a plant POST /api/plants INSERT ... RETURNING * 201 and the new plant
See my plants GET /api/plants SELECT ... WHERE owner_id = $1 200 and an array
See one plant GET /api/plants/:id SELECT ... WHERE id = $1 AND owner_id = $2 200, or 404
Change a plant PATCH /api/plants/:id UPDATE ... WHERE id = $1 AND owner_id = $2 RETURNING * 200 and the plant, or 404
Remove a plant DELETE /api/plants/:id DELETE ... WHERE id = $1 AND owner_id = $2 204 and no body, or 404

Every statement that touches one plant also matches the owner. That single AND owner_id = ... is what stops one user from reading or changing another's plants; Section 4 looks at it closely.

Here is the update route in full:

app.patch('/api/plants/:id', async (req, res) => {
  const { rows } = await pool.query(
    'UPDATE plants SET name = $1 WHERE id = $2 AND owner_id = $3 RETURNING *',
    [req.body.name, req.params.id, req.user.id]
  );
  if (rows.length === 0) return res.status(404).json({ error: 'Plant not found' });
  res.json(rows[0]);
});

If no row matched, the plant either does not exist or is not this user's, and either way the answer is 404. Otherwise the updated row goes back to the client, which swaps it into its list.

PATCH changes some fields and leaves the rest alone. Its cousin PUT replaces the whole plant, so a PUT without water_every_days would be asking to erase that field. For editing a form of a few fields, PATCH is usually what you mean.

On the client, the same row of the table drives the code: after a successful DELETE /api/plants/7, remove plant 7 from state; after a successful PATCH, replace plant 7 with the server's version.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.