R
Railway11mo ago
Caillou

Custom environment variables on build process

Since migrating to Postgres 2.0, I have been trying to connect to Railway's internal network in Prisma, but there is a problem that during the build process the private address cannot be accessed, which does not allow Prisma to generate the modules according to the database. Is there any way to define environment variables to be used exclusively in build mode?
9 Replies
Caillou
CaillouOP11mo ago
N/A
Brody
Brody11mo ago
both variables are going to be available at build, but it would be up to your code to connect to the public url to the do migrations/models and then connect to the private url for everything else during runtime, or just don't connect to the database at all during build and only during runtime
Caillou
CaillouOP11mo ago
That's the question, does railway support any package like dotenv-cli? The only way to do this is this, since Prisma takes it directly from the .env
Brody
Brody11mo ago
thats absolutely not the only way to do this, configure your code to use the public url during build and the private during runtime
Caillou
CaillouOP11mo ago
So is it impossible to use Prisma on Railway using private networks? Since the compilation environment uses NODE_ENV == 'production' there is no way to do this directly within the code.
Brody
Brody11mo ago
i have seen people do this before, i have not done this before, might be best to do some research on this topic
Caillou
CaillouOP11mo ago
import { PrismaClient } from '@prisma/client';

let prisma: PrismaClient;
let hasFailed;

if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient({
datasources: {
db: {
url: hasFailed ? 'postgresql://xxxxx:[email protected]:0000/railway' : 'postgresql://xxxxx:[email protected]:0000/railway',
},
},
});

if(hasFailed == undefined){
prisma.$queryRaw`SELECT 1`.then(x => hasFailed = false).catch(x => {
console.log("Using external IP Address");
hasFailed = true;
prisma = new PrismaClient({
datasources: {
db: {
url: 'postgresql://xxxxx:[email protected]:0000/railway',
},
},
});
})
}
} else {
let globalWithPrisma = global as typeof globalThis & {
prisma: PrismaClient;
};
if (!globalWithPrisma.prisma) {
globalWithPrisma.prisma = new PrismaClient({
log: ['warn', 'error', 'query'],
datasources: {
db: {
url: 'postgresql://xxxxx:[email protected]:0000/railway',
},
},
});
}
prisma = globalWithPrisma.prisma;
}

export default prisma;
import { PrismaClient } from '@prisma/client';

let prisma: PrismaClient;
let hasFailed;

if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient({
datasources: {
db: {
url: hasFailed ? 'postgresql://xxxxx:[email protected]:0000/railway' : 'postgresql://xxxxx:[email protected]:0000/railway',
},
},
});

if(hasFailed == undefined){
prisma.$queryRaw`SELECT 1`.then(x => hasFailed = false).catch(x => {
console.log("Using external IP Address");
hasFailed = true;
prisma = new PrismaClient({
datasources: {
db: {
url: 'postgresql://xxxxx:[email protected]:0000/railway',
},
},
});
})
}
} else {
let globalWithPrisma = global as typeof globalThis & {
prisma: PrismaClient;
};
if (!globalWithPrisma.prisma) {
globalWithPrisma.prisma = new PrismaClient({
log: ['warn', 'error', 'query'],
datasources: {
db: {
url: 'postgresql://xxxxx:[email protected]:0000/railway',
},
},
});
}
prisma = globalWithPrisma.prisma;
}

export default prisma;
I'm not proud of it, but unfortunately desperate actions require desperate measures
Brody
Brody11mo ago
never good to hardcode the database variables
Caillou
CaillouOP11mo ago
Unfortunately, that's all there is for today, can you close this topic please?
Want results from more Discord servers?
Add your server