UNPKG

3.06 kBMarkdownView Raw
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>
19A 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
28generator client {
29 provider = "prisma-client-js"
30}
31
32/// Always after the prisma-client-js generator
33generator 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}
39
40model Example {
41 /// [MyType]
42 normal Json
43
44 /// [MyType]
45 optional Json?
46
47 /// [MyType]
48 array Json[]
49}
50```
51
52```ts
53// index.ts
54
55import type { Example } from '@prisma/client';
56
57declare global {
58 namespace PrismaJson {
59 // you can use classes, interfaces, types, etc.
60 type MyType = boolean;
61 }
62}
63
64function myFunction(example: Example) {
65 // example.normal is now a boolean
66 // example.optional is now a boolean | null
67 // example.array is now a boolean[]
68}
69```
70
71### How it works
72
73> ⚠️ **It just changes the declaration files of your generated client, no runtime code is
74> affected!**
75
76By using the Typescript Compiler API, this generator parses the generated client's types
77AST and looks for `Prisma.JsonValue` types [_(or related)_](src/helpers/regex.ts) and
78replaces them with their corresponding type.
79
80### Some types are still json!
81
82There are some complex json types like `JsonFilter` and `JsonWithAggregatesFilter` that,
83if typed, would impact the usability of the client. So, they are still json.
84
85### Limitations
86
87- This project **should be** a temporary workaround _(and possible solution)_ to
88 https://github.com/prisma/prisma/issues/3219.
89
90- Json types inside `type` declarations won't work. (see
91 https://github.com/prisma/prisma/issues/13726)