So, can you do something like this in the schema f...
# orm-help
m
So, can you do something like this in the schema file?
Copy code
env("postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}&sslmode=prefer")
j
I'm not the most knowledgeable, but you definitely can use a string instead of using the env() command.
url = "<<url>>"
However, to my knowledge you can't do string literals in a .schema file.
m
yeah. But i want to keep it dynamic, in the sense of the host will change.
I mean, i have to i will, either define it 2x (eg: once to pass in from my CI pipeline, and one for running locally or whatever. Was just hoping to avoid having to define the connection string in more than one place.
but still learning prisma/nestjs, so maybe a better pattern will become clearer to me
j
Yeah you just described the only method I'm aware of. I'm interested though an would appreciate an update if you find a solution akin to what you're looking for.
r
@Mitchell Amihod 👋 You can build it directly in the application instead of your schema like this 🙂
👀 1
m
@Ryan Thanks. I’ve done that, but the issue i’m, having is at the generate stage… does it not need a valid connection when doing the generation?
r
No.
prisma generate
doesn’t need a valid connection.
m
so this is part of our build pipeline. So i had removed the datasource form the schema, so it failed to generate. // So if I just leave an emoty string or whatnot in the generate, that would be fine? (sorry, lots of newb q’s here 🙂 - trying to wrap my head around the flow)
r
Yeah, as long as the
datasource
is valid, it will generate fine 🙂
So you just need
postgres
in your provider and it will run generate successfully
m
kk cool. i was confused by generate, and my colleague was mentioning some introspection, which implied to me that its looking at the table to generate the client.
thanks, this helps clear it up
r
Introspection is something that you use when you want to connect Prisma to an existing database where you need your
schema.prisma
mapped to your database. Generate will create the types as per your
prisma.schema
and will also install the
@prisma/client
if not present, and this doesn’t require a valid URL 🙂
m
cool. thanks 👍 will read up some more.
💯 1
this helped.
🙌 2
this is just an out of curiosity question: woudl this have worked?
Copy code
datasource db {
  provider = "postgresql"
  url      = env("postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}&sslmode=prefer")
}
or does env only pluck singular env var values?
actually, i guess i can test by removing my dsn init in constructor 😄
r
No this wouldn’t work. Currently there’s only support for passing a single env variable with the entire URL. You can programmatically construct the URL in the
PrismaClient
constructor for now.
m
cool thanks. can confirm i blew it up. :D