简体   繁体   中英

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.

As of Prisma 4:

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

console.log("Account fields:", Prisma.dmmf.datamodel.models.find(model => model.name === "Account").fields)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM