
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
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 is a prisma client generator which changes all json types from
your @prisma/client
into the ones you specified. It adds another layer of checking
within your schema, as you must make typescript happy with your json type before inserting
it into your DB.
generator client {
provider = "prisma-client-js"
}
generator json {
provider = "prisma-json-types-generator"
}
model Example {
/// [MyType]
normal Json
}
npm install -D prisma-json-types-generator
Include it in your schema and provide your own namespace declarations inside a file included in your tsconfig.
// schema.prisma
generator client {
provider = "prisma-client-js"
}
/// Always after the prisma-client-js generator
generator json {
provider = "prisma-json-types-generator"
}
// types.ts
declare global {
namespace PrismaJson {
// Insert your types here!
}
}
generator json {
provider = "prisma-json-types-generator"
// The namespace to generate the types in.
namespace = "PrismaJson"
// The name of the client output type. By default it will try
// to find it automatically
// (./ -> relative to schema, or an importable path to require() it)
clientOutput = "finds automatically"
// In case you need to use a root type inside PrismaJson, export it
// inside the namespace and we will add a index signature to it
useType = "PrismaJson.GlobalType"
// If untyped JSON fields should be any instead of `unknown`.
allowAny = false
}
This package adds multiple ways to type a JSON
or String
field.
We need to avoid circular dependencies between the prisma schema and your codebase, and
typescript namespaces are the best way to solve this. As we declare a global namespace,
any type declared within a normal declaration (/// [T]
) gets transpiled to Namespace.T
inside the prisma schema.
Sometimes you have a very simple type, like a union literal or some constants, using a
literal declaration (/// ![T]
) will probably be better, as it gets transpiled to onlt
T
inside the schema. This way, types like /// ['some' | 'none']
that are only used
once, are way more easy to write and maintain.
model Example {
/// [MyType]
normal Json
/// [MyType]
optional Json?
/// [MyType]
array Json[]
/// [ComplexType]
complex Json
/// !['A' | 'B']
literal Json
/// ![PrismaJson.MyType | 'none']
anything Json[]
}
declare global {
// you can use typical basic types
// or you can use classes, interfaces, object types, etc.
namespace PrismaJson {
type MyType = boolean;
type ComplexType = { foo: string; bar: number };
}
}
// Example#normal is now a boolean
// Example#optional is now a boolean | null
// Example#array is now a boolean[]
// Example#complex is now a { foo: string; bar: number }
// Example#literal is now a 'A' | 'B'
// Example#anything is now a boolean | 'none'
Yes! And it is right! Some complex filter types like JsonFilter
or
JsonWithAggregatesFilter
must not be typed, as we cannot change the object signature,
only its types, mutating these objects would make the where usage less powerfull and
probably lose functionality. So no, not all types will be converted. However if you find a
field which is missing types, please open an issue in this repository.
If you're working with a monorepo, you must make sure the file containing the global
definition for namespace PrismaJson
is part of the runtime imports of your application.
If you don't, the types will silently fall back to any.
// package1/src/types.ts
declare global {
namespace PrismaJson {
// ...
}
}
// package2/src/client.ts
// Manually import the definition.
import 'package1/types.ts';
import { PrismaClient } from '@prisma/client';
export const client = new PrismaClient(...);
4.16.0
. (see
prisma/prisma#13726)By using the Typescript Compiler API, this generator parses the generated client's types
AST and looks for Prisma.JsonValue
(or related) types
and replaces them with their corresponding type.
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 117,003 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.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.