#creating mysql db for nextjs app with prisma

1 messages · Page 1 of 1 (latest)

slim bridgeBOT
#

Project ID: N/A

woven blaze
#

huh

marsh pollen
#

Hey Brody, I was able to create the database after all. and tried to delete this post.

woven blaze
#

oh well glad you where able to figure that out, feel free to open a new help thread for any future questions

marsh pollen
#

while I have you here, can I use the MYSQL_PRIVATE_URL as DATABASE_URL in my nextjs app and push my prisma schema into the empty db?

woven blaze
#

yes as long as you run the needed command in the context of your railway deployment, as the private url would not be accessible publicly

#

having your app use the private url would also eliminate any database <--> service egress fees!

marsh pollen
#

Did I hear less fees? I like the sound of that! But where would you point a person in my position, prisma schema and nextjs app are ready, I have an empty mysql db on railway, what should i do at this point?

#

any docs on that?

#

but I also want people to access the database publicly , so that private url is not usable

woven blaze
#

the only docs for that would be the prisma docs, as theres nothing too fancy going on with railways mysql database, essentially you would use it the same way you are using planetscale's database

#

you may want to access to database publicly, and you can, but when you deploy your app to railway you should have your app use the private url

marsh pollen
#

my app is on vercel. Can I use your database only, or am I doing it wrong?

woven blaze
#

yes you can still use the database, but if your app is hosted outside of railway it would not be possible to use the private url, you would need to use the public url and would be subjecting yourself to egress fees on the database service

#

what environment variable(s) does your app use to connect to the database?

marsh pollen
#

just a database_url for planetscale

#

egress fees are a big deal? sounds like I should stay with planetscale

woven blaze
#

just 10¢ per GB, isn't planetscale shutting down their free or hobby plan?

marsh pollen
#

yes..that's why I'm moving away

woven blaze
#

and if anything you should bring your app on vercel over to railway!!

marsh pollen
#

nextjs apps run okay on railway?

woven blaze
#

they run great, they probably make for the biggest portion of languages deployed to railway

marsh pollen
#

It would be helpful if you put out a guide on how to do the vercel+planetscale transfer to railway. Many people like me looking for a new home

woven blaze
#

you have a good point

#

in the mean time, I can always help you step by step

marsh pollen
#

Can you give me a checklist of what to do? Since I have a built app already, the template app you have is confusing. I have my project on github and push it to vercel

woven blaze
#

it's quite simple, with railway, you have your app on github (check) and push to github (check) then railway does the deploying, you'd just have to do the initial setup with railway, and once done you will be amazed on how easy it is

#

wanna begin?

marsh pollen
#

sure

woven blaze
#

can you send a screenshot of your current railway project, the one with the database

marsh pollen
#

oh, I only created a database there. Shouldn't I create sth else?

woven blaze
#

see, you know what's up. I just needed to see where we were at.

go ahead and add a new empty service from that + New button

marsh pollen
#

done!salute

woven blaze
#

open it up and add all your needed environment variables into it's service variables

marsh pollen
#

that'll take some time. I don't want to keep you waiting. What should i do after adding the env vars?

woven blaze
#

I'm kinda making it up as I go along from experience

#

may I ask why it would take a long time? lots of variables?

marsh pollen
#

no, I guess I can do it in a min or two

woven blaze
#

what did you say your app's stack was? next?

marsh pollen
#

nextjs react (prisma trpc Clerk planetscale)

#

I added most of my env vars

woven blaze
#

do you have the database url variable added?

marsh pollen
#

you said it's private. so should I?

#

The database should be accessible to the public via the app

#

I can add the planetscale one for now

woven blaze
#

for right now we would want to use the public database url for mysql on railway

marsh pollen
#

oh MYSQL_URL then.

#

but the app will throw an error not finding the tables in it

woven blaze
#

the variable would be DATABASE_URL=${{MySQL.MYSQL_URL}} assuming the variable you use in code is DATABASE_URL

woven blaze
marsh pollen
#

When I switched to get the db url, all the env vars in the service are gone. there was no save button.

#

have to put them back in

woven blaze
#

where you using the raw editor?

marsh pollen
#

yeah, I know what happened now.

#

