IssueRaiser

Define type of content of `Json` field

## Problem Right now if you have the following schema with Json field: ```prisma model User { id Int @default(autoincrement()) @id name String? extendedProfile Json } ``` You'll end up with a problem that you don't have strict type for `extendedProfile` field in `.ts`. ```typescript const user = prismaService.user.findOne(...); user.extendedProfile // we don't know the type of extendedProfile ``` The one way to fix it, is specify some interface in your code and use it like this: ```typescript interface UserProfile { field1: string; field2: number; } const user = prismaService.user.findOne(...); (user.extendedProfile as UserProfile).field1; // now we have autocompletion ``` But it's not really comfortable to use it like that each time. Also we can create some class and create instance of it like that: ```typescript interface UserProfile { field1: string; field2: number; } class User { id: string; name?: string; extendedProfile: UserProfile; constructor(user: PrismaUser /* user object returned by prisma */) { // ... initialize } } const user = new User(prismaService.user.findOne(...)); ``` But this solution creates some overhead due to the creation of an additional object. ## Suggested solution Maybe we can specify type in `schema.prisma` file like that? ```prisma json ExtendedUserProfileJson { field1 String field2 Int } model User { id Int @default(autoincrement())

Posted by MaximNd on Aug 6, 2020, 8:14 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Other)

Define type of content of `Json` field

MaximNdAug 6, 2020, 8:14 AM

## Problem Right now if you have the following schema with Json field: ```prisma model User { id Int @default(autoincrement()) @id name String? extendedProfile Json } ``` You'll end up with a problem that you don't have strict type for `extendedProfile` field in `.ts`. ```typescript const user = prismaService.user.findOne(...); user.extendedProfile // we don't know the type of extendedProfile ``` The one way to fix it, is specify some interface in your code and use it like this: ```typescript interface UserProfile { field1: string; field2: number; } const user = prismaService.user.findOne(...); (user.extendedProfile as UserProfile).field1; // now we have autocompletion ``` But it's not really comfortable to use it like that each time. Also we can create some class and create instance of it like that: ```typescript interface UserProfile { field1: string; field2: number; } class User { id: string; name?: string; extendedProfile: UserProfile; constructor(user: PrismaUser /* user object returned by prisma */) { // ... initialize } } const user = new User(prismaService.user.findOne(...)); ``` But this solution creates some overhead due to the creation of an additional object. ## Suggested solution Maybe we can specify type in `schema.prisma` file like that? ```prisma json ExtendedUserProfileJson { field1 String field2 Int } model User { id Int @default(autoincrement())

Comments (0)

No comments yet. Start the discussion.

Comment on this issue