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
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
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())
Comments (0)
No comments yet. Start the discussion.