prismaprisma2

Is it possible to access metadata about the prisma model?


Let's say I have a model in my schema.prisma file:

model Post {
  id        Int   @id @default(autoincrement())
  author    User  @relation(fields: [authorId], references: [id])
  authorId  Int
}

Having a variable called model in my server containing a model name

const model: string = [model name generated dynamically]

Using this string I want to know every information about this model. For example if this variable model happens to be Post, I want to know that it has fields id, author, authorId and also information about each field separately, like in the case of author which field in which model does it reference, in this example the model User the field id.

I'm aware that prisma generates a type for each model and that way maybe I can access the fields that way but that't not enough for me, I want information about each fields as well.

I search the prisma docs, also googled something like 'get meta information about model in prisma2' but I didn't find any solution. Is there a way to achieve this?


Solution

  • Yes, you can get all the metadata for your entire schema in the following manner:

    const prisma = new PrismaClient()
    
    // @ts-ignore
    console.log(prisma._dmmf)
    

    This will give you all the details for the models and relations. The reason this is not documented is that this is highly experimental and will change with releases, which is why it's used for internal purposes only.