Kinara Nyakaru
Kinara Nyakaru

Reputation: 501

How can I setup more than one PRISMA service, one for testing and one for development

I want to have a separate database for testing and development. What I’m trying to achieve is to have more than one Prisma service, one for testing and one for normal development.

This my docker-compose.yml file

version: '3'
services:
    prisma:
        image: prismagraphql/prisma:1.34
        restart: 'always'
        ports:
            - '4466:4466'
        environment:
            PRISMA_CONFIG: |
                port: 4466
                databases:
                  default:
                    connector: mongo
                    uri: ${MONGO_CONNECTION_STRING}
    prisma_testing:
        image: prismagraphql/prisma:1.34
        restart: 'always'
        ports:
            - '4400:4400'
        environment:
            PRISMA_CONFIG: |
                port: 4466
                databases:
                  default:
                    connector: mongo
                    uri: ${TEST_MONGO_CONNECTION_STRING}

I can’t find anything in the docs for achieving this. Is there a recommended flow or config for achieving this?

Upvotes: 2

Views: 459

Answers (1)

Danny Sofftie
Danny Sofftie

Reputation: 1051

The easiest way would be to change prisma's endpoint to point to a different prisma server before you run prisma deploy. As of the time of writing, prisma has been renamed from prisma to prisma1. You can find more details here, so ensure you have prisma1 installed as a devDependency.

Prisma explains how to achieve the same in their docs here.

Follow below steps:

  1. Install prisma1 as a devDependency yarn add prisma1 -D,
  2. Generate prisma client and specify config file to use npx prisma1 generate -p path/to/prisma.test.yml
  3. Run your tests

In your config file(s), specify different endpoints pointing to different prisma servers, you might want to have one for testing and another for production.

Contents for the different config files might look as below:

prisma.test.yml (For running your local tests)

endpoint: http://127.0.0.1:4466
datamodel: datamodel.prisma
databaseType: document
secret: u4r4secret

generate:
    - generator: javascript-client
      output: ./generated/prisma-client/

prisma.yml (For production use)

endpoint: http://prod-server-ip:4466
datamodel: datamodel.prisma
databaseType: document
secret: u4r4secret

generate:
    - generator: javascript-client
      output: ./generated/prisma-client/

Also improtant, don't forget to regenerate prisma client before pushing your code to production. A quick and easy way would be to use git hooks.

Suggestion:

Use husky and add pre-commit hook, which will run before git commit, to always ensure your prisma client will always have production endpoint before you push to production. Add below section to package.json.

"husky": {
    "hooks": {
      "pre-commit": "yarn prisma:generate -p path/to/prisma.yml"
    }
  }

Upvotes: 1

Related Questions