samedi 25 mars 2017

Best practices: Should a query rely on existing client-side loaded data?

In the context of a debate I'm having with a coworker:

Let's say we're loading categories on a certain page. Then there's a search function that allows you to load a category's items on the same page.

Since you're not on the category/slug page when you search (i.e., you haven't click into a category), you need to tell the user which category the items belong to by displaying the category name of the item.

The category items query already has a join that loads category Ids on the item (join is on the item_category_item table). But you'd need to perform a 2nd join on the item_category table to then retrieve the category name by Id and return it with the category items.

So, which is the better approach?

A) We're pulling the categories already (with name and ID) on category view page load. When we search for category items, the items are returned with the category Id. We can then pair the already loaded categories by Id, and add the category name attribute to the items. This saves an extra join on the category items query.

Or

B) Add the 2nd join to the category items search query so we can return the category items with the category name.

My thought is, a query shouldn't rely on what's been loaded/not loaded on the front-end. It should load the stuff it needs for the query in question (even if an additional join is required).

AKA, even though X has already been loaded at some point to the page, we shouldn't need to retroactively add attributes to objects based on existing information because what happens if the client-side data that it's relying on changes or is removed?

Aucun commentaire:

Enregistrer un commentaire