Im’ quite new to full stack development (coming from UX design). I’m asking in the direction of software design and database query strategies.
Quick example: Imagine you have a database with booking for a service including customer info and start and end date for a booking. Now there are two components: first is listing customer information for each booking and second component is a visualization of booked period. Would you prefer to make one database query in the parent component and pass the information into the component or rather do a single query within each of these components? One single query for all needed information or rather multiple queries with just the right amount of needed information? I have no experience in any delay issues of one or the other solution. Lets assume there are maximum 500 booking results from a query.
Hope you can help me
I would like to avoid to put to much effort into the wrong architecture. Instead I hope for some assistance to start in the right direction
2
Answers
This is a bit subjective but there are also certain strategies I think are mostly applicable. So I’ll share what I think, but this is my approach so nothing is necessarily best practice or anything.
To answer your question directly, in the context, which is fairly specific. I would without doubt fetch in the parent component and pass to the children.
I see a number of enterprise applications that fetch very granularly which is convenient in a sense and the code can seem quite cool because every component is like self contained unit fetching and doing everything but I don’t think its the best approach. As a user I did’t love the loading state UI on every component on every click etc… and I just don’t think it’s the best approach.
So for your case I would definitely fetch in parent. But beyond that I would actually take it maybe a step further and look at some pattern where I fetch in a Page component, so to speak, and pass all the data to components, in your case, parent, and children. I think this satisfies the kind of need we have as programmers to have consistency across types of code and also allows the queries to be efficient.
Because at the end of the day, you’re still making an http request for each component if you do it per child, and on top of that you’re making a new database query. So one join query will be more efficient. And I’m fairly okay with the idea of not worrying too much about performance until it’s an issue but I also appreciate the concern of not wanting to do much work on some structure you may want to change soon.
Given that, I would say it’s even more important that you consider a little bit splitting your business logic with ui logic, its common in react to just fetch in useEffect or similar but I believe it’s nice to separate the business logic out, so in react its common to do this with custom hooks like
use<PageX>Data
etc.. and on top that, depending on how much you want to abstract the logic, you could also abstract the requests to haveconst fetchXdata = (...args)=>fetch(<url>, ....)
and then call that in your hooks.And just to add, if there comes a point, where to much data is fetched at once, and it’s not needed and not being displayed (which is likely the case as you can only show so much data once, and it’s unlikely you reduce data so much to display it on UI through calculations etc..), you can look into pagination as a strategy, still on the page level. But this will likely become apparent as a need when it’s needed and should fairly straightforward to implement.
Hope it helps, whatever you go with, I think it’s important you come with a solution that you’re comfortable maintaining so if you were to have to refractor almost everything for some reason you wouldn’t be too annoyed and you’re able to happily adapt to the needs of the business/app.
Both options are valid, depending on what you want to optimize.
Frontend or backend?
If you want to have a reasonable and maintainable backend solution, then the best option is to have simpler endpoints in the backend. With simpler endpoints you have simple tests; with simple endpoints you will have simpler SQL queries; with simpler queries you will have more chances to optimize what you need. Optimization in the backend will be much much more easy when you have this simpler architecture. You will have opportunities to categorize your code in bounded contexts, and perhaps in the future extract those bounded contexts into microservices if it’s really needed.
If you want to have a single frontend store, fully completed with all values in a single fetch, super simple frontend, the go for the single http fetch. Easy to test because you know you have all values and you don’t have to handle issues such as race conditions between components, or wait for some component to load. etc.