ok. all good now

woven blaze
#

didnt click the update variables button?

marsh pollen
#

yes, never mind. I thought that form was just for entry, but the editor data is supposed to persist and I deleted them when adding the db url

woven blaze
#

ah gotcha

woven blaze
marsh pollen
#

like this:?
MYSQL_URL=blahblah
DATABASE_URL=${{MySQL.MYSQL_URL}}

#

or just the second line

woven blaze
#

just the second line, i would guess you arent using MYSQL_URL in your app, so you wouldnt need it in the service variables

marsh pollen
#

ok. done.

woven blaze
#

deploy the changes

#

then go back into the service and give it a nice name, and generate yourself a domain

marsh pollen
#

name done, but generate a domain how

woven blaze
#

from its settings page

marsh pollen
#

ah done

woven blaze
#

can i see another screenshot of the project?

marsh pollen
#

with the railway.internal

woven blaze
#

that is not applicable for a next app in this context

#

quick question, how much data would you say you have in your ps database?

marsh pollen
#

it's basically empty except for a table with 4300 rows and 5 cols that I created using a seed

woven blaze
#

haha im not sure if i would call that empty

#

what command did you run to create the tables and add the seed data?

marsh pollen
#

as soon as I set it up prisma should be able to see it

#

seed it

#

in my schema.prisma I have:
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}

generator client {
provider = "prisma-client-js"
seed = "ts-node prisma/seed.ts"

}

#

and that seed.ts is executed to seed the db

woven blaze
#

right but you would need to run a command to migrate (make the tables) and seed the database, its unfortunately not gonna happen magically

marsh pollen
#

yes, as i showed above it's a seed script that is run by prisma that gets the data from a .csv file

woven blaze
#

but what command do you run to start the migration and seed?

#

like npx prisma migrate deploy for example

marsh pollen
#

oh, that!

#

let me see...

#

npx prisma db seed

woven blaze
#

okay cool, thats what i was looking for

marsh pollen
#

done.

woven blaze
#

then make sure you are on the latest with railway --version latest being 3.5.2 iirc

marsh pollen
#

it is 3.5.2

#

no iirc

woven blaze
#

perfect

marsh pollen
#

don't know what iirc means

woven blaze
#

if i remember correctly

marsh pollen
#

pardon my ignorance!

woven blaze
#

no worries at all

#

you have a local .env file right?

marsh pollen
#

yes

woven blaze
#

for now, can you remove the DATABASE_URL line from it

marsh pollen
#

done

woven blaze
#

we will have the railway cli inject the new database url when we run migrations and seeds

#

we also dont want to really be storing database credentials in a plaintext file

#

run railway login if you havent already

#

and then run railway link in a terminal open inside of your apps project folder, follow the steps to link to your cooli service

marsh pollen
#

ok

woven blaze
#

let me know when thats done!

marsh pollen
#

sorry, on it.

#

got an error. have to do it with --browserless

#

is that ok?

woven blaze
#

yeah, but what error did you get?

marsh pollen
#

never mind. I should select my service as the service right? not the database..

#

for linking

woven blaze
#

correct

marsh pollen
#

ok. seems like linking was done

woven blaze
#

are you in your apps project folder? the same folder you are pushing to github

marsh pollen
#

yes

woven blaze
#

and what command did you say you use to run migrations?

marsh pollen
#

my vercel app is connected to github, so to update the website, I just push to github. I don't do any migrations. sorry if I'm misunderstanding. I'm in business not web dev.

woven blaze
#

im talking in the context of database migrations

marsh pollen
#

npx prisma db push

woven blaze
marsh pollen
#

with db push, I let prisam use the schema to create the db

woven blaze
marsh pollen
#

done

woven blaze
#

did the command go well?

marsh pollen
#

yes

woven blaze
#

and now run your seed command through railway run in the same way

marsh pollen
#

it's running

woven blaze
#

do you own a custom domain thats in use with vercel?

marsh pollen
#

yes

woven blaze
#

who is the domain with?

marsh pollen
#

it's with Google domains. I need your DNS values I guess

woven blaze
#

you would need to use cloudflare's nameservers first

marsh pollen
#

should i create a cloudflare account?

woven blaze
#

