Hi everyone. I recently created a <simple REST API...
# prisma-client
r
Hi everyone. I recently created a simple REST API (using Express, Prisma and PostgreSQL) which works fine. Am now currently trying to write some unit tests but am wondering how I can create a test environment with it's own test database. Is it possible to programmatically change the DATABASE_URL in the
schema.prisma
file? For example: Given DEVELOPMENT, TEST, PRODUCTION environments, I would maybe create this kind of logic;
Copy code
const { DEVELOPMENT, PRODUCTION, LOCAL } = require("./envTypes");
let DB_URL;
switch (process.env.NODE_ENV) 
{  case DEVELOPMENT:    
     DB_URL = process.env.DB_URL_DEV;    
      break;  
   case PRODUCTION:    
     DB_URL = process.env.DB_URL_PROD;    
     break;   
   case LOCAL:    
     DB_URL = process.env.DB_URL_LOC;    
     break;  
   default:    
      DB_URL = process.env.DB_URL;
}
1
a
Hey Roland! This is definitely possible! Take a look at the
datasources
option on the
PrismaClient
constructor:
Copy code
import {PrismaClient} from '@prisma/client'

const prisma = new PrismaClient({
  datasources: { db: { url: 'connection string' } }
})
r
@Austin thanks alot for this. Let me check it out. I'll get back soon.