在 postgresql 中指定 Prisma 的数据库名称

Specifying Prisma's database name in postgresql

我正在尝试实现一种微服务架构,其中每个微服务都有自己的数据库。我使用 prisma 作为我的服务和 单个 数据库服务器之间的数据访问层。因为我只有一个数据库服务器,所以我希望每个 prisma 实例都可以访问该服务器上它自己的数据库,但是我没有找到指定特定 prisma 实例使用的数据库名称的选项。

问题来了。有没有办法为 prisma 实例指定数据库名称?

如果有帮助,这是我的 docker-compose.yml 文件:

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.14
    restart: always
    ports:
    - "${PRODUCT_PRISMA_PORT}:${PRODUCT_PRISMA_PORT}"
    environment:
      PRISMA_CONFIG: |
        port: ${PRODUCT_PRISMA_PORT}
        managementApiSecret: ${PRODUCT_PRISMA_SECRET}
        databases:
          product:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: prisma
            migrations: true
  postgres:
    image: postgres:11-alpine
    restart: always
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - /var/lib/postgresql/data
  product-app:
    command: yarn start
    image: product-web
    volumes:
      - ./product-service:/usr/app
    ports:
      - "${PRODUCT_APP_PORT}:${PRODUCT_APP_PORT}"
    depends_on:
      - prisma
    environment:
      PORT: ${PRODUCT_APP_PORT}
      PRISMA_ENDPOINT: ${PRODUCT_PRISMA_ENDPOINT}

尝试使用配置中的 "database" 键:

 environment:
  PRISMA_CONFIG: |
    port: ${PRODUCT_PRISMA_PORT}
    managementApiSecret: ${PRODUCT_PRISMA_SECRET}
    databases:
      product:
        connector: postgres
        host: postgres
        port: 5432
        user: prisma
        password: prisma
        migrations: true
        database: ${DATABASE} 

但是,请注意 Prisma 使用 pg_advisory_lock 锁定对数据库的独占访问。我不知道这是否会阻止您在不同的 PG 数据库 上 运行 多个 Prisma 实例,但我强烈建议您尝试一下使用两个容器设置。如果日志行 Obtaining exclusive agent lock... Successful. 出现在 both 容器日志中,则它有效。如果只有 Obtaining exclusive agent lock... 挂起,它不会挂起。

希望对您有所帮助。