yes please, but we can hold off on that if you want, because you can test your app with the railway public domain for now

marsh pollen
#

yes, I'll hold off on that. make sure things run smoothly first

#

but why cloudflare. They have to be my cdn?

woven blaze
#

because they support root level cnames, google domains does not

marsh pollen
#

Are you saying I'll need to transfer my domains too?

#

from Google to Cloudflare

#

?

woven blaze
#

nope, the domain can stay on google domains, you would only need to use cloudflare's nameservers

#

theres a guide for that made by cloudflare

marsh pollen
#

I've used them as a CDN before.

woven blaze
#

then you already have an account right? so thats good!

marsh pollen
#

I see my tables replicated on your end

woven blaze
#

sweet

marsh pollen
#

the seeding is still going on.

#

I need to connect my github project to railway somehow, right?

#

it was connected to vercel before

woven blaze
#

yeah but first, does the service have a domain?

marsh pollen
#

I have added my domain names to vercel if that's what you mean. not sure what you mena

#

mean

woven blaze
#

right now im talking in the context of your railway service

marsh pollen
#

I haven't adde a domain on railway yet

woven blaze
#

do you have railway public domain added to the service?

#

if not, go ahead and generate one, from its settings page

marsh pollen
#

generated. shows 'nothing here yet' when I navigate to it

woven blaze
#

thats normal, there isnt anything there yet

marsh pollen
#

Will i need the private networking or should i disable it

woven blaze
#

you dont need to touch that

#

does the service have all the needed variables for your app?

marsh pollen
#

I believe so./

woven blaze
#

i guess we will soon find out eh?

marsh pollen
#

moment of truth

woven blaze
#

do you have the correct github linked to your railway account?

marsh pollen
#

I don't think so, railway has access to my github but I have not linked the proejct to it yet

woven blaze
#

go into the service settings and connect your repo

marsh pollen
#

connected

woven blaze
#

and it will now deploy

marsh pollen
woven blaze
#

thats normal, nothing is there yet

marsh pollen
#

that's the last bit, I guess. What should i do?

woven blaze
#

wait until the deploy finishes

#

go check the status

marsh pollen
#

and the seed is still ongoing. taking longer than I thought

woven blaze
#

as long as the tables are there lol

marsh pollen
#

It says my project has no deploys. Should I manually do it?

woven blaze
#

have you connected your github repo to the service?

marsh pollen
#

yes. should i do a git push to initiate it?

woven blaze
#

is your stuff still blue?

#

if so, you would need to click the deploy button after you make any changes

marsh pollen
#

just did it. it's building now.

woven blaze
#

awsome

marsh pollen
#

deployment failed. could it be due to the seeding being underway?

#

RROR: failed to solve: process "/bin/bash -ol pipefail -c npm ci" did not complete successfully: exit code: 1

Error: Docker build failed

woven blaze
marsh pollen
#

how? copy the contents of that page and paste here?

woven blaze
#

your lock file is out of sync, i guess vercel doesnt care about that or is fixing it for you.

to fix it properly run npm i --package-lock-only and then push your changes, once you push your changes railway will auto deploy the new code for you

marsh pollen
#

I think I'd better disconnect from vercel too at this point

woven blaze
#

good idea!!

marsh pollen
#

seeding done

woven blaze
#

awsome

marsh pollen
#

new git push triggered deployment. but that package lock line generated this:

#

railway is serverfull right?

woven blaze
#

if serverfull is the opposite of serverless, yes

#

did you push your changes to github?

marsh pollen
#

yes, the new deployment failed

woven blaze
#

new logs please

#

your lock file is still out of sync

marsh pollen
#

you mean the package-lock.json file?

#

and out of sync with what?

woven blaze
#

with your package.json

marsh pollen
#

would it be recreated if i delete the package-lock.json?

#

and run that npm i --package-lock-only

#

?

#

it's not registering any changes. git status shows an up to date branch after I run the above command.

woven blaze
#

are you pushing to the correct branch?

marsh pollen
#

it's always main. yes

#

just pushed again

woven blaze
#

delete your lock file and run that command I gave you again

marsh pollen
#

that regenerated a fresh file finally. pushing now.

#

failed again. log attached.

woven blaze
#

somehow you arent syncing your lock file

marsh pollen
#

what if i delete my node_modules folder and do an npm install?

woven blaze
#

instead lets just tell railway to not care about that.
add this nixpacks.toml file to your project.

[phases.install]
  cmds = ['npm install']
marsh pollen
#

so I create a file named nixpacks.toml and add this to it in my root directory?

woven blaze
#

yes

marsh pollen
#

new deploy building

#

no failure 3 min in. still deploying

woven blaze
#

good sign

marsh pollen
#

yeah. no error. deployed.

#

database working

#

I guess all is set ! wow! I can't believe it!

#

I can't thank you enough

woven blaze
#

one last step

#

now that you have ran migrations and done the seeding, we want to switch your app over to use the private url for the database

#

your DATABASE_URL service variable should now be ${{MySQL.MYSQL_PRIVATE_URL}}

marsh pollen
#

oh, ok

woven blaze
#

one you change that, click deploy and let ne know if anything fails

marsh pollen
#

You mentioned sth about not keeping the .env values in a text file. What's the alternative? I have added it to my gitignore though.

#

and the new deploy failed:

woven blaze
#

okay i think i know what to do for this error

woven blaze
#

add a new service variable DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}} but this time hold your alt key while clicking the deploy button

#

holding the alt key stops it from doing a new deployment, since we dont want a new deployment just yet

marsh pollen
#

done

woven blaze
marsh pollen
#

I have these now:
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}

woven blaze
#

perfect

visual cosmos
#

Hun

#

A

marsh pollen
#

Hi

visual cosmos
#

I have these now:
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}

visual cosmos
marsh pollen
#

Brody had to leave?

woven blaze
#

do you two know each other?

marsh pollen
#

No, he just joined the chat!

woven blaze
#

uh strange

marsh pollen
#

I thought he replace you

woven blaze
#

i dont know them

#

i dont know why they repeated something you said either, sorry about that

marsh pollen
#

Isn't he a member of railway support?

woven blaze
#

nope

marsh pollen
#

his role is "support access"

woven blaze
#

that just means they they can chat in #1006629907067064482 forums

#

same as you

#

and for transparency i dont work for railway

marsh pollen
#

you are not railway support either, right?

woven blaze
#

correct

marsh pollen
#

thanks for your help man. you were saying

woven blaze
#

always happy to help

marsh pollen
#

yes

#

DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}

seems equivalent to what we had earlier:

DATABASE_URL=${{MySQL.MYSQL_URL}}

woven blaze
#

okay can you show me your new shema.prisma file

marsh pollen
#

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}

generator client {
provider = "prisma-client-js"
seed = "ts-node prisma/seed.ts" //shouldn't this be .js(mar 15, 24) No!!! it worked as-is in deploying to railway

}

marsh pollen
#

I thought you were talking about the env variables in railway settings. made the change in the schema file now

woven blaze
#

lets see the new schema file please

marsh pollen
#

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
directUrl = env("DATABASE_URL_DIRECT") //added for railway
relationMode = "prisma"
}

generator client {
provider = "prisma-client-js"
seed = "ts-node prisma/seed.ts" //shouldn't this be .js(mar 15, 24) No!!! it worked as-is in deploying to railway

}

woven blaze
#

looks good, push the changes

marsh pollen
#

should i have these in my railway env vars?
DATABASE_URL=${{MySQL.MYSQL_PRIVATE_URL}}
DATABASE_URL_DIRECT=${{MySQL.MYSQL_URL}}

#

I misunderstood you, but you were ok with these.

woven blaze
#

yeah, those are what should be in the service variables

marsh pollen
#

but isn't this circumventing the private_url? when we add the public one?

woven blaze
#

from your schema.prisma file -

  url          = env("DATABASE_URL")
  directUrl    = env("DATABASE_URL_DIRECT") //added for railway

the directUrl is used for generation, migrations, and seeding and such, but url will be used for all other in app database calls

marsh pollen
#

it failed this time:

woven blaze
#

can you remove the postinstall script in your package.json?

marsh pollen
#

deploying after making this change

woven blaze
#

it seems like the architecture of your app prevents you from using the private url since the private network is not available during build

