Next.js Discord

Discord Forum

Understanding ORM - where to put SQL? (resources needed)

Unanswered
Red-billed Tropicbird posted this in #help-forum
Open in Discord
Red-billed TropicbirdOP
Hi,
I have been learning development and have now an okay-ish of different concepts isolated.
I can write basic MySQL/PostgreSQL database code by itself, and also isolated Next.js/React/TypeScript code.

My struggle comes when I want to combine these concepts - i.e. the frontend and database. I believe the correct term for this is ORM. More specifically, the confusion is where one should write SQL and where one should write React code. The confusion is not about how to use an ORM, but once everything is set up and connected - how do I know if I should write some SQL command instead of React code, and vice versa?

Other than doing projects and building them - which I am doing - aren't there some books or courses that discuss this more readily? I get that I can learn System Design, but often system design is too abstract and doesn't deal with exact SQL code vs React Code (or TS... etc)

3 Replies

@Philippine Crocodile I would say once you have your ORM setup e.g. Prisma or Drizzle, just use their abstractions to query your database, so you are no longer writing actual SQL
Red-billed TropicbirdOP
Yes, I would do that. But the problem is still to know when to write the SQL/abstractions and when to write the actual React code.

For example, I believe fetching millions of users through React code would be horrible to do. One would probably do this with SQL directly.
Philippine Crocodile
Assuming you are using the new App router, I would fetch the data you need as close to the UI that is going to consume that data. So for example if you have a user table, you could create a <Users /> component and fetch the data in that component. You would also want to paginate your db query so that you do not over fetch.