I've got a app.config.ts
file with plenty of config variables that look like this:
export const exampleConfig = (config: ConfigService) => {
// Do stuff
return config.get('EXAMPLE')
}
And I've got my schema.prisma
file which requires a database URL... Currently, I use this config file on my app.module.ts
and on my main.ts
:
AppModule:
ExampleModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: exampleConfig
}),
Main:
[...]
import { ConfigService } from '@nestjs/config';
import { exampleConfig } from './config/app.config';
[...]
(async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
[...]
app.exampleCallThatNeedsConfig(exampleConfig(configService));
await app.listen(3000);
})();
Notice how I always use @nestjs/config configService on my app.config.ts
? The thing here is, my schema.prisma
file needs a DB_URL, but I cannot use configService nor anything quite like that in there... I could use a env('DB_URL') as the docs tells me to do, but I'd like to keep using the same pattern all over my application, so I'd rather stick with configService
As soon as I published this question, I ended up finding the answer I needed much closer than I tought: Basically, I had to set the URL on my PrismaClient (as peer the NestJS Prisma doc) like this:
[...]
import { PrismaClient } from '@prisma/client'
import { exampleConfig } from './config/app.config';
@Injectable()
export class PrismaService extends PrismaClient
implements OnModuleInit {
[...]
const prisma = new PrismaClient({
datasources: {
db: {
url: exampleConfig(configService),
},
},
})