Next.js Discord

Discord Forum

mysql

Answered
Blue whiting posted this in #help-forum
Open in Discord
Blue whitingOP
I'm using next.js 14 with the app router. I'm wanting to generate dynamic routes and trying to figure out the best way to do so. I have a mysql database that I'd like to use to generate my routes based on IDs. However, I'm not sure if I can create these routes using server actions or if I'd need to setup an API?
Answered by aardani
What i meant to say is that you can use this in generateStaticParams directly.
View full answer

84 Replies

@aardani can you elaborate more on your issue?
Blue whitingOP
I believe I want dynamic routes that are generated when I build the site.

I have a mysql database with hundreds of product IDs, I want to have a page for each of these products and I think it makes more sense to generate them when building the site. The data on these pages won't change often, but there will be some data points that I'd like to refresh every hour.
ah
then what you're looking for is ISR, a feature of SSG (static site generation)
Blue whitingOP
I've made some progress on the issue.. I was able to setup app/api/products/route.js, and that displays the json data for my product IDs... I'm thinking I can use this to generate the pages statically
you need to set up generateStaticParams()
then return a length of array that you want that route to be available at build time
this is where you check the DB and get the IDs
Blue whitingOP
Ah, great. I was looking at that, but I was getting stuck here, since I didn't have a URL to provide until a few minutes ago:

const posts = await fetch('https://.../posts').then((res) => res.json())
Although I dont know if the app/api/products/ is the best way to do this? I was reading about server actions that can interact with the database without an api? Is that an option?
if its a db to mysql, dont it have a driver to connect directly to your db? or is mysql on remote cloud service?
@Blue whiting Although I dont know if the app/api/products/ is the best way to do this? I was reading about server actions that can interact with the database without an api? Is that an option?
server actions are only used for runtime server mutation, not build time nor request time. so its irrelevant in this case
It's a azure hosted mysql server. There's a connector I'm using that connects and I can query data I want
So to use generateStaticParams, it sounds like I'm pretty close now that I have my api url working?
yeah?
the API is not Next.js Route Handler right?
Blue whitingOP
Yea, it is. Is that a problem? Should I be doing it differently?
Next.js server is not run at build time so you will be hitting an empty endpoint
hence, just fetch to the db directly in generateStaticParams
follow the "dont fetch your own Next.js server in the server" rule
Blue whitingOP
Yea, that'd be my preferred method
ok so its more like const posts = await db.posts.get(...)
in your generateStaticParams
and then map it into the desired array type (check the docs)
and
if you need to refresh it,
Blue whitingOP
hmm okay. I couldn't find anything in the docs about grabbing the data from a database
everything was API based and I don't know how to use a database connection instead
yeah but who provides the API? is it your database provider or Next.js?
fetch() is only preferred if its fetching to external domain/outside of Next.js application
Blue whitingOP
I don't have an API, just my database
allright so you just have Next.js, and Database
does your database has a domain, username, password, port and database name?
Blue whitingOP
Yup, I have all of that
I suggest getting prisma
as an interface between Next.js and your Database
you can set it to MySQL
Blue whitingOP
Interesting, okay. I've heard of it.
then provide environment variable DATABASE_URL to your MySQL database
Blue whitingOP
As of now, I have this function which I confirmed works:
well
Blue whitingOP
But I'm open to prisma if that'll make my life easier
@Blue whiting As of now, I have this function which I confirmed works:
What i meant to say is that you can use this in generateStaticParams directly.
Answer
Data Fetching in Next.js only concerns about whether you should fetch or not. If you use fetch, it better be for fetching to external endpoint.

But if you dont use fetch and have like an in built library to connect to Data sources then use that instead of fetch.
And use that directly in any server environment like generateStaticParams
Blue whitingOP
I'm pretty new to this, how can I know what options I have other than fetch? Can I somehow run the query function I have instead?
@Blue whiting I'm pretty new to this, how can I know what options I have other than fetch? Can I somehow run the query function I have instead?
Othe rthan fetch, its up to the user what they use to conncect to db. In your case its that query function
Or use prisma to make data querying easier
Its whatever library the user uses
Thats why i said you can use the query function directly in generateStaticParams
Or any server components
Blue whitingOP
How can I do that? I haven't found any documentation on running sql on server components
I'm fine with writing sql queries, it's the routing/apis/etc that is making my head spin 😛
GenerateStaticParams is already on the server, you dont need to create any api or routes. Think of coding in Node.js server
Its already on the server
Server Component are also already run in the server
Blue whitingOP
Gotcha gotcha.
so did you finally realize something after me telling you 5 times of the same thing :v
Blue whitingOP
Lol, it makes sense at a high level. But I'm really bad with javascript. Now that I know it's possible I should be able to figure it out 🙂
Now I gotta figure out how to import the query function and call it correctly
I really appreciate your time, though. You've helped a lot
@Blue whiting Lol, it makes sense at a high level. But I'm really bad with javascript. Now that I know it's possible I should be able to figure it out 🙂
Its not javascript, you should be thinking of the architecture level, yeah basically at a high level
Blue whitingOP
errr, typescript.
My point still stands
I meant to say, think where the code is run
Blue whitingOP
Right, that makes sense now that you explained it
I get stuck on things like this:
I'm trying to figure out why I can't import this function
did you export it?
oh
Blue whitingOP
export async function query({ query, values = [] }) {

Yup, it's exported
it has to be await query
solve for the first error first
Blue whitingOP
oh my lord, that was it
you export a named export query so it has to be import { query }
import abcde is when you export default something but the default export will be renamed to abcde
Blue whitingOP
ahh, I never knew that
Thanks so much!
you're welcome! if you have any question feel free open up a new forum posts