
Product
A Fresh Look for the Socket Dashboard
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
prisma-json-types-generator
Advanced tools
Using this package? Please consider donating to support my open source work ❤️
Help prisma-json-types-generator grow! Star and share this amazing repository with your friends and co-workers!
Generate your prisma client with strict JSON types and String literals!
Prisma JSON Types Generator enhances your @prisma/client
by replacing all JSON types with your specified TypeScript types. This ensures a stricter type-checking layer, requiring your JSON types to conform to TypeScript rules before insertion into the database.
Install the package as a development dependency:
npm install -D prisma-json-types-generator
Add the generator to your Prisma schema:
Place the generator below the prisma-client-js
generator in your schema.prisma
:
generator client {
provider = "prisma-client-js"
}
generator json {
provider = "prisma-json-types-generator"
}
Define your JSON types:
Create a TypeScript file containing the type declarations. This file must be included in your tsconfig
.
// types.ts
declare global {
namespace PrismaJson {
// Define your custom types here!
}
}
// The file MUST be a module!
export {};
This generator allows multiple approaches to type Json
or String
fields, avoiding circular dependencies between the Prisma schema and your codebase. Global namespaces (PrismaJson
) provide a centralized location for your type definitions.
model Example {
/// [MyType]
normal Json
/// [MyType] comments are allowed after the type definition!
comment Json
/// [MyType]
optional Json?
/// [MyType]
array Json[]
/// [ComplexType]
complex Json
/// !['A' | 'B']
literal Json
/// ![[('A' | 'B')[], number[][][][]]]
literalArray Json
/// ![PrismaJson.MyType | 'none']
anything Json[]
}
declare global {
namespace PrismaJson {
type MyType = boolean;
type ComplexType = { foo: string; bar: number };
}
}
import type { Example } from '@prisma/client';
// example.normal -> boolean
// example.optional -> boolean | null
// example.array -> boolean[]
// example.complex -> { foo: string; bar: number }
// example.literal -> 'A' | 'B'
// example.anything -> PrismaJson.MyType | 'none'
generator json {
provider = "prisma-json-types-generator"
// Specify the namespace for type generation.
// Default: "PrismaJson"
// namespace = "PrismaJson"
// Define the client output path.
// Default: Automatically determined.
// clientOutput = "automatic"
// Add an index signature to a root type for dynamic JSON fields.
// useType = "GlobalType"
// Use `any` instead of `unknown` for untyped JSON fields.
// allowAny = false
}
JsonFilter
and JsonWithAggregatesFilter
remain untyped to preserve functionality.In monorepos, ensure the file containing the PrismaJson
namespace is part of the runtime imports. Otherwise, types will default to any
.
// package1/src/types.ts
declare global {
namespace PrismaJson {
// Define types here
}
}
// package2/src/client.ts
// Import the type definitions
import 'package1/types.ts';
import { PrismaClient } from '@prisma/client';
export const client = new PrismaClient();
Directly declare types in the schema:
model Example {
/// ![Record<string, string>]
map Json
}
This generator leverages the TypeScript Compiler API to analyze the client's type definitions. It identifies Prisma.JsonValue
(or related) types and replaces them with the specified types using AST transformations. For details, see the implementation.
Licensed under the MIT. See LICENSE
for more informations.
FAQs
Changes JsonValues to your custom typescript type
The npm package prisma-json-types-generator receives a total of 87,693 weekly downloads. As such, prisma-json-types-generator popularity was classified as popular.
We found that prisma-json-types-generator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
Industry Insights
Terry O’Daniel, Head of Security at Amplitude, shares insights on building high-impact security teams, aligning with engineering, and why AI gives defenders a fighting chance.
Security News
MCP spec updated with structured tool output, stronger OAuth 2.1 security, resource indicators, and protocol cleanups for safer, more reliable AI workflows.