Hi, is there a way to check the available keys on ...
# prisma-client
p
Hi, is there a way to check the available keys on a model at runtime? We'd like to have a dropdown/select render a list of all keys on a given model so we can specify permissions on specific fields
r
prisma._dmmf
should give you the model names.
Where
prisma
is the
PrismaClient
instance.
p
is this a private field that can change in future or why the underscore?
r
Yes it’s a private field and its API is subject to change in the future.
p
😕
How do I see what's on there without manually logging it?
r
You would need to use
// @ts-ignore
in this case
There’s no documentation on this so logging is the only way.
p
yeah I figured as much, but I'd like to see what's on there and have type safety around it
ah ok
any plans to expose it for real in future?
r
Not in the near future. Generators like TypeGraphQL use this API internally to generate CRUD resolvers.
p
ok thanks
👍 1
Maybe I should just do
Copy code
const user = prisma.users.findFirst()
console.log(Object.keys(user))
It means a DB hit but at least I get type safety
r
Yes that’s an option as well.
p
I'll test both out and see. Thanks
💯 1
j
You could create a Class that implements the Prisma type which would require you to implement all the properties. Then you could use getOwnProperties on that class since it's concrete at runtime.
👀 1
p
I dont follow. Can you please elaborate @Jay Bell?
j
Create a class that implements the prisma type
Copy code
// this is the auto generated type from prisma
type Post {
  title: string;
}

class PostEntity implements Post {
  title: string;

  constructor() {
    this.title = '';
  }
}
Then you can do this to get the properties
Copy code
Object.getOwnPropertyNames(new Post());
Although thats a little dirty, and you need to define a constructor that sets the props since they won’t be included in the JS code. You could also do it without classes
Copy code
const post: Post = { title: '' };
Object.getOwnPropertyNames(post);