1 | <p align="center">
|
2 | <b>Using this package?</b> Please consider <a href="https://github.com/sponsors/arthurfiorette" target="_blank">donating</a> to support my open source work ❤️
|
3 | </p>
|
4 |
|
5 | <br />
|
6 |
|
7 | [![Issues](https://img.shields.io/github/issues/arthurfiorette/prisma-json-types-generator?logo=github&label=Issues)](https://github.com/arthurfiorette/prisma-json-types-generator/issues)
|
8 | [![Stars](https://img.shields.io/github/stars/arthurfiorette/prisma-json-types-generator?logo=github&label=Stars)](https://github.com/arthurfiorette/prisma-json-types-generator/stargazers)
|
9 | [![License](https://img.shields.io/github/license/arthurfiorette/prisma-json-types-generator?logo=githu&label=License)](https://github.com/arthurfiorette/prisma-json-types-generator/blob/main/LICENSE)
|
10 | [![Downloads](https://img.shields.io/npm/dw/prisma-json-types-generator?style=flat)](https://www.npmjs.com/package/prisma-json-types-generator)
|
11 | [![Bundlephobia](https://img.shields.io/bundlephobia/minzip/prisma-json-types-generator/latest?style=flat)](https://bundlephobia.com/package/prisma-json-types-generator@latest)
|
12 | [![Packagephobia](https://packagephobia.com/badge?p=prisma-json-types-generator@latest)](https://packagephobia.com/result?p=prisma-json-types-generator@latest)
|
13 |
|
14 | <h1 align=center>
|
15 | ⚒️ Prisma Json Types Generator
|
16 | </h1>
|
17 |
|
18 | <h3 align=center>
|
19 | A generator that changes the Prisma Client output to strongly type Json fields
|
20 | </h3>
|
21 |
|
22 | <br />
|
23 | <br />
|
24 |
|
25 | ```prisma
|
26 | // schema.prisma
|
27 |
|
28 | generator client {
|
29 | provider = "prisma-client-js"
|
30 | }
|
31 |
|
32 | /// Always after the prisma-client-js generator
|
33 | generator json {
|
34 | provider = "prisma-json-types-generator"
|
35 | // namespace = "PrismaJson"
|
36 | // clientOutput = "<finds it automatically>"
|
37 | // (./ -> relative to schema, or an importable path to require() it)
|
38 | // useType = "MyType"
|
39 | // In case you need to use a type, export it inside the namespace and we will add a index signature to it
|
40 | // (e.g. export namespace PrismaJson { export type MyType = {a: 1, b: 2} }; will generate namespace.MyType["TYPE HERE"])
|
41 | }
|
42 |
|
43 | model Example {
|
44 | /// [MyType]
|
45 | normal Json
|
46 |
|
47 | /// [MyType]
|
48 | optional Json?
|
49 |
|
50 | /// [MyType]
|
51 | array Json[]
|
52 |
|
53 | /// [ComplexType]
|
54 | complex Json
|
55 | }
|
56 | ```
|
57 |
|
58 | Provide type definitions in a file that is part of the `tsconfig.json#includes` paths. For
|
59 | example:
|
60 |
|
61 | ```ts
|
62 | // src/jsonTypes.ts
|
63 |
|
64 | declare global {
|
65 | namespace PrismaJson {
|
66 | // you can use typical basic types
|
67 | type MyType = boolean;
|
68 | // or you can use classes, interfaces, object types, etc.
|
69 | type ComplexType = {
|
70 | foo: string;
|
71 | bar: number;
|
72 | };
|
73 | }
|
74 | }
|
75 | ```
|
76 |
|
77 | When you use your Prisma types in your application code, the JSON columns will now have
|
78 | the types provided under the `PrismaJson` namespace.
|
79 |
|
80 | ```ts
|
81 | // src/example.ts
|
82 |
|
83 | import type { Example } from '@prisma/client';
|
84 |
|
85 | function myFunction(example: Example) {
|
86 | // example.normal is now a boolean
|
87 | // example.optional is now a boolean | null
|
88 | // example.array is now a boolean[]
|
89 | // example.complex is now a { foo: string; bar: number }
|
90 | }
|
91 | ```
|
92 |
|
93 | ### How it works
|
94 |
|
95 | > ⚠️ **It just changes the declaration files of your generated client, no runtime code is
|
96 | > affected!**
|
97 |
|
98 | By using the Typescript Compiler API, this generator parses the generated client's types
|
99 | AST and looks for `Prisma.JsonValue` types [_(or related)_](src/helpers/regex.ts) and
|
100 | replaces them with their corresponding type.
|
101 |
|
102 | ### Some types are still json!
|
103 |
|
104 | There are some complex json types like `JsonFilter` and `JsonWithAggregatesFilter` that,
|
105 | if typed, would impact the usability of the client. So, they are still json.
|
106 |
|
107 | ### Usage with monorepos
|
108 |
|
109 | If you're working with a monorepo, you must make sure the file containing the global
|
110 | definition for `namespace PrismaJson` is part of the runtime imports of your application.
|
111 | If you don't, the types will silently fall back to `any`.
|
112 |
|
113 | ```ts
|
114 | // package1/src/jsonTypes.ts
|
115 | declare global {
|
116 | namespace PrismaJson { /* ... */ }
|
117 | }
|
118 |
|
119 | // package1/src/client.ts
|
120 | import { PrismaClient } from '@prisma/client';
|
121 | import './jsonTypes.ts'; // if this is omitted, types are silently `any` outside of `package1`
|
122 |
|
123 | export const client = new PrismaClient(...);
|
124 | export { Example } from '@prisma/client';
|
125 | ```
|
126 |
|
127 | ### Limitations
|
128 |
|
129 | - This project **should be** a temporary workaround _(and possible solution)_ to
|
130 | https://github.com/prisma/prisma/issues/3219.
|
131 |
|
132 | - Json types inside `type` declarations won't work. (see
|
133 | https://github.com/prisma/prisma/issues/13726)
|