Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 957
Sergey Pletnev Avatar asked Nov 01 '25 01:11

Sergey Pletnev


2 Answers

As of Prisma 4:

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

console.log("Account fields:", Prisma.dmmf.datamodel.models.find(model => model.name === "Account").fields)
like image 99
techpet Avatar answered Nov 03 '25 17:11

techpet


As of Prisma 5:

const allowedFields = Object.keys(prisma[this.modelName].fields)
like image 31
Caio Lucena Colaço Avatar answered Nov 03 '25 17:11

Caio Lucena Colaço



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!