简体   繁体   English

未在 Prisma 模式中注册的环境变量

[英]Environment variables not registering in Prisma schema

I am finding the only way the Prisma schema can detect environment variables is to include a .env file in the root directory of its app.我发现 Prisma 模式可以检测环境变量的唯一方法是在其应用程序的根目录中包含一个.env文件。

I am using docker compose to build a few services, one of which is an app that manages a database using Prisma.我正在使用 docker compose 来构建一些服务,其中一个是使用 Prisma 管理数据库的应用程序。 The environmental variables is available in the app via process.env however Prisma can not detect this using the env function.环境变量可通过process.env在应用程序中使用,但 Prisma 无法使用env function 检测到这一点。

Here is how I am accessing the variable in schema.prisma :这是我访问schema.prisma中的变量的方式:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

And I am passing the variable in the docker compose in this way:我通过这种方式组合 docker 中的变量:

  app:
    build:
      context: ./app
    environment:
      DATABASE_URL: ${DATABASE_URL}

How can I make this function detect environment variables that don't necessarily belong to a .env variable?如何让这个 function 检测不一定属于.env变量的环境变量?

I eventually worked this out.我最终解决了这个问题。 When I am running commands in my terminal I am running them outside of the docker container, So the prisma code running inside docker works perfect.当我在终端中运行命令时,我在 docker 容器之外运行它们,因此在 docker 中运行的棱镜代码完美运行。 but the commands I run outside can't see that.env file.但是我在外面运行的命令看不到那个 .env 文件。

I was able to solve by adding the env values to my scripts.我能够通过将 env 值添加到我的脚本来解决。 It may not be the most elegant solution but it works.它可能不是最优雅的解决方案,但它确实有效。 For example:例如:

eval $(grep '^DATABASE_URL' ../.env) prisma db pull && prisma generate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM