Project structure and plan
Unanswered
Richard Evanson posted this in #help-forum
I am making a website and I have all pages already set up with some functionalities and components but now I need to connect mySql database. I decided to use Prisma and I have connnected desting database but I am not sure how to structure everything. Do I need api? if so where do I store them. I really dont know what to do next this is my file structure
17 Replies
Cape horse mackerel
the
api folder must go in the /appCape horse mackerel
now let's say you want to fetch a data for one of the users.
You can do smth like
Or you can try to replace APIs with server components/actions https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating
You can do smth like
// app/api/users/[userId]/route.ts|js
export async function GET(req: Request, { params }: { params: { userId: string } }){
try {
const user = await prisma.users.findUnique({
where: {
id: userId
}
});
return NextResponse.json(user, { status: 200 });
} catch(error) {
return NextRepsonse.json('internal server error', { status: 500 }
}
}Or you can try to replace APIs with server components/actions https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating
so server components are better?
but where would I store them then
Cape horse mackerel
whatever suits you, you can use only API route handlers
so these components act similar to sql queries? like in your example it would just find user with same id and thats it?
I have never done anything with api/server componenents so it is still confusing to me
Cape horse mackerel
yes, basically prisma doing sql queries behind the scenes
Blood cockle
Hi! The new NextJS tutorial as a all section on structure, Fetching Data, APIs and Server Components https://nextjs.org/learn/dashboard-app/fetching-data
so I managed to get some test variant working but now I wonder
so for every time of data request I will need to make new component?
Because when I made few components for my website like table componenent i made it very universal where you input collumn names, and functionalities so it generates according to that
Blood cockle
You don’t need to create new components for every request. I think the model used in that tutorial is very good. You create a data fetch component and export a function for each query. Then feed those queries in a table component, for example. Not sure if that’s your actual doubt. Cheers
I am not gonna use components but actions. Still I think I will make one file called actions and just make functions in it like you suggested
And I think I can make them somewhat universal
So I would have function called delete and I would give table name and ID of entry to delete entry in database
@Richard Evanson And I think I can make them somewhat universal
Blood cockle
Seems to be a good way to go ðŸ‘