SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.16k stars 659 forks source link

[Bug]: Documented Prisma config doesn't work because PrismaClient object doesn't have a _basedmmf #1506

Closed gogapps closed 1 year ago

gogapps commented 1 year ago

What happened?

Use AdminJS with Fastify and Prisma

Bug prevalence

When I start the server

AdminJS dependencies version

"dependencies": { "@adminjs/fastify": "^4.0.0", "@adminjs/prisma": "^4.0.0", "@fastify/cors": "^8.2.1", "@fastify/env": "^4.2.0", "@fastify/jwt": "^6.7.1", "@fastify/multipart": "^7.6.0", "@fastify/session": "^10.3.0", "adminjs": "^7.0.6", "bcrypt": "^5.1.0", "fastify": "^4.17.0", "fastify-plugin": "^4.5.0", "nodemon": "^2.0.22", "prisma": "^4.14.1", "tslib": "^2.5.2", "zod": "^3.21.4" }, "devDependencies": { "@types/bcrypt": "^5.0.0", "@types/node": "^20.2.3", "ts-node": "^10.9.1", "typescript": "^5.0.4", "@prisma/client": "^4.14.1" }

Relevant log output

TypeError: Cannot read properties of undefined (reading 'modelMap')
    at /path/to/project/src/index.ts:55:33
    at Generator.next (<anonymous>)
    at fulfilled (/path/to/project/src/index.ts:5:58)

Relevant code that's giving you issues

const dmmf = (prisma as any)._baseDmmf as DMMFClass;
  const adminOptions = {
    // We pass Publisher to `resources`
    resources: [
      {
        resource: { model: dmmf.modelMap.Publisher, client: prisma },
        options: {},
      },
    ],
  };
  // Please note that some plugins don't need you to create AdminJS instance manually,
  // instead you would just pass `adminOptions` into the plugin directly,
  // an example would be "@adminjs/hapi"
  const admin = new AdminJS(adminOptions);

Link to recreated demo

here

dziraf commented 1 year ago

It was apparently removed in Prisma 4.14.0: https://github.com/SoftwareBrothers/adminjs-prisma/issues/35

We don't have an official "fix" for this yet as it would also probably require us to release a breaking change release. I'm more inclined towards limiting @adminjs/prisma to prisma versions lower than 4.14.0

gogapps commented 1 year ago

Can you please specify which version is supported by the AdminJS (would be nice if this has also been updated in the docs)

Yureien commented 1 year ago

Can you please specify which version is supported by the AdminJS (would be nice if this has also been updated in the docs)

Any version less than 4.14 (4.13 for example) is supported by AdminJS. 4.14+ will require a breaking change.

Yureien commented 1 year ago

It was apparently removed in Prisma 4.14.0: SoftwareBrothers/adminjs-prisma#35

We don't have an official "fix" for this yet as it would also probably require us to release a breaking change release. I'm more inclined towards limiting @adminjs/prisma to prisma versions lower than 4.14.0

I don't think limiting it to older versions is a good decision (bugs, security vulnerabilities, etc. can occur). IMHO, we should upgrade the major version of @adminjs/prisma to include a breaking change.

MoSattler commented 1 year ago

It was apparently removed in Prisma 4.14.0: SoftwareBrothers/adminjs-prisma#35

We don't have an official "fix" for this yet as it would also probably require us to release a breaking change release. I'm more inclined towards limiting @adminjs/prisma to prisma versions lower than 4.14.0

Hey @dziraf, thanks for looking into this.

I totally get where you're coming from, but holding back at Prisma 4.14.0 kinda keeps us all from some really important fixes and features of later versions. I reckon many of us users would be up for dealing with a bit of a shake-up in AdminJS if it means we can tap into the newer Prisma stuff. Keeping up with Prisma's updates is pretty key for us.

dziraf commented 1 year ago

This should be fixed in @adminjs/prisma@5.0.0. The setup's changed a little, but it's documented in README

MoSattler commented 1 year ago

@dziraf thank you, appreciated!

nicezic commented 1 year ago

same problem. hope to solve this soon.

Briuor commented 1 year ago

@dziraf Thank you for the solution! This new way to configure prisma should be updated in the current docs

f-naranjo commented 1 year ago

@dziraf thank you for the solution! I got AdminJS working with Nestjs using this config, in case someone needs some help with it.

I adapted the solution on the current docs with the new resource declaration:


@Module({
  imports: [
    // AdminJS version 7 is ESM-only. In order to import it, you have to use dynamic imports.
     import('@adminjs/nestjs').then(({ AdminModule }) =>
          AdminModule.createAdminAsync({
            useFactory: () => {
              const prisma = new PrismaService();
              return {
                adminJsOptions: {
                  rootPath: '/admin',
                  resources: [
                     {
                      resource: {
                       model: AdminJSPrisma.getModelByName('Product'),
                       client: prisma,
                        },
                     options: {},
                      },
                   //... every Prisma model that you need to use on AdminJs
                   ]
                 }
               }
           })
      //....