Documenting learning on how to reduce the db queries on the server #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The goal…
Query the DB for only the IDs of an object (or any other subset of columns) to optimize the data "cost".
Here are a few things I learned – or at least my current state of know how.
select
option on the tanstackuseQuery
hook. That would give type save return types.getProject
query has a zod and a types component. Not clear how to best specify the zod part. Not clear how to remove the redundancy.I looked into how Tanstack query would handle the return types. There is good docs like https://tkdodo.eu/blog/react-query-and-type-script#the-four-generics. BUT, my understanding is now that the blitz useQuery uses Tanstack useQuery internally but exposes its own hook under the same name with a completely different signature (WHY…). https://blitzjs.com/docs/query-usage — I asked in Discord and might PR a docs update to clarify.
Update: This is the blitz > tanstack wrapper code https://github.com/blitz-js/blitz/blob/main/packages/blitz-rpc/src/data-client/react-query.tsx
We can optimize on different layer
Conclusion
Right now, it looks like we either duplicate the
getProject
file and create specific files per query, likegetProjectId
.Or, we just pay the data "cost" and optimize later…