Define type of content of Json field
See original GitHub issueProblem
Right now if you have the following schema with Json field:
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
.
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:
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:
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?
json ExtendedUserProfileJson {
field1 String
field2 Int
}
model User {
id Int @default(autoincrement()) @id
name String?
extendedProfile ExtendedUserProfileJson
}
Alternatives
Alternatively, we can somehow manage this in the typescript.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:480
- Comments:81 (10 by maintainers)
Top Results From Across the Web
Working with Json fields (Concepts) - Prisma
Use the Json Prisma field type to read, write, and perform basic filtering on JSON types in the underlying database. In the following...
Read more >Documentation: 15: 8.14. JSON Types - PostgreSQL
JSON data types are for storing JSON (JavaScript Object Notation) data, as specified in RFC 7159. Such data can also be stored as...
Read more >JSON Data Types - W3Schools
In JSON, values must be one of the following data types: a string; a number; an object (JSON object); an array; a boolean;...
Read more >JSON schema for creating a content type - Contentstack
Here's the JSON schema of all the fields and how you can use them in the content type JSON file. Title. The Title...
Read more >JSON | Data Types - GeeksforGeeks
string; number; boolean; null ; object; array. Note: string, number, boolean, null are simple data types or primitives data types whereas ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Being able to type your Json fields is a simple and understandable feature requests. Although there might be workarounds, this might very well be something that Prisma could offer in the future on its own - so having this feature request is valid.
Up — we really need this feature 🚀