nestjs-prisma icon indicating copy to clipboard operation
nestjs-prisma copied to clipboard

Custom Prisma Client is not working

Open brandoncollins7 opened this issue 2 years ago • 17 comments

I am using an nx mono repo and I have my prisma models dumping to a library. So I am trying to integrate this by using a custom prisma client:

`import {PrismaClient} from '@frnt/lending-api/models'

@Global() @Module({ imports: [ CustomPrismaModule.forRoot({ name: 'PrismaService', client: new PrismaClient() }),`

But it just seems like it is not working at all because when I run it is looking for the prisma client in node_modules?

`Error: Cannot find module '.prisma/client/index' Require stack:

  • node_modules.pnpm@[email protected][email protected]\node_modules@prisma\client\index.js
  • node_modules.pnpm\[email protected]_@[email protected]_@[email protected][email protected]\node_modules\nestjs-prisma\dist\prisma.service.js
  • node_modules.pnpm\[email protected]_@[email protected]_@[email protected][email protected]\node_modules\nestjs-prisma\dist\prisma.module.js
  • node_modules.pnpm\[email protected]_@[email protected]_@[email protected][email protected]\node_modules\nestjs-prisma\dist\index.js
  • dist\apps\lending-api\main.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15) at Function.Module._load (node:internal/modules/cjs/loader:920:27) at Function.Module._load (node_modules@nx\js\src\executors\node\node-with-require-overrides.js:18:31)
    at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at Object. (node_modules@[email protected][email protected]\node_modules@prisma\client\index.js:2:6) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Function.Module._load (node:internal/modules/cjs/loader:958:12) at Function.Module._load (node_modules@nx\js\src\executors\node\node-with-require-overrides.js:18:31)
    at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at Object. (node_modules\nestjs-prisma\lib\prisma.service.ts:9:1) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1308:10)

`

brandoncollins7 avatar Oct 05 '23 16:10 brandoncollins7

Hi @brandoncollins7, how are you accessing the database are you using CustomPrismaService? Do you have a reproduction repository, which would be easier to help if i can reproduce it myself.

marcjulian avatar Oct 06 '23 08:10 marcjulian

I cannot reproduce it atm. Please provide a reproduction repo.

marcjulian avatar Oct 17 '23 08:10 marcjulian

Hi @marcjulian, I'm having the same issue but with a PNPM and Turborepo monorepo. I am using CustomPrismaService<PrismaClient> with my PrismaClient exported from the output.

I have tried both exporting the custom output to node_modules as specified on the documentation as well as on a folder inside my src directory, but it seems that nestjs-prisma is trying to import the default @prisma/client and it cannot find it (prisma generate creates the Prisma client on the specified custom output). I'm attaching the error:

test:start:dev: Error: Cannot find module '.prisma/client/index'
test:start:dev: Require stack:
test:start:dev: - /Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/index.js
test:start:dev: - /Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/nestjs-prisma/dist/prisma.service.js
test:start:dev: - /Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/nestjs-prisma/dist/prisma.module.js
test:start:dev: - /Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/nestjs-prisma/dist/index.js
test:start:dev: - /Users/tomascastro/Code/org/org-svcs/apps/test/dist/app.module.js
test:start:dev: - /Users/tomascastro/Code/org/org-svcs/apps/test/dist/main.js
test:start:dev:     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
test:start:dev:     at Module.Hook.Module.require (/Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/[email protected]/node_modules/dd-trace/packages/dd-trace/src/ritm.js:54:29)
test:start:dev:     at require (node:internal/modules/helpers:130:18)
test:start:dev:     at Object.<anonymous> (/Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/index.js:2:6)
test:start:dev:     at Module._compile (node:internal/modules/cjs/loader:1233:14)
test:start:dev:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
test:start:dev:     at Module.load (node:internal/modules/cjs/loader:1091:32)
test:start:dev:     at Function.Module._load (node:internal/modules/cjs/loader:938:12)
test:start:dev:     at Module.require (node:internal/modules/cjs/loader:1115:19)
test:start:dev:     at Module.Hook.Module.require (/Users/tomascastro/Code/org/org-svcs/node_modules/.pnpm/[email protected]/node_modules/dd-trace/packages/dd-trace/src/ritm.js:85:33)
generator client {
  provider = "prisma-client-js"
  output   = "../node_modules/@org/client"
}

app.module.ts

import { CustomPrismaModule } from 'nestjs-prisma';
import { PrismaClient } from '@org/client';

...
CustomPrismaModule.forRoot({
  name: 'PrismaService',
  client: new PrismaClient(),
  isGlobal: true,
}),

I can help providing a small repo to reproduce, but that will take a bit of time. Maybe this can help in the meantime? 😄

tomcastro avatar Oct 18 '23 07:10 tomcastro

Thanks for the error logs. Looks like PrismaService, which is in the package is trying to access the default prisma client. I will add an example and see if I can reproduce the same error.

marcjulian avatar Oct 18 '23 09:10 marcjulian

It looks like that this is caused because of the index.ts exporting all files from the library, even though PrismaService is not used it imports from @prisma/client.

I guess the import need to change e.g.

- import { CustomPrismaModule } from 'nestjs-prisma';
+ import { CustomPrismaModule } from 'nestjs-prisma/custom';

https://github.com/notiz-dev/nestjs-prisma/blob/a84e24d8f204e978fa1158c1d908a603df62b2fb/lib/index.ts#L1-L4

A current workaround would be to change the imports too

import { CustomPrismaModule } from 'nestjs-prisma/dist/custom';
import { CustomPrismaService } from 'nestjs-prisma/dist/custom';

marcjulian avatar Oct 18 '23 09:10 marcjulian

Hi, thanks for the workaround, but it's seem we can't use the PrismaClientExceptionFilter with this (as it's also require prisma/client)

Do you think it will be possible in the near future to use all the feature with a custom client ? or is there also another workaround for this case ?

mvp-studio-repo avatar Nov 09 '23 15:11 mvp-studio-repo

I am facing the same issue, I managed to solve this issue with workaround (thanks to @marcjulian), although I am not sure whether this is a stable or temporary solution.

My stack:

  • Nx, typescript
  • Custom Prisma Client path

Krr0ptioN avatar Nov 22 '23 15:11 Krr0ptioN

@Krr0ptioN Could you share / show how you did your workaround ?

mvp-studio-repo avatar Nov 22 '23 16:11 mvp-studio-repo

@Valetek have you tried this workaround? https://github.com/notiz-dev/nestjs-prisma/issues/79#issuecomment-1768053972 I think @Krr0ptioN means that workaround.

marcjulian avatar Nov 22 '23 16:11 marcjulian

HO yes this one, finally I used the setup for custom config (with prisma module) and everything is working fine, except that I used my own custom FilterException

I did tried this workaround but it's still fail when calling the prismaFilterException

mvp-studio-repo avatar Nov 22 '23 16:11 mvp-studio-repo

Yes PrismaClientExceptionFilter does not currently work for custom location. I will have a look on a fix for that as well.

marcjulian avatar Nov 22 '23 16:11 marcjulian

@Valetek this workaround

Krr0ptioN avatar Nov 22 '23 17:11 Krr0ptioN

I am facing the same issue, I managed to solve this issue with workaround (thanks to @marcjulian), although I am not sure whether this is a stable or temporary solution.

My stack:

  • Nx, typescript
  • Custom Prisma Client path

This is currently a temporary solution. When I have the time I would like to move all exports related to custom prisma service under nestjs-prisma/custom.

marcjulian avatar Nov 27 '23 09:11 marcjulian