prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
39.02k stars 1.52k forks source link

Deeper types can't be used as InputJsonValue #14244

Open Faithfinder opened 2 years ago

Faithfinder commented 2 years ago

Bug description

When your input type definition has deeper objects, it can't be used as input for JSON type columns.

How to reproduce

Repro: https://codesandbox.io/s/prisma-json-input-type-bad-134mdg

Code copypaste:

import { PrismaClient } from "@prisma/client";

const client = new PrismaClient();

interface UserMeta {
  someObject?: SomeObjectProp;
};

interface SomeObjectProp {
  prop1: string;
}

const meta: UserMeta = {};

client.user.create({
  data: {
    email: "email",
    meta
    // Type 'UserMeta' is not assignable to type 'string | number | boolean | InputJsonObject | InputJsonArray | undefined'.
    //Type 'UserMeta' is not assignable to type 'InputJsonObject'.
    //Index signature for type 'string' is missing in type 'UserMeta'.ts(2322)
  }
});

Expected behavior

"Just works"

Prisma information

Irrelevant, repro included

Environment & setup

Every, it's a typescript problem.

Prisma Version

prisma                  : 4.0.0
@prisma/client          : 4.0.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : da41d2bb3406da22087b849f0e911199ba4fbf11
Studio                  : 0.465.0
sabinadams commented 2 years ago

Hey there!

Similar to the documentation here for writing an Array of Json objects, you will have to specify the JsonObject type when writing the data. For example:

import { PrismaClient, Prisma } from "../prisma/built";

const client = new PrismaClient();

interface UserMeta {
  someObject?: SomeObjectProp;
}

interface SomeObjectProp {
  prop1: string;
}

const meta: UserMeta = {};

client.user.create({
  data: {
    email: "email",
    meta: meta as Prisma.JsonObject
  }
});
Faithfinder commented 2 years ago

You can always typecast as a Typescript workaround, sure. Typecasting should be last resort though, IMO, and ideally not an official way to work with a library.

pantharshit00 commented 2 years ago

I can reproduce the original problem with the latest version.

SevInf commented 2 years ago

Reproduction https://github.com/prisma/reproductions/tree/main/14244