Using this package? Please consider donating to support my open source work ❤️
// schema.prisma
generator client {
provider = "prisma-client-js"
}
/// Always after the prisma-client-js generator
generator json {
provider = "prisma-json-types-generator"
// namespace = "PrismaJson"
// clientOutput = "<finds it automatically>"
// (./ -> relative to schema, or an importable path to require() it)
// useType = "MyType"
// In case you need to use a type, export it inside the namespace and we will add a index signature to it
// (e.g. export namespace PrismaJson { export type MyType = {a: 1, b: 2} }; will generate namespace.MyType["TYPE HERE"])
}
model Example {
/// [MyType]
normal Json
/// [MyType]
optional Json?
/// [MyType]
array Json[]
}
// index.ts
import type { Example } from '@prisma/client';
declare global {
namespace PrismaJson {
// you can use classes, interfaces, types, etc.
type MyType = boolean;
}
}
function myFunction(example: Example) {
// example.normal is now a boolean
// example.optional is now a boolean | null
// example.array is now a boolean[]
}
⚠️ It just changes the declaration files of your generated client, no runtime code is affected!
By using the Typescript Compiler API, this generator parses the generated client's types
AST and looks for Prisma.JsonValue
types (or related) and
replaces them with their corresponding type.
There are some complex json types like JsonFilter
and JsonWithAggregatesFilter
that,
if typed, would impact the usability of the client. So, they are still json.
-
This project should be a temporary workaround (and possible solution) to prisma/prisma#3219.
-
Json types inside
type
declarations won't work. (see prisma/prisma#13726)