'How can I get the all fields from Prisma class?

I have these two tables in Prisma schema:

model Accounts {
  id Int @id @default(autoincrement())
  name String @db.VarChar(100)
  description String? @db.VarChar(255)
  timeZone Int @default(0)
  tableBusinessApplication AccountsBusinessApplications[]
}

model AccountsBusinessApplications {
  id Int @id @default(autoincrement())
  account Accounts @relation(fields: [accountId], references: [id])
  accountId Int
  name String @db.VarChar(100)
  identification String @db.VarChar(100)
  secretKey String @db.VarChar(32)
}

I have the follow piece of code:

const name = 'Accounts'
prisma[name].findFirst({
  where: { id: 1}
}).then(result => { console.log(result) })

and as a result I have:

{
  id: 1,
  name: 'test',
  description: 'test description',
  timeZone: 0
}

but I don't see 'tableBusinessApplication' inside. How can I get all data if I know only first class name "Accounts" and I can't use 'Include' in Query?

I try to find how to get a list of fields using prisma class, but it seems like there is nothing.



Solution 1:[1]

You can use Prisma's DMMF property to get the name of the fields of a model.

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();


// A `main` function so that you can use async/await
async function main() {

  // @ts-ignore
  console.log('dmmf', prisma._dmmf.modelMap.Accounts.fields);
}
main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

Here's the output

dmmf [
  {
    name: 'id',
    kind: 'scalar',
    isList: false,
    isRequired: true,
    isUnique: false,
    isId: true,
    isReadOnly: false,
    type: 'Int',
    hasDefaultValue: true,
    default: { name: 'autoincrement', args: [] },
    isGenerated: false,
    isUpdatedAt: false
  },
  {
    name: 'name',
    kind: 'scalar',
    isList: false,
    isRequired: true,
    isUnique: false,
    isId: false,
    isReadOnly: false,
    type: 'String',
    hasDefaultValue: false,
    isGenerated: false,
    isUpdatedAt: false
  },
  {
    name: 'description',
    kind: 'scalar',
    isList: false,
    isRequired: false,
    isUnique: false,
    isId: false,
    isReadOnly: false,
    type: 'String',
    hasDefaultValue: false,
    isGenerated: false,
    isUpdatedAt: false
  },
  {
    name: 'timeZone',
    kind: 'scalar',
    isList: false,
    isRequired: true,
    isUnique: false,
    isId: false,
    isReadOnly: false,
    type: 'Int',
    hasDefaultValue: true,
    default: 0,
    isGenerated: false,
    isUpdatedAt: false
  },
  {
    name: 'tableBusinessApplication',
    kind: 'object',
    isList: true,
    isRequired: true,
    isUnique: false,
    isId: false,
    isReadOnly: false,
    type: 'AccountsBusinessApplications',
    hasDefaultValue: false,
    relationName: 'AccountsToAccountsBusinessApplications',
    relationFromFields: [],
    relationToFields: [],
    isGenerated: false,
    isUpdatedAt: false
  }
]

Please note that DMMF is an internal API and may have changes in future versions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nurul Sundarani