prisma / docs

📚 Prisma Documentation
https://www.prisma.io/docs
Apache License 2.0
979 stars 772 forks source link

Document how to use Prisma in monorepos #1288

Open nikolasburk opened 3 years ago

nikolasburk commented 3 years ago

e.g. using:

Note that there's a simple example with Yarn workspaces here: https://github.com/nikolasburk/monorepo-prisma

More context on the internal Slack:

screenshot ![image](https://user-images.githubusercontent.com/4058327/107616595-ab2c7b00-6c4e-11eb-86a0-97c19dad155a.png)
johannesschobel commented 3 years ago

I would like to add nrwl/nx ( https://github.com/nrwl/nx ) to the list of monorepos.

tsongas commented 2 years ago

Vercel just bought Turborepo I think this is going to become very important very quickly!

yorch commented 2 years ago

From what I've been testing, doesn't actually work when using ENV variables unless I place the .env file in the prisma directory.

So for instance, I have this monorepo with yarn workspaces

 |- packages/
     |- server/
         |- package.json
 |- prisma/
     |- schema.prisma
     |- package.json
 |- package.json
 |- .env

Seems like it needs to have the .env file in side the prisma directory instead of reading the file from the file on root

Liam-Tait commented 2 years ago

Related https://github.com/vercel/turborepo/pull/979#issuecomment-1183444069

zomars commented 2 years ago

Yes please, also declaring .env files on the root of the monorepo break prisma env() function if prisma schema is not located at prisma/schema.prisma

zomars commented 2 years ago

From what I've been testing, doesn't actually work when using ENV variables unless I place the .env file in the prisma directory.

So for instance, I have this monorepo with yarn workspaces

 |- packages/
     |- server/
         |- package.json
 |- prisma/
     |- schema.prisma
     |- package.json
 |- package.json
 |- .env

Seems like it needs to have the .env file in side the prisma directory instead of reading the file from the file on root

Yes! +1 to this 🙌🏽

EDIT: Relevant issue as well prisma/prisma#12535

leerob commented 2 years ago

Related example: https://github.com/vercel/turborepo/tree/main/examples/with-prisma

kokokenada commented 2 years ago

I have an issue with monorepo using npm workspaces and prisma. The set up is there are two microservices that both use prisma (say packages/api1, packages/api2). If the same version of prisma is used in each, npm will create @prisma in the root node_modules. The generated .prisma files are installed as a sibling to @prisma, so api1 and api2 can clobber each other. One can specify where to generate the files, however, other dependencies (i.e. nestjs) uses @prisma/client to import, so the new location is ignored.

nikolasburk commented 2 years ago

however, other dependencies (i.e. nestjs) uses @prisma/client to import, so the new location is ignored.

@kokokenada Do you think this could be configurable in your NestJS app? If no, it might be worth opening an issue in the nest repo or submit a PR to add this option.

kokokenada commented 2 years ago

Thanks for the reply @nikolasburk, and yes, I also opened a ticket there: https://github.com/nestjs/nest/issues/10150 and moved to https://github.com/notiz-dev/nestjs-prisma/issues/31

skworden commented 1 year ago

I have the same issue as @kokoenada . I can't have a turborepo monorepo with two different Prisma schemas - Prisma gets confused about which schema to use.

sebpowell commented 1 year ago

I came across this thread whilst looking for a solution to this: https://github.com/vercel/turbo/discussions/5610

Wondering if anyone has experienced a similar issue?

Edited: found the fix, see here in case it's of use to anyone: https://www.prisma.io/docs/guides/other/troubleshooting-orm/help-articles/nextjs-prisma-client-monorepo

toteto commented 10 months ago

For my case where the monorepo will have multiple packages with different schemas, I opted to generate separate prisma clients into separate directories.

// packages/accounts/prisma.schema

generator client {
  provider = "prisma-client-js"
  output   = "./node_modules/@prisma/accounts-client" // <-- note this
  // output   = "./node_modules/@prisma/dashboard-client" // <-- for other package "dashboard"
}
// pacakges/accounts/src/...
import { PrismaClient } from '@prisma/accounts-client';

// ...

Can anyone tell me if this is OK approach or I will have some unexpected issues along the way?