有没有一种方法可以通过嵌套配置以编程方式覆盖数据源 url?
Is there a way to programmatically override a datasource url with nest config?
我有一个 app.config.ts
文件,其中包含大量如下所示的配置变量:
export const exampleConfig = (config: ConfigService) => {
// Do stuff
return config.get('EXAMPLE')
}
我有我的 schema.prisma
文件,它需要一个数据库 URL...目前,我在我的 app.module.ts
和 [=17= 上使用这个配置文件]:
AppModule:
ExampleModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: exampleConfig
}),
主线:
[...]
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);
})();
注意到我总是如何在 app.config.ts
上使用 @nestjs/config configService 了吗?这里的问题是,我的 schema.prisma
文件需要一个 DB_URL,但我不能使用 configService 也不能在那里使用任何类似的东西......我可以使用 env('DB_URL') 作为文档告诉我这样做,但我想在我的应用程序中继续使用相同的模式,所以我宁愿坚持使用 configService
我一发布这个问题,就找到了我需要的答案,比我想象的更接近:基本上,我必须在我的 PrismaClient 上设置 URL(与 NestJS Prisma doc) 像这样:
[...]
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),
},
},
})
我有一个 app.config.ts
文件,其中包含大量如下所示的配置变量:
export const exampleConfig = (config: ConfigService) => {
// Do stuff
return config.get('EXAMPLE')
}
我有我的 schema.prisma
文件,它需要一个数据库 URL...目前,我在我的 app.module.ts
和 [=17= 上使用这个配置文件]:
AppModule:
ExampleModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: exampleConfig
}),
主线:
[...]
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);
})();
注意到我总是如何在 app.config.ts
上使用 @nestjs/config configService 了吗?这里的问题是,我的 schema.prisma
文件需要一个 DB_URL,但我不能使用 configService 也不能在那里使用任何类似的东西......我可以使用 env('DB_URL') 作为文档告诉我这样做,但我想在我的应用程序中继续使用相同的模式,所以我宁愿坚持使用 configService
我一发布这个问题,就找到了我需要的答案,比我想象的更接近:基本上,我必须在我的 PrismaClient 上设置 URL(与 NestJS Prisma doc) 像这样:
[...]
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),
},
},
})