@innovixx/prisma-typed-json-generator
v0.0.4
Published
A Prisma generator that automatically creates TypeScript types for your `Json` fields with `@type()` annotations, and patches the generated Prisma Client types to use your custom types.
Readme
prisma-typed-json-generator
A Prisma generator that automatically creates TypeScript types for your Json fields with @type() annotations, and patches the generated Prisma Client types to use your custom types.
Features
- Extracts custom types from commented blocks in your Prisma schema.
- Generates a
prisma-json.types.tsfile in your Prisma Client output directory. - Patches the generated
index.d.tsto use your custom types for annotatedJsonfields, using a namespace import to avoid type name clashes. - Context-aware: only patches the correct field in the correct model, even if multiple models use the same field name.
Usage
- Add the generator to your
schema.prisma:
generator client {
provider = "prisma-client-js"
output = "./client"
}
generator prisma_types_json_generator {
provider = "prisma-typed-json-generator"
output = "../types"
}- Annotate your
Jsonfields with@type()and define your types in comments:
// type Address {
// street string
// city string
// country string
// }
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
address Json // @type(Address)
}- Run
prisma generate:
npx prisma generate- The generator will:
- Create
prisma-json.types.tsin your client output directory. - Patch
index.d.tsto usePrismaJsonTypes.Addressfor theaddressfield in theUsermodel.
- Create
Why?
Prisma's Json fields are typed as Prisma.JsonValue by default, which is not type-safe. This generator lets you use your own types for better safety and DX.
License
MIT
