Opinion on db fetching
Unanswered
Miniature Pinscher posted this in #help-forum
Miniature PinscherOP
I am stuck between two choices.
1. Write all db queries in server components, using the (cookie) authenticated user’s id in the WHERE statement.
2. Write all db queries in my separate backend that I already have, and then query those endpoints from my server components.
Which one would you choose? The only downside I can think of in the second alternative is that you’d need to create/tweak an endpoint every time you think of a new query/query result structure that you need.
1. Write all db queries in server components, using the (cookie) authenticated user’s id in the WHERE statement.
2. Write all db queries in my separate backend that I already have, and then query those endpoints from my server components.
Which one would you choose? The only downside I can think of in the second alternative is that you’d need to create/tweak an endpoint every time you think of a new query/query result structure that you need.
11 Replies
If you already have a backend, no point having 2 backends. Simplify it and make your normal backend expose endpoints your server components can hit
@Miniature Pinscher I am stuck between two choices.
1. Write all db queries in server components, using the (cookie) authenticated user’s id in the WHERE statement.
2. Write all db queries in my separate backend that I already have, and then query those endpoints from my server components.
Which one would you choose? The only downside I can think of in the second alternative is that you’d need to create/tweak an endpoint every time you think of a new query/query result structure that you need.
Do the first one. Don't do the second one because of: https://nextjs-faq.com/fetch-api-in-rsc
@B33fb0n3 Do the first one. __Don't__ do the second one because of: https://nextjs-faq.com/fetch-api-in-rsc
We shld first check with Op what he means by external backend anyways
So Op, why do you have an external backend? Can you describe what's its purpose and what it does currently
Since our answers will differ wrt how you have your existing backend :)
i think the principle of a single source of truth should be upheld here.
the backend already contains the business logic, that's the source of truth. so use the backend.
query db directly in the server component only if the feature is entirely logically separate from the existing external backend and you would have to create new endpoints in the said backend to support the new features.
the backend already contains the business logic, that's the source of truth. so use the backend.
query db directly in the server component only if the feature is entirely logically separate from the existing external backend and you would have to create new endpoints in the said backend to support the new features.
Miniature PinscherOP
My backend handles signups, logins, auth in general and long living websocket connections as well as regular cron jobs
@B33fb0n3 Do the first one. __Don't__ do the second one because of: https://nextjs-faq.com/fetch-api-in-rsc
Miniature PinscherOP
I think you misunderstood. I said separate backend.
@Miniature Pinscher My backend handles signups, logins, auth in general and long living websocket connections as well as regular cron jobs
So it's different from nextjs right?
Just to like confirm
Probs express or smthn