gperdomor / nx-tools

Nx Workspaces builders and tools
MIT License
351 stars 54 forks source link

DTE and Prisma #408

Closed smasala closed 2 years ago

smasala commented 2 years ago

Any ideas on how to get the client to generate correctly for Distributed Task Execution? https://github.com/nrwl/nx/discussions/8342

gperdomor commented 2 years ago

Just create a new target in your project.json file to generate the client, something like this:

"prisma-generate": {
    "executor": "@nx-tools/nx-prisma:generate",
    "options": {
        "schema": "apps/api/src/prisma/schema.prisma"
    }
}

and then run in your agents:

    - NX_CLOUD_DISTRIBUTED_EXECUTION=false npx nx affected --target=prisma-generate --parallel
    - npx nx-cloud start-agent

That will generate the client in every distributed agent...

smasala commented 2 years ago

@gperdomor unfortunately, that doesn't work. Tried that already.

smasala commented 2 years ago

CI Setting

- name: Generate Prisma
   run: NX_CLOUD_DISTRIBUTED_EXECUTION=false pnpm run nx -- affected --target=prisma-generate --parallel

- name: 'Init nx cloud'
   run: npx nx-cloud start-ci-run

Project config

"prisma-generate": {
      "builder": "@nx-tools/nx-prisma:generate",
      "options": {
        "schema": "libs/mysuperlib:prisma/src/prisma/schema.prisma"
      }
    }

Lib:

> nx run mysuperlib:prisma-generate 
--> Generating Client...
/opt/hostedtoolcache/node/16.13.1/x64/bin/npx prisma generate --schema=libs/mysuperlib/src/prisma/schema.prisma
Environment variables loaded from .env
Prisma schema loaded from libs/mysuperlib

✔ Generated Prisma Client (3.7.0 | library) to ./node_modules/.pnpm/@prisma+client@3.7.0_prisma@3.7.0/node_modules/@prisma/client in 384ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

———————————————————————————————————————————————

>  NX   SUCCESS  Running target "prisma-generate" succeeded

App build

TS2305: Module '"node_modules/@prisma/client"' has no exported member 'Prisma'.
  > 5 | import { Prisma } from '@prisma/client';
      |          ^^^^^^
    8 |

Turn off DTE globally and all is fine image

gperdomor commented 2 years ago

can you provide a minimum reproduction repository?

smasala commented 2 years ago

@gperdomor sure: https://github.com/smasala/nx-dte-prisma/runs/4693791308?check_suite_focus=true

smasala commented 2 years ago

P.s. can this even work on "affected"? Don't all libraries that use prisma delete and create the same prisma client?

smasala commented 2 years ago

https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/generating-prisma-client#the-location-of-prisma-client this should do the trick

gperdomor commented 2 years ago

@smasala I send a PR to your repo to solve the problem 👍🏻 ...<

Regarding to the client location, it's only required if you have multiple schemas, for example if you have 2 apps, but since this repo is using only one app, you can use the default location... The issue was not related to the prisma client location

smasala commented 2 years ago

@gperdomor thanks, workflows seem to be failing and the agents are stuck in a loop 😬

gperdomor commented 2 years ago

@gperdomor thanks, workflows seem to be failing and the agents are stuck in a loop 😬

Maybe because permissions??? weird!... anyways, this is the same code on my fork https://github.com/gperdomor/nx-dte-prisma/pulls and it's working fine... I will try to send a new commit to force a new run

smasala commented 2 years ago

@gperdomor ok cool, I'll try it out on my actual repo and let you. Thanks for you help 😊👍

smasala commented 2 years ago

@gperdomor excellent, works thanks!

gperdomor commented 2 years ago

@smasala glad to hear that... I will close this issue :D

brandoncollins7 commented 1 year ago

What fix was made to this guys code? I am having the same issue. The agents each pull their own code and they can't run a target unless it's cachable so I can't figure out how to get them to generate the client.

AlexJWayne commented 1 year ago

Yeah, stumbled into this today, too, and I have no idea what I'm doing wrong here.

smasala commented 1 year ago

@brandoncollins7 @AlexJWayne Run this as an extra step inside your agents:

    - name: Generate Prisma
      run: NX_CLOUD_DISTRIBUTED_EXECUTION=false pnpm run nx run-many --target=prisma-generate --all --parallel=1

I also generate the prisma's locally (in libs, not under node_modules) because I have multiple projects:

generator client {
  provider = "prisma-client-js"
  output   = "../generated/client"
  binaryTargets = ["native", "linux-musl"]
}
sourcecodeit commented 2 months ago

I have the same issue. An Nx workspace with many apps and 2 of them using Prisma with their own schema.

I added this in both:

generator client {
    provider = "prisma-client-js"
    output   = "../generated/client"
}

both project.json have:

"prisma-generate": {
      "executor": "@nx-tools/nx-prisma:generate",
      "options": {
        "schema": "importer/prisma/schema.prisma"
      }
    },

The command:

npx nx run-many -t prisma-generate

works, but if I try to build too then it fails:

npx nx run-many -t prisma-generate build

    kpi-check/src/app/services/kpi.ts:101:33 - error TS2339: Property 'customer' does not exist on type 'PrismaClient<PrismaClientOptions, never, DefaultArgs>'.

          99 |  if (failedSources.length > 0) {
         100 |      getLogger().info(`Sending alert for ${failedSources.length} failed indicators`)
       > 101 |      const customer = await prisma.customer.findFirst({
             |                                    ^
         102 |          where: {
         103 |              code: customerCode
         104 |          },

Clearly, running both tasks on a single project works:

nx run kpi-check:prisma-generate
nx run kpi-check:build

So my problem is not about the Prisma client but it's about the generated Prisma code.

brandoncollins7 commented 2 months ago

I ended up having each project that uses prisma generate it's code into a seperate models library for each project as opposed to the default location. I found that this way it works and my other microservices which might receive a particular model as a result to a service call can also reference that models project and get free type checking that's in sync

sourcecodeit commented 2 months ago

I ended up having each project that uses prisma generate it's code into a seperate models library for each project

You mean this?

generator client {
    provider = "prisma-client-js"
    output   = "../generated/client"
}

I tried and it works locally but it fails in the Github action

sourcecodeit commented 2 months ago

Ok it seems it fixed with this:

npx nx affected -t prisma-generate build --prod --parallel=1

brandoncollins7 commented 2 months ago

then you probably need a build dependsOn prisma-generate

On Sat, Aug 3, 2024 at 10:16 AM Diego Imbriani @.***> wrote:

Ok it seems it fixed with this:

npx nx affected -t prisma-generate build --prod --parallel=1

— Reply to this email directly, view it on GitHub https://github.com/gperdomor/nx-tools/issues/408#issuecomment-2266725688, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIV2AKTN37XD4GEC2QCLR5LZPTQ4RAVCNFSM6AAAAABL5WFB7KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRWG4ZDKNRYHA . You are receiving this because you were mentioned.Message ID: @.***>

sourcecodeit commented 2 months ago

then you probably need a build dependsOn prisma-generate

thanks, that seems it fixes too!