P
Prisma4mo ago
mata

Share prisma schema and client between two backend services.

I have this backend project where I am creating two services both running on different port e.g httpService , wsService. Though I am challenged with handling prisma between the two services since they are going to rely on the same database, I think they would need to share the prisma client and not to have challenges with manually updating the schema from one service to another. Below is my file structure, whenever I try to run prisma it tries to create a package.json in the root folder which I don't desire. How can I share prisma client between to services running within different docker containers?
backend/
├── docker-compose.yml
├── httpServer/
│ ├── Dockerfile
│ ├── package.json
│ ├── package-lock.json
│ ├── prismaClient.ts
│ ├── src/
│ │ ├── httpServer.ts
│ │ └── ...
│ ├
│ └── .env
├── wsServer/
│ ├── Dockerfile
│ ├── package.json
│ ├── package-lock.json
│ ├── prismaClient.ts
│ ├── src/
│ │ ├── wsServer.ts
│ │ └── ...
│ ├
│ └── .env
└── prisma/
├── schema.prisma
└── migrations/
backend/
├── docker-compose.yml
├── httpServer/
│ ├── Dockerfile
│ ├── package.json
│ ├── package-lock.json
│ ├── prismaClient.ts
│ ├── src/
│ │ ├── httpServer.ts
│ │ └── ...
│ ├
│ └── .env
├── wsServer/
│ ├── Dockerfile
│ ├── package.json
│ ├── package-lock.json
│ ├── prismaClient.ts
│ ├── src/
│ │ ├── wsServer.ts
│ │ └── ...
│ ├
│ └── .env
└── prisma/
├── schema.prisma
└── migrations/
I will be glad for any assistance offered. thanks
3 Replies
Dkyc
Dkyc4mo ago
maybe this might help you; you can run npx prisma generate in each subfolder but add this to to both package.json files:
"prisma": {
"schema": "../prisma/schema.prisma"
}
"prisma": {
"schema": "../prisma/schema.prisma"
}
Fr0xy
Fr0xy4mo ago
Your folder structure is good, what you need to do is having 2 generators inside you schema.prisma (https://www.prisma.io/docs/orm/prisma-schema/overview/generators) in this way you will be able to specify the output of both like this:
Generators (Reference) | Prisma Documentation
Generators in your Prisma schema specify what assets are generated when the prisma generate command is invoked. This page explains how to configure generators.
Fr0xy
Fr0xy4mo ago
generator clientHttp {
provider = "prisma-client-js"
output = "../httpServer/node_modules/@prisma/client"
}

generator clientWs {
provider = "prisma-client-js"
output = "../wsServer/node_modules/@prisma/client"
}
generator clientHttp {
provider = "prisma-client-js"
output = "../httpServer/node_modules/@prisma/client"
}

generator clientWs {
provider = "prisma-client-js"
output = "../wsServer/node_modules/@prisma/client"
}
Want results from more Discord servers?
Add your server