I have a list of slugs (e.g., slug-d, slug-c, slug-a, slug-b
) that I pass as a parameter in a URL. When the page loads, I have a prototype solution that currently split the list into individual queries for each slug, fetching them separately from the database. I then merge the results into one list, ensuring the original order (slug-d, slug-c, slug-a, slug-b
) is preserved.
While this approach works, it seems to significantly increase workload unit (WU) consumption due to multiple queries.
Some additional context:
- If I query all the slugs at once, the backend returns the data sorted in created date.
- I need to preserve the order of the slugs as passed in the URL.
Is there a more efficient way to:
- Fetch all the data in a single query while ensuring the data is reordered to match the original slug order after fetching?
I’d appreciate any advice, especially if you’ve dealt with a similar scenario!
Demo: Demo Link
Editor: Editor Link
.
Why do you need to do this via slugs in the URL? Would it not be simpler to create 1 object that holds this list?
In it’s simplest form it will just add employees to the list. In a better form you can add list of “added_employees” which have an employee and an index to keep the order and any other details you may need.
No matter what you need to fetch the employee objects to show the name on the screen, so just simplify how you fetch the list.
Thank you for responding.
Indeed it would be easier to just add it into a list, however, I needed to push the list of slugs in the URL so that I could share the link and whoever opens the link can see the same view.
Could you elaborate on how can you add an index to the list of “added_employees”?
Sure let’s say you have an object called a “group”
The group will have a uniqueID (and a slug if you set it, let’s say “group-a”)
(simple version)
This object called group will have a field called employees_added which is a LIST of type “employee” or “user”. So when you want to share the link you are sharing the link to the group, which has all the added employees in there.
www. yoursite .com /view_group /group-a
(better version)
If you understand how that works, then we can take it one step forward if the order is important, or if you want to store any additional data about each Employee in the group.
Now, instead of adding each employee directly to the Group object, we will create a new object which is called “Group_Employee” which will have these fields to start:
- Index (number) which is the position of that employee in the group, (1, 2, etc)
- Employee (a single user/employee)
Then, your Group object will have a list of type “Group_Employees”
So there will be 1 group, with many “Group_Employees” and each group_employee contains the information about a single person with their position number (index).
Thanks, I could roughly get what you are trying to say.
One thing to clarify, is the “Group” / “Group_Employee” a new data type that I would have to create in the database. Or is it something else that I am missing.
Correct, you will create 2 new types.
1 - Group (which you will use the slug to share link to on the page)
2 - Group_Employee (which belongs to the group)
Tip: I would add one more field to Group_Employee which is called Parent_Group which is of type “Group”.
This will help you in the future if you find a bug and you need to figure out what group the employee was supposed to be on.
Thanks for your guidance, that sure was a solution to the situation at hand.
However, can you think of anything that does not involve a new data_type from the database?
Is there a reason you don’t want to create new types?
Editor
Demo
I have did what you said.
As for the reason why I dont want new types
- As seen in the demo, the loading is now slower
- Writing to database takes more WU then querying the database which defeats the purpose of me trying to save on WU
- The database entries will begin to pile up after uses, and if I have to create a backend workflow to clean them up after x amount of time, that is more WU consumed.
- Comparing the demo of version 1 and 2, this one is much more convoluted.
- With your approach, if I share the link, it wouldnt be a snapshot of the selection during that time, let’s say that I changed the “Group” after I shared the link, the content would have also changed as well.
So a non database approach is much preferred.
I tried a few methods using format as text but the issue is that once you “do a search for” to fill in the repeating group of the employees, you have no data point as a reference to sort the list.
You don’t want to use the Data Types, okay, but what about Javascript or Listshifter plugin?
Sure thing, enlighten me on the Javascript solution.
Listshifter plugin probably as a last resort as it is a paid plugin