#

you will have to go back to using the public url

#
DATABASE_URL=${{MySQL.MYSQL_URL}}
marsh pollen
#

ah. ok. I reversed the changes.

woven blaze
#

i know railway but i unfortunately dont have enough experience with prisma to be able to advise on the needed changes you would have to make so that the public url is used at build and the private url is used during runtime

marsh pollen
#

no worries. I'll look into that.

woven blaze
#

sounds good

marsh pollen
#

about the .env file, you said there's a better way of storing that information?

#

I didn't understand why, cause the .env file is in the dev environment only.

woven blaze
#

do you have different stuff in there as opposed to what you have in railway?

marsh pollen
#

no, it's the same stuff, used for dev.

#

localhost

woven blaze
#

if thats true, then you can get rid of the local .env and use railway run <your command here> as that will run the given command with all your railway service variables now available to the command you ran

#

its better because everything is stored securely in railway instead of a plaintext file

marsh pollen
#

Thank you for all your help. I'm in a comfortable place to figure out the rest of the details.

woven blaze
#

awsome, but dont feel shy if you have any more questions

#

i assume your latest deployment is working again?

marsh pollen
#

yes

woven blaze
#

great!

marsh pollen
#

Hey, I got you some coffee. not much but a small token of appreciation. I hadn't realized you weren't railway support.

woven blaze
#

wow thank you so much, that means a lot ❤️

marsh pollen
#

Have a good one.

woven blaze
#

you too!!

marsh pollen
#

Hi @woven blaze About that custom domain with cloudflare, I added the two DNS records from cloudflare to my Google Domains DNS list and then added the CNAME @ root value from railway to cloudflare with a Full DNS setup. Am I missing something 'cause it's not working.

woven blaze
#

show me what you have put into your nameservers in Google domains please

marsh pollen
#

Here they are @woven blaze

woven blaze
#

yeah that looks good, what is the status of your custom domain in your railway service settings?

marsh pollen
#

Record not yet detected

woven blaze
#

okay, show me your dns settings in cloudflare

#

can i see the public networking section of your railway service too

#

hmmm, something is not quite right with your dns

marsh pollen
#

yeah. after refreshing, this is what I'm seeing. so my setup must have been done correctly:

#

Thank you. I guess I'll have to deal with cloudflare now. Can you please delete the last image you shared, the less publicly shared the better.

woven blaze
#

no problem

marsh pollen
#

should I use the same address from railway for this?

woven blaze
#

railway doesnt have anything to do mail servers

marsh pollen
#

my www address resolves now. I changed my custom domain to www.blahblah.com but blahblah.com is not resolving. Do I have to upgrade my railway plan or add a redirect rule on cloudflare?

woven blaze
#

show me your current cloudflare dns settings please

marsh pollen
woven blaze
#

and your domains in railway

marsh pollen
woven blaze
#

you dont have the non www domain there

marsh pollen
#

seems like I can either have www or the one without. I have to upgrade for more.

woven blaze
#

do you need a www domain?

#

its 2024 and www is a thing of the past for most cases

marsh pollen
#

I understand, but it's expected.

woven blaze
#

i would have to disagree with that

#

but to have two domains you would need to be on hobby

marsh pollen
#

isn't the trial plan same as the hobby plan?

#

I'll get the hobby anyway.

woven blaze
#

nope, different names, different plans

marsh pollen
#

realistically, If 50 people are using the website, what kind of bill should i be expecting? I access the website without any data in it and spent 10c so far. It seems to me I need to get back to planetscale and accept their new plan.

woven blaze
#

billing isnt based on how many users your site has, its based on how much resources your services use

marsh pollen
#

more users = more resources, right? bandwidth , database calls, etc

woven blaze
#

that is true, there is a lose correlation

#

it looks like planetscale's cheapest plan is now 39$ a month, your next app and mysql database should never come near 39$ with light to moderate usage

marsh pollen
#

I'll give it a go anyway. Will see how things evolve.

woven blaze
#

whats the current estimated costs say?

marsh pollen
#

not sure. I'd better start using it and decide later. I already am on hobby plan.

woven blaze
marsh pollen
#

great! thanks. Can this name be changed?