
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
prisma-json-types-generator
Advanced tools
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[]
/// [ComplexType]
complex Json
}
Provide type definitions in a file that is part of the tsconfig.json#includes
paths. For
example:
// src/jsonTypes.ts
declare global {
namespace PrismaJson {
// you can use typical basic types
type MyType = boolean;
// or you can use classes, interfaces, object types, etc.
type ComplexType = {
foo: string;
bar: number;
};
}
}
When you use your Prisma types in your application code, the JSON columns will now have
the types provided under the PrismaJson
namespace.
// src/example.ts
import type { Example } from '@prisma/client';
function myFunction(example: Example) {
// 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 }
}
⚠️ 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.
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/jsonTypes.ts
declare global {
namespace PrismaJson { /* ... */ }
}
// package1/src/client.ts
import { PrismaClient } from '@prisma/client';
import './jsonTypes.ts'; // if this is omitted, types are silently `any` outside of `package1`
export const client = new PrismaClient(...);
export { Example } from '@prisma/client';
This project should be a temporary workaround (and possible solution) to https://github.com/prisma/prisma/issues/3219.
Json types inside type
declarations won't work. (see
https://github.com/prisma/prisma/issues/13726)
FAQs
Changes JsonValues to your custom typescript type
The npm package prisma-json-types-generator receives a total of 146,604 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.