Prisma JSON Types Generator

4.1.1 · active · verified Sun Apr 19

The `prisma-json-types-generator` is a development-time tool that enhances the Prisma client by providing strict TypeScript typings for `Json` and `String` fields, replacing Prisma's default `JsonValue` with custom, user-defined interfaces. The current stable version is `4.1.1`, which primarily supports Prisma 7 and above, with earlier `v3.x` releases supporting Prisma 6. This package operates during the `prisma generate` step, ensuring all type transformations occur at compile-time without introducing any runtime overhead. Its release cadence generally follows major Prisma version updates, with patch releases for bug fixes and minor improvements. Key differentiators include robust type-safety for complex JSON structures, the ability to define string literal enums without relying on native database enums, flexible type configuration through global namespaces or inline annotations, and compatibility with multiple Prisma clients in a single project. It works seamlessly across all database drivers supported by Prisma.

Common errors

Warnings

Install

Quickstart

This quickstart demonstrates how to configure the `prisma-json-types-generator` in `schema.prisma` to apply custom TypeScript types to a `Json` field. It then shows how to interact with the Prisma client, benefiting from compile-time type-safety and autocompletion for the `settings` field.

/* schema.prisma */
generator client {
  provider = "prisma-client-js"
}

generator json {
  provider  = "prisma-json-types-generator"
  namespace = "PrismaJson" // Optional: define a custom namespace for generated types
  allowAny  = false
}

model User {
  id        String @id @default(uuid())
  settings  Json   @db.Text @PrismaJson.UserSettings // Apply custom type
}

/// @PrismaJson.UserSettings
type UserSettings = {
  theme: "dark" | "light";
  notifications: boolean;
  preferences?: { locale: string; timezone: string; };
}

/* index.ts */
import { PrismaClient } from '@prisma/client';
import { UserSettings } from './@prisma/client'; // Import generated type (adjust path as needed if namespace is used)

const prisma = new PrismaClient();

async function main() {
  // Example: Create a user with strongly typed JSON settings
  const user = await prisma.user.create({
    data: {
      settings: {
        theme: 'dark',
        notifications: true,
        preferences: { locale: 'en-US', timezone: 'UTC' }
      },
    },
  });

  console.log('Created user:', user);
  // Accessing settings provides full TypeScript autocompletion and type-checking
  console.log('User theme:', user.settings.theme); // 'dark'

  // Example: Update user settings with type-safety
  const updatedUser = await prisma.user.update({
    where: { id: user.id },
    data: {
      settings: {
        theme: 'light', // Autocompletion for 'dark' | 'light'
        notifications: false,
        preferences: { locale: 'es-ES', timezone: 'America/Mexico_City' }
      },
    },
  });

  console.log('Updated user:', updatedUser);
  // If you try to assign an invalid type, TypeScript will catch it at compile time.
  // e.g., updatedUser.settings.theme = 'invalid'; // Type error

}

main().catch(console.error).finally(() => prisma.$disconnect());

view raw JSON →