Skip to content

Can't get model name when extending prisma client #19821

Closed

Description

@ghostlexly

Bug description

It's look like impossible to get the model name when extending prisma client.

How to reproduce

export const prisma = new PrismaClient().$extends({
  result: {
    $allModels: {
      uri: {
        compute(model: any) {
          console.log(model.modelName); // <- this returns undefined
          return `${model}/${model.id}`;
        },
      },
    },
  },
});

Expected behavior

I want to display the model name and the id in the results.

Prisma information

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  password      String
  role Role @default(CUSTOMER)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

enum Role {
    ADMIN
    CUSTOMER
}
// Add your code using Prisma Client

Environment & setup

  • OS: MacOS
  • Database: MySQL
  • Node.js version: 18.16.0

Prisma Version

4.15.0

Activity

  1. added
    kind/bug
    on Jun 16, 2023
  2. added
    kind/feature
    and removed
    kind/bug
    on Jun 19, 2023
  3. SevInf commented on Jun 19, 2023

    @SevInf
    Contributor

    Hi @ghostlexly.

    export const prisma = new PrismaClient().$extends({
      result: {
        $allModels: {
          uri: {
            compute(model: any) {
              console.log(model.modelName); // <- this returns undefined
              return `${model}/${model.id}`;
            },
          },
        },
      },
    });
    

    This is not the API that exists in the current version of Prisma. compute callback receives the dependencies of your computed field (fields of your model you specify in needs), nothing more, nothing less. It does not receive any information on top of that, like modelName in your examples.

    I've converted the issue into the feature request.

  4. added and removed
    kind/feature
    on Jun 19, 2023
  5. SevInf commented on Jun 19, 2023

    @SevInf
    Contributor

    Note for the future: here is a couple of ideas of how this feature might look like once we get to it:

    getExtensionContext

    This is consistent with how we do it for model extensions:

    prisma.$extends({
      $result: {
        $allModels: {
             field: {
               compute() {
                  Prisma.getExtensionContext(this).name
               }
             }
        }
      }
    })

    Second argument to the compute

    Since compute arguments are not exposed via extended client directly (unlike model extensions) we can also do simpler and more discoverable variant:

    ```ts
    prisma.$extends({
      $result: {
        $allModels: {
             field: {
               compute(data, modelName) {
               }
             }
        }
      }
    })
  6. sakgoyal commented on Feb 17, 2024

    @sakgoyal

    is there a status update on if/when this will be done?

  7. vic-blt commented on Feb 6, 2026

    @vic-blt
    Contributor

    Since compute arguments are not exposed via extended client directly (unlike model extensions) we can also do simpler and more discoverable variant:

    prisma.$extends({
      $result: {
        $allModels: {
             field: {
               compute(data, modelName) {
               }
             }
        }
      }
    })

    @SevInf Something like that https://github.com/vic-blt/prisma/commit/38cdd027412b6d8fd5e67c592f450c7253d01973 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions