Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Update and Delete Entries

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 Points

Is it better to have less calls to the database or more readable syntax?

Here Guil and I had different approaches to updating a row. I don't have a lot of knowledge about time and space complexity, but I am under the impression that less queries to a database are better (to a point, depending on how complicated your query is and how it is structured, as well as how complex your db is). Is the complexity the same on these or is one greater, and how would that affect us in larger databases? (this database has less than 10 entries right now)

Guil's code:

router.post(
  "/:id/edit",
  asyncHandler(async (req, res) => {
    const article = await Article.findByPk(req.params.id);
    await article.update(req.body);
    res.redirect("/articles/" + article.id);
  })
);

My code

router.post(
  "/:id/edit",
  asyncHandler(async (req, res) => {
    const { id } = req.params;
    const article = await Article.update(req.body, { where: { id } });
    res.redirect("/articles/" + id);
  })
);

1 Answer

If you have logging selected you will see the raw SQL query which is executed on the database. You can try the 2 different code structures to see where the differences are.