I'd like to loop through the model keys and genera...
# orm-help
a
I'd like to loop through the model keys and generate a json/JavaScript object for testing purpose. Is it possible to access the model?
r
You can but you would need to check the model names as the
PrismaClient
instance contains other properties as well.
a
Copy code
let testJsonItem = prisma.myModel.map(function(){return jsonObject}
```
This is what I'd to do
This helps to generate the json/javascript objects and share with the frontend for creating the state objects
@Ryan Just referred to this doc https://www.prisma.io/docs/reference/api-reference/prisma-client-reference It seems like we don't the option to get the schema keys
r
The keys will be on the instance. So if you have a model like
User
it will be available as
prisma.user
.
a
so how to access the field properties of the user/User such as firstName?
I'd like to a get list of the properties, map through it and create a json/javascript object.
console.log(
prisma.user)
gives only the methods available on the model
r
For that you need
dmmf
:
Copy code
const prisma = new PrismaClient()
// @ts-ignore
console.log(prisma._dmmf)
This will give you all fields and the structure of the models
a
I'm getting this format. DMMFClass isn't the key here
Copy code
DMMFClass {
  outputTypeToMergedOutputType: [Function (anonymous)],
  datamodel: {
    enums: [],
    models: [ [Object], [Object], [Object], [Object], [Object], [Object] ]
  },
  schema: {
    inputObjectTypes: { prisma: [Array] },
    outputObjectTypes: { prisma: [Array], model: [Array] },
    enumTypes: { prisma: [Array] }
r
Yes and
datamodel.models
contains all the models you need along with the fields/properties.
a
That's inside the DMMFClass which is the key to access