Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less restrictive JSON types on create/insert #24054

Open
raclettes opened this issue May 1, 2024 · 2 comments
Open

Less restrictive JSON types on create/insert #24054

raclettes opened this issue May 1, 2024 · 2 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: client types Types in Prisma Client topic: create() topic: Json Scalar type `Json`

Comments

@raclettes
Copy link

Problem

When using the Prisma client (with Postgres in my case) to update data in the database, I've frequently been slowed down by the more restrictive types of the insert and create functions. For example, given the model

model MyModel {
  id String  @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  myData    Json?   @db.Json
}

And attempting to insert data that follows the type

interface CustomData {
    customKey: number
}

interface MyCustomModelInterface {
    id?: string;
    myData: CustomData;
}

You are often met with a message like

Types of property 'myData' are incompatible.
        Type 'CustomData' is not assignable to type 'NullableJsonNullValueInput | InputJsonValue'.
          Type 'CustomData' is not assignable to type 'InputJsonObject'.
            Index signature for type 'string' is missing in type 'CustomData'

Even though CustomData does satisfy the requirements for the JSON field (on retrieval/return I understand there is no guarantee of the structure, but on insert there should be a guarantee)

Suggested solution

If possible, .insert and .create (along with the types MyModelCreateWithout... etc) should ideally be able to allow types that satisfy the requirements of the JSON field instead of strictly requiring that it matches every possible JSON type. While I understand this is likely a TypeScript limitation, potentially there could be investigation into alternative solutions?

Alternatives

Potentially allow declaration and/or validation of JSON fields coming from the database (although this could contradict the goal of the Json field type).

Additional context

N/A

@SevInf SevInf added kind/bug A reported bug. topic: client types Types in Prisma Client topic: Json Scalar type `Json` bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. labels May 2, 2024
@jkomyno
Copy link
Contributor

jkomyno commented May 16, 2024

Hi @raclettes, what version of Prisma are you using?

@jkomyno jkomyno added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels May 16, 2024
@jkomyno
Copy link
Contributor

jkomyno commented May 16, 2024

Huh, I've found something curious. Looks like rewriting CustomData as a type rather than an interface works well:

import { PrismaClient } from '.prisma/client'

type CustomData = {
  customKey: number
}

interface MyCustomModelInterface {
  readonly id?: string;
  readonly myData: CustomData;
}

async function main() {
  const prisma = new PrismaClient()

  const myCustomModel: MyCustomModelInterface = {
    myData: {
      customKey: 1,
    },
    id: '1',
  }


  const myModel = await prisma.myModel.create({
    data: {
      id: myCustomModel.id,
      myData: myCustomModel.myData,
    },
  })

  console.dir(myModel, { depth: null })
}

void main().catch((e) => {
  console.log(e.message)
  process.exit(1)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: client types Types in Prisma Client topic: create() topic: Json Scalar type `Json`
Projects
None yet
Development

No branches or pull requests

4 participants