question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define type of content of Json field

See original GitHub issue

Problem

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:open
  • Created 3 years ago
  • Reactions:480
  • Comments:81 (10 by maintainers)

github_iconTop GitHub Comments

94reactions
janpiocommented, Aug 6, 2020

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.

66reactions
eakriulincommented, Feb 16, 2022

Up — we really need this feature 🚀

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found