Next.js Discord

Discord Forum

Too many connection

Answered
Николя posted this in #help-forum
Open in Discord
In short, I’m making my own website on nextjs, for the first time I worked with an apishka and a database, I don’t know how to work with it correctly, so I did + - as I felt. As a result, my folders look like this on the screenshot (anons -> 3 requests: the last one, nex series that will be released, and that’s all! choose/id -> just the current series, for the page with viewing, data -> for all series, just pulls out all episodes up to the current time, hero/name -> pulls out the current hero)

So this is the problem, my database just crashes at a random moment and says too many connections (error 1040, I think), what could be causing this?
And in general, did I write the api entry correctly?
Answered by B33fb0n3
yes, you should and can direclty call the functions inside your server components. To keep everything clean, you can create something like fetcher.ts and inside it you have for example two functions getPatients (return of all patients) and getPatientsByStatus(status: "cold"). Like that you can easily call these functions in your server components and get directly the correct data. No need for api route stuff 🙂
View full answer

8 Replies

@Николя Oh, okay, thank you very much And in general, did I write the api entry correctly? And could you give me some advice how i can close them after i done with query?
I guess your use case for the api is to big to explain it for me. Just keep in mind to fetch directly in server components instead of calling your own api routes 👍

It depends on the framework/database/library how to close a specific connection. For drizzle for example I would try this:
db.select().something().finally(() => client.end())

But I don't think that you are using drizzle... So take a look online on how to close a connection specific for your framework/database/library/...
@B33fb0n3 I guess your use case for the api is to big to explain it for me. Just keep in mind to fetch directly in server components instead of calling your own api routes 👍 It depends on the framework/database/library how to close a specific connection. For drizzle for example I would try this: db.select().something().finally(() => client.end()) But I don't think that you are using drizzle... So take a look online on how to close a connection specific for your framework/database/library/...
Just keep in mind to fetch directly in server components instead of calling your own api routes 👍

Does this mean that I should carry out various operations in server components, and not in api routes? That is, for example, if I have two pages and one database (for example, patients in a hospital). On one page I need to display all the patients, on the other those who have an example of a cold. For this I need one API routes, but should I calculate it in the component itself? Thank you so much for your help, sorry for the weird questions, I just really need to know right now
@Николя Just keep in mind to fetch directly in server components instead of calling your own api routes 👍 Does this mean that I should carry out various operations in server components, and not in api routes? That is, for example, if I have two pages and one database (for example, patients in a hospital). On one page I need to display all the patients, on the other those who have an example of a cold. For this I need one API routes, but should I calculate it in the component itself? Thank you so much for your help, sorry for the weird questions, I just really need to know right now
yes, you should and can direclty call the functions inside your server components. To keep everything clean, you can create something like fetcher.ts and inside it you have for example two functions getPatients (return of all patients) and getPatientsByStatus(status: "cold"). Like that you can easily call these functions in your server components and get directly the correct data. No need for api route stuff 🙂
Answer
happy to help