{"id":15442179,"url":"https://github.com/arthurfiorette/prisma-json-types-generator","last_synced_at":"2025-05-14T13:05:46.922Z","repository":{"id":65182102,"uuid":"585913181","full_name":"arthurfiorette/prisma-json-types-generator","owner":"arthurfiorette","description":"⚒️ Changes JsonValues to your custom typescript type.","archived":false,"fork":false,"pushed_at":"2025-05-08T17:40:00.000Z","size":1414,"stargazers_count":442,"open_issues_count":4,"forks_count":31,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-05-08T18:28:29.280Z","etag":null,"topics":["generator","json","prisma","safe","safety","type","types","typescript"],"latest_commit_sha":null,"homepage":"https://arthur.run/prisma-json-types-generator/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arthurfiorette.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"arthurfiorette"}},"created_at":"2023-01-06T12:33:01.000Z","updated_at":"2025-05-08T17:39:50.000Z","dependencies_parsed_at":"2023-01-09T07:37:35.483Z","dependency_job_id":"30d86e0e-9af7-4407-939a-c47176d78f02","html_url":"https://github.com/arthurfiorette/prisma-json-types-generator","commit_stats":{"total_commits":371,"total_committers":14,"mean_commits":26.5,"dds":"0.37735849056603776","last_synced_commit":"c9e03bef2e94bcd369b9c15ec2947918b31dd4e8"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthurfiorette%2Fprisma-json-types-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthurfiorette%2Fprisma-json-types-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthurfiorette%2Fprisma-json-types-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arthurfiorette%2Fprisma-json-types-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arthurfiorette","download_url":"https://codeload.github.com/arthurfiorette/prisma-json-types-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254149919,"owners_count":22022851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generator","json","prisma","safe","safety","type","types","typescript"],"created_at":"2024-10-01T19:25:37.086Z","updated_at":"2025-05-14T13:05:46.910Z","avatar_url":"https://github.com/arthurfiorette.png","language":"TypeScript","funding_links":["https://github.com/sponsors/arthurfiorette"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n   \u003cb\u003eUsing this package?\u003c/b\u003e Please consider \u003ca href=\"https://github.com/sponsors/arthurfiorette\" target=\"_blank\"\u003edonating\u003c/a\u003e to support my open source work ❤️\n  \u003cbr /\u003e\n  \u003csup\u003e\n   Help prisma-json-types-generator grow! Star and share this amazing repository with your friends and co-workers!\n  \u003c/sup\u003e\n\u003c/p\u003e\n\n\u003cbr /\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca title=\"MIT license\" target=\"_blank\" href=\"https://github.com/arthurfiorette/prisma-json-types-generator/blob/main/LICENSE\"\u003e\u003cimg alt=\"License\" src=\"https://img.shields.io/github/license/arthurfiorette/prisma-json-types-generator\"\u003e\u003c/a\u003e\n  \u003ca title=\"NPM Package\" target=\"_blank\" href=\"https://www.npmjs.com/package/prisma-json-types-generator\"\u003e\u003cimg alt=\"Downloads\" src=\"https://img.shields.io/npm/dw/prisma-json-types-generator?style=flat\"\u003e\u003c/a\u003e\n  \u003ca title=\"Install size\" target=\"_blank\" href=\"https://packagephobia.com/result?p=prisma-json-types-generator@latest\"\u003e\u003cimg alt=\"Packagephobia\" src=\"https://packagephobia.com/badge?p=prisma-json-types-generator@latest\"\u003e\u003c/a\u003e\n  \u003ca title=\"Last Commit\" target=\"_blank\" href=\"https://github.com/arthurfiorette/prisma-json-types-generator/commits/main\"\u003e\u003cimg alt=\"Last commit\" src=\"https://img.shields.io/github/last-commit/arthurfiorette/prisma-json-types-generator\"\u003e\u003c/a\u003e\n  \u003ca title=\"Blazingly fast\" target=\"_blank\" href=\"https://twitter.com/acdlite/status/974390255393505280\"\u003e\u003cimg src=\"https://img.shields.io/badge/blazingly-fast-fa3737\"/\u003e\u003c/a\u003e\n\n\u003c/p\u003e\n\n\u003cbr /\u003e\n\u003cbr /\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/arthurfiorette/prisma-json-types-generator/refs/heads/main/images/logo.png\" /\u003e\n\u003c/p\u003e\n\n\u003ch1\u003ePrisma Json Types Generator\u003c/h1\u003e\n\n\u003e Generate your prisma client with strict JSON types and String literals!\n\n\u003cbr /\u003e\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Typing JSON Fields](#typing-json-fields)\n  - [Example Schema](#example-schema)\n  - [Example Type Declarations](#example-type-declarations)\n  - [Resulting Type Inference](#resulting-type-inference)\n- [Configuration Options](#configuration-options)\n- [Limitations](#limitations)\n- [Advanced Usage](#advanced-usage)\n  - [Monorepos](#monorepos)\n  - [Inline Type Declarations](#inline-type-declarations)\n- [How It Works](#how-it-works)\n- [License](#license)\n\n\u003cbr /\u003e\n\u003cbr /\u003e\n\nPrisma 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.\n\n\u003cbr /\u003e\n\n## Installation\n\nInstall the package as a development dependency:\n\n```bash\nnpm install -D prisma-json-types-generator\n```\n\n\u003cbr /\u003e\n\n## Usage\n\n1. **Add the generator to your Prisma schema**:\n   Place the generator below the `prisma-client-js` generator in your `schema.prisma`:\n\n   ```prisma\n   generator client {\n     provider = \"prisma-client-js\"\n   }\n\n   generator json {\n     provider = \"prisma-json-types-generator\"\n   }\n   ```\n\n2. **Define your JSON types**:\n   Create a TypeScript file containing the type declarations. This file must be included in your `tsconfig`.\n\n   ```ts\n   // types.ts\n\n   declare global {\n     namespace PrismaJson {\n       // Define your custom types here!\n     }\n   }\n\n   // The file MUST be a module! \n   export {};\n   ```\n\n\u003cbr /\u003e\n\n## Typing JSON Fields\n\nThis 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.\n\n### Example Schema\n\n```prisma\nmodel Example {\n  /// [MyType]\n  normal Json\n\n  /// [MyType] comments are allowed after the type definition!\n  comment Json\n\n  /// [MyType]\n  optional Json?\n\n  /// [MyType]\n  array Json[]\n\n  /// [ComplexType]\n  complex Json\n\n  /// !['A' | 'B']\n  literal Json\n\n  /// ![[('A' | 'B')[], number[][][][]]]\n  literalArray Json\n\n  /// ![PrismaJson.MyType | 'none']\n  anything Json[]\n}\n```\n\n### Example Type Declarations\n\n```ts\ndeclare global {\n  namespace PrismaJson {\n    type MyType = boolean;\n    type ComplexType = { foo: string; bar: number };\n  }\n}\n```\n\n### Resulting Type Inference\n\n```ts\nimport type { Example } from '@prisma/client';\n\n// example.normal   -\u003e boolean\n// example.optional -\u003e boolean | null\n// example.array    -\u003e boolean[]\n// example.complex  -\u003e { foo: string; bar: number }\n// example.literal  -\u003e 'A' | 'B'\n// example.anything -\u003e PrismaJson.MyType | 'none'\n```\n\n\u003cbr /\u003e\n\n## Configuration Options\n\n```prisma\ngenerator json {\n  provider = \"prisma-json-types-generator\"\n\n  // Specify the namespace for type generation.\n  // Default: \"PrismaJson\"\n  // namespace = \"PrismaJson\"\n\n  // Define the client output path.\n  // Default: Automatically determined.\n  // clientOutput = \"automatic\"\n\n  // Add an index signature to a root type for dynamic JSON fields.\n  // useType = \"GlobalType\"\n\n  // Use `any` instead of `unknown` for untyped JSON fields.\n  // allowAny = false\n}\n```\n\n\u003cbr /\u003e\n\n## Limitations\n\n- **Complex Filters**: Types like `JsonFilter` and `JsonWithAggregatesFilter` remain untyped to preserve functionality.\n- **Prisma v4 Support**: Only Prisma v5+ and generator v3+ are supported.\n- **Known Gaps**: If some JSON fields are missing types, open an issue in the repository.\n\n\u003cbr /\u003e\n\n## Advanced Usage\n\n### Monorepos\n\nIn monorepos, ensure the file containing the `PrismaJson` namespace is part of the runtime imports. Otherwise, types will default to `any`.\n\n```ts\n// package1/src/types.ts\n\ndeclare global {\n  namespace PrismaJson {\n    // Define types here\n  }\n}\n```\n\n```ts\n// package2/src/client.ts\n\n// Import the type definitions\nimport 'package1/types.ts';\nimport { PrismaClient } from '@prisma/client';\n\nexport const client = new PrismaClient();\n```\n\n### Inline Type Declarations\n\nDirectly declare types in the schema:\n\n```prisma\nmodel Example {\n  /// ![Record\u003cstring, string\u003e]\n  map Json\n}\n```\n\n\u003cbr /\u003e\n\n## How It Works\n\nThis 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](src/handler/replace-object.ts).\n\n\u003cbr /\u003e\n\n## License\n\nLicensed under the **MIT**. See [`LICENSE`](LICENSE) for more informations.\n\n\u003cbr /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farthurfiorette%2Fprisma-json-types-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farthurfiorette%2Fprisma-json-types-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farthurfiorette%2Fprisma-json-types-generator/lists"}