Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting learning on how to reduce the db queries on the server #10

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

tordans
Copy link
Contributor

@tordans tordans commented Feb 2, 2023

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.

  1. Tanstack query has a select option on the tanstack useQuery hook. That would give type save return types.
    • BUT, it would only work in the Frontend and we want to reduce the select on our DB level, not just the frontend after all data was transfered
    • BUT, we actually don't use tanstack query (more below) direclty
  2. Passing select to the 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.
  3. We actually need to research this stuff on the Prisma level. The prisma call is the one that is responsible to receive the select option and thus to specify the return type.
    • TODO maybe more research needed; the documented takes are all hacky/no nice

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

  1. the db query – this would be optimal; minimal data queried and transferred
  2. what we return from the server function – this would be good, the db is not optimized but the client receives minimal data
  3. what we return from useQuery in our frontend; this is what Tanstack query would do (if we could use it). However, that is not really a optimization since the data "cost" is already paid.

Conclusion

Right now, it looks like we either duplicate the getProject file and create specific files per query, like getProjectId.
Or, we just pay the data "cost" and optimize later…

- getProject is "green"
- BUT the return type of getProject is {} as soon as I add the select to the prisma call
- BUT the call with select in NavigationProject is "red" on the type level
Extending the zod "type" with select:any gets the call in NavigationProject "green"
It was suggested in discord to use passthrough() for zod, which allows other params then those specified, but that did not work for my type checking in NavigationProject
This take tries to have a clear switch based on the scope param. However, even with "as" types the function does not return the right type for the default case. It is always the Pick id-type.
According to https://tkdodo.eu/blog/react-query-and-type-script#the-four-generics the type signature for useQuery has 4 params, the third is the data.

BUT, it looks like our blitz useQuery has a completely different signature that tanstack useQuery. (Question asked in Discord)

We can still pass the return type and force the right response like this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant