3

I'm trying to implement a microservice architecture where every microservice has its' own database. I'm using prisma as a data access layer between my services and a single database server. Since I have a single database server I want each prisma instance to access its' own database on this server, but I didn't find an option to specify name of a database used by particular prisma instance.

So here's the question. Is there a way to specify a database name for prisma instance?

Here's my docker-compose.yml file if it's helpful:

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}

1 Answer 1

9

Try using the "database" key on the config:

 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} 

However, be aware that Prisma uses pg_advisory_lock to lock exclusive access on the database. I don't know from the top off my head if this will prevent you from running multiple Prisma instances on different PG databases, but I would strongly advise you to try it out with a two container setup. If the log line Obtaining exclusive agent lock... Successful. appears in both container logs, it works. If one hangs with Obtaining exclusive agent lock... only, it doesn't.

Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it works. Looks like the lock works only for a database, not the whole server. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.