olivierwilkinson / prisma-extension-nested-operations

Utils for creating Prisma client extensions with nested operations
Apache License 2.0
28 stars 4 forks source link

Unhandled Runtime Error: Prisma DMMF not found #7

Open kachar opened 4 months ago

kachar commented 4 months ago

We've stumbled upon the following error when we've introduced "prisma-extension-soft-delete": "1.0.0", in our app.

I'm raising the issue in this repo since the error is located in the prisma-extension-nested-operations code.

Unhandled Runtime Error
Error: Prisma DMMF not found, please generate Prisma client using `npx prisma generate`

Call Stack
eval
../node_modules/prisma-extension-nested-operations/dist/esm/lib/utils/relations.js (3:10)
(app-pages-browser)/../../node_modules/prisma-extension-nested-operations/dist/esm/lib/utils/relations.js
file:///Users/kachar/Projects/my-app/apps/web/.next/static/chunks/app/(auth)/(public)/sign-in/page.js (2105:1)
options.factory
file:///Users/kachar/Projects/my-app/apps/web/.next/static/chunks/webpack.js (716:31)
__webpack_require__
file:///Users/kachar/Projects/my-app/apps/web/.next/static/chunks/webpack.js (37:33)
fn
file:///Users/kachar/Projects/my-app/apps/web/.next/static/chunks/webpack.js (371:21)
eval
(app-pages-browser)/../../node_modules/prisma-extension-nested-operations/dist/esm/lib/utils/extractNestedOperations.js (13:68)
(app-pages-browser)/../../node_modules/prisma-extension-nested-operations/dist/esm/lib/utils/extractNestedOperations.js
file:///Users/kachar/Projects/my-app/apps/web/.next/static/chunks/app/(auth)/(public)/sign-in/page.js (2072:1)

We're using turborepo and the lib is loaded from a sub-package located at packages/db/schema.prisma However the main app is unable to load the dependencies.

    "next": "^14.1.0",
    "next-auth": "^4.24.6",
  "dependencies": {
    "@prisma/client": "5.10.2",
    "@t3-oss/env-nextjs": "^0.9.2",
    "prisma-extension-soft-delete": "1.0.0",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "eslint": "^8.57.0",
    "prettier": "^3.2.5",
    "prisma": "5.10.2",
    "tsx": "^4.7.1",
    "typescript": "^5.3.3",
    "zod-prisma-types": "3.1.6"
  },
generator client {
    provider        = "prisma-client-js"
    previewFeatures = ["postgresqlExtensions"]
    extensions      = [pgcrypto, citext, uuidOssp(map: "uuid-ossp")]
}

generator zod {
    provider                         = "zod-prisma-types"
    createInputTypes                 = false
    createOptionalDefaultValuesTypes = true
    addInputTypeValidation           = true
}

The prisma schema is generated, all migrations are applied.

rpaterson commented 1 month ago

I think the problem is the Turborepo setup is storing the cached generated Prisma code in an unexpected place. The Turborepo docs say:

Prisma behaves differently with different package managers. This can lead to unpredictable results, which might lead to broken deployments in some situations. Instead of documenting the intricacies of each approach, we recommend not caching the results of prisma generate.

rpaterson commented 1 month ago

Actually I think it's not just Turborepo. Prisma has an output option to change the location of the generated Prisma client, but relations.ts is always trying to load it from the default location.

rpaterson commented 1 month ago

Here's how a similar extension solves this same issue: https://github.com/47ng/prisma-field-encryption?tab=readme-ov-file#custom-prisma-client-location

rpaterson commented 1 month ago

I looked at the this object passed to the extension by Prisma, and also the result of Prisma.getExtensionContext(this), but neither of them expose the DMMF as far as I can tell - I think the fix needs to be the same I linked above with the prisma-field-encryption extension: optionally pass in the DMMF.

rpaterson commented 1 month ago

@olivierwilkinson would you accept a patch to add this?

mohamedamara1 commented 2 days ago

Running into the same issue