@visulima/prisma-dmmf-transformer 2.0.20 → 2.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +191 -0
- package/dist/index.d.mts +168 -3
- package/dist/index.d.ts +168 -3
- package/dist/index.mjs +1 -7
- package/dist/packem_shared/getJSONSchemaProperty-2XKY7Nu4.mjs +1 -0
- package/dist/packem_shared/getJSONSchemaProperty-CapSYG-2.cjs +1 -0
- package/dist/packem_shared/transformDMMF-Ccqk94NT.cjs +1 -0
- package/dist/packem_shared/transformDMMF-KYYhvd46.mjs +1 -0
- package/package.json +21 -11
- package/dist/index.js +0 -14
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
## @visulima/prisma-dmmf-transformer [2.0.22](https://github.com/visulima/visulima/compare/@visulima/prisma-dmmf-transformer@2.0.21...@visulima/prisma-dmmf-transformer@2.0.22) (2024-09-24)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
* update packem to v1 ([05f3bc9](https://github.com/visulima/visulima/commit/05f3bc960df10a1602e24f9066e2b0117951a877))
|
|
6
|
+
* updated esbuild from v0.23 to v0.24 ([3793010](https://github.com/visulima/visulima/commit/3793010d0d549c0d41f85dea04b8436251be5fe8))
|
|
7
|
+
|
|
8
|
+
### Miscellaneous Chores
|
|
9
|
+
|
|
10
|
+
* updated dev dependencies ([05edb67](https://github.com/visulima/visulima/commit/05edb671285b1cc42875223314b24212e6a12588))
|
|
11
|
+
|
|
12
|
+
## @visulima/prisma-dmmf-transformer [2.0.21](https://github.com/visulima/visulima/compare/@visulima/prisma-dmmf-transformer@2.0.20...@visulima/prisma-dmmf-transformer@2.0.21) (2024-09-12)
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* **prisma-dmmf-transformer:** moved from tsup to packem ([39ae471](https://github.com/visulima/visulima/commit/39ae4716c64a2488fd10df64d5c3adc4ae7a5937))
|
|
17
|
+
|
|
18
|
+
### Miscellaneous Chores
|
|
19
|
+
|
|
20
|
+
* updated dev dependencies ([ac67ec1](https://github.com/visulima/visulima/commit/ac67ec1bcba16175d225958e318199f60b10d179))
|
|
21
|
+
* updated dev dependencies and sorted the package.json ([9571572](https://github.com/visulima/visulima/commit/95715725a8ed053ca24fd1405a55205c79342ecb))
|
|
22
|
+
|
|
1
23
|
## @visulima/prisma-dmmf-transformer [2.0.20](https://github.com/visulima/visulima/compare/@visulima/prisma-dmmf-transformer@2.0.19...@visulima/prisma-dmmf-transformer@2.0.20) (2024-06-19)
|
|
2
24
|
|
|
3
25
|
### Bug Fixes
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./packem_shared/getJSONSchemaProperty-CapSYG-2.cjs"),r=require("./packem_shared/transformDMMF-Ccqk94NT.cjs");exports.getJSONSchemaProperty=e;exports.transformDMMF=r;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { ReadonlyDeep, DMMF } from '@prisma/generator-helper';
|
|
2
|
+
|
|
3
|
+
// ==================================================================================================
|
|
4
|
+
// JSON Schema Draft 07
|
|
5
|
+
// ==================================================================================================
|
|
6
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
7
|
+
// --------------------------------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Primitive type
|
|
11
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
12
|
+
*/
|
|
13
|
+
type JSONSchema7TypeName =
|
|
14
|
+
| "string" //
|
|
15
|
+
| "number"
|
|
16
|
+
| "integer"
|
|
17
|
+
| "boolean"
|
|
18
|
+
| "object"
|
|
19
|
+
| "array"
|
|
20
|
+
| "null";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Primitive type
|
|
24
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
25
|
+
*/
|
|
26
|
+
type JSONSchema7Type =
|
|
27
|
+
| string //
|
|
28
|
+
| number
|
|
29
|
+
| boolean
|
|
30
|
+
| JSONSchema7Object
|
|
31
|
+
| JSONSchema7Array
|
|
32
|
+
| null;
|
|
33
|
+
|
|
34
|
+
// Workaround for infinite type recursion
|
|
35
|
+
interface JSONSchema7Object {
|
|
36
|
+
[key: string]: JSONSchema7Type;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Workaround for infinite type recursion
|
|
40
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
41
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Meta schema
|
|
45
|
+
*
|
|
46
|
+
* Recommended values:
|
|
47
|
+
* - 'http://json-schema.org/schema#'
|
|
48
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
49
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
50
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
51
|
+
*
|
|
52
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
53
|
+
*/
|
|
54
|
+
type JSONSchema7Version = string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* JSON Schema v7
|
|
58
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
59
|
+
*/
|
|
60
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
61
|
+
interface JSONSchema7 {
|
|
62
|
+
$id?: string | undefined;
|
|
63
|
+
$ref?: string | undefined;
|
|
64
|
+
$schema?: JSONSchema7Version | undefined;
|
|
65
|
+
$comment?: string | undefined;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
69
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
70
|
+
*/
|
|
71
|
+
$defs?: {
|
|
72
|
+
[key: string]: JSONSchema7Definition;
|
|
73
|
+
} | undefined;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
77
|
+
*/
|
|
78
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
79
|
+
enum?: JSONSchema7Type[] | undefined;
|
|
80
|
+
const?: JSONSchema7Type | undefined;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
84
|
+
*/
|
|
85
|
+
multipleOf?: number | undefined;
|
|
86
|
+
maximum?: number | undefined;
|
|
87
|
+
exclusiveMaximum?: number | undefined;
|
|
88
|
+
minimum?: number | undefined;
|
|
89
|
+
exclusiveMinimum?: number | undefined;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
93
|
+
*/
|
|
94
|
+
maxLength?: number | undefined;
|
|
95
|
+
minLength?: number | undefined;
|
|
96
|
+
pattern?: string | undefined;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
100
|
+
*/
|
|
101
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
102
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
|
103
|
+
maxItems?: number | undefined;
|
|
104
|
+
minItems?: number | undefined;
|
|
105
|
+
uniqueItems?: boolean | undefined;
|
|
106
|
+
contains?: JSONSchema7Definition | undefined;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
110
|
+
*/
|
|
111
|
+
maxProperties?: number | undefined;
|
|
112
|
+
minProperties?: number | undefined;
|
|
113
|
+
required?: string[] | undefined;
|
|
114
|
+
properties?: {
|
|
115
|
+
[key: string]: JSONSchema7Definition;
|
|
116
|
+
} | undefined;
|
|
117
|
+
patternProperties?: {
|
|
118
|
+
[key: string]: JSONSchema7Definition;
|
|
119
|
+
} | undefined;
|
|
120
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
|
121
|
+
dependencies?: {
|
|
122
|
+
[key: string]: JSONSchema7Definition | string[];
|
|
123
|
+
} | undefined;
|
|
124
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
128
|
+
*/
|
|
129
|
+
if?: JSONSchema7Definition | undefined;
|
|
130
|
+
then?: JSONSchema7Definition | undefined;
|
|
131
|
+
else?: JSONSchema7Definition | undefined;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
135
|
+
*/
|
|
136
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
|
137
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
|
138
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
|
139
|
+
not?: JSONSchema7Definition | undefined;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
143
|
+
*/
|
|
144
|
+
format?: string | undefined;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
148
|
+
*/
|
|
149
|
+
contentMediaType?: string | undefined;
|
|
150
|
+
contentEncoding?: string | undefined;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
154
|
+
*/
|
|
155
|
+
definitions?: {
|
|
156
|
+
[key: string]: JSONSchema7Definition;
|
|
157
|
+
} | undefined;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
161
|
+
*/
|
|
162
|
+
title?: string | undefined;
|
|
163
|
+
description?: string | undefined;
|
|
164
|
+
default?: JSONSchema7Type | undefined;
|
|
165
|
+
readOnly?: boolean | undefined;
|
|
166
|
+
writeOnly?: boolean | undefined;
|
|
167
|
+
examples?: JSONSchema7Type | undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface PropertyMetaData {
|
|
171
|
+
hasDefaultValue: boolean;
|
|
172
|
+
isScalar: boolean;
|
|
173
|
+
required: boolean;
|
|
174
|
+
}
|
|
175
|
+
interface ModelMetaData {
|
|
176
|
+
enums: ReadonlyDeep<DMMF.DatamodelEnum[]>;
|
|
177
|
+
}
|
|
178
|
+
type DefinitionMap = [name: string, definition: JSONSchema7Definition];
|
|
179
|
+
type PropertyMap = [...DefinitionMap, PropertyMetaData];
|
|
180
|
+
interface TransformOptions {
|
|
181
|
+
includeRequiredFields?: "false" | "true";
|
|
182
|
+
keepRelationScalarFields?: "false" | "true";
|
|
183
|
+
persistOriginalType?: "false" | "true";
|
|
184
|
+
schemaId?: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
declare const getJSONSchemaProperty: (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (field: DMMF.Field) => [name: string, definition: JSONSchema7Definition, PropertyMetaData];
|
|
188
|
+
|
|
189
|
+
declare const transformDmmf: (dmmf: DMMF.Document, transformOptions?: TransformOptions) => JSONSchema7;
|
|
190
|
+
|
|
191
|
+
export { type DefinitionMap, type ModelMetaData, type PropertyMap, type PropertyMetaData, type TransformOptions, getJSONSchemaProperty, transformDmmf as transformDMMF };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,172 @@
|
|
|
1
|
-
import * as json_schema from 'json-schema';
|
|
2
|
-
import { JSONSchema7Definition, JSONSchema7 } from 'json-schema';
|
|
3
1
|
import { ReadonlyDeep, DMMF } from '@prisma/generator-helper';
|
|
4
2
|
|
|
3
|
+
// ==================================================================================================
|
|
4
|
+
// JSON Schema Draft 07
|
|
5
|
+
// ==================================================================================================
|
|
6
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
7
|
+
// --------------------------------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Primitive type
|
|
11
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
12
|
+
*/
|
|
13
|
+
type JSONSchema7TypeName =
|
|
14
|
+
| "string" //
|
|
15
|
+
| "number"
|
|
16
|
+
| "integer"
|
|
17
|
+
| "boolean"
|
|
18
|
+
| "object"
|
|
19
|
+
| "array"
|
|
20
|
+
| "null";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Primitive type
|
|
24
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
25
|
+
*/
|
|
26
|
+
type JSONSchema7Type =
|
|
27
|
+
| string //
|
|
28
|
+
| number
|
|
29
|
+
| boolean
|
|
30
|
+
| JSONSchema7Object
|
|
31
|
+
| JSONSchema7Array
|
|
32
|
+
| null;
|
|
33
|
+
|
|
34
|
+
// Workaround for infinite type recursion
|
|
35
|
+
interface JSONSchema7Object {
|
|
36
|
+
[key: string]: JSONSchema7Type;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Workaround for infinite type recursion
|
|
40
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
41
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Meta schema
|
|
45
|
+
*
|
|
46
|
+
* Recommended values:
|
|
47
|
+
* - 'http://json-schema.org/schema#'
|
|
48
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
49
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
50
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
51
|
+
*
|
|
52
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
53
|
+
*/
|
|
54
|
+
type JSONSchema7Version = string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* JSON Schema v7
|
|
58
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
59
|
+
*/
|
|
60
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
61
|
+
interface JSONSchema7 {
|
|
62
|
+
$id?: string | undefined;
|
|
63
|
+
$ref?: string | undefined;
|
|
64
|
+
$schema?: JSONSchema7Version | undefined;
|
|
65
|
+
$comment?: string | undefined;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
69
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
70
|
+
*/
|
|
71
|
+
$defs?: {
|
|
72
|
+
[key: string]: JSONSchema7Definition;
|
|
73
|
+
} | undefined;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
77
|
+
*/
|
|
78
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
79
|
+
enum?: JSONSchema7Type[] | undefined;
|
|
80
|
+
const?: JSONSchema7Type | undefined;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
84
|
+
*/
|
|
85
|
+
multipleOf?: number | undefined;
|
|
86
|
+
maximum?: number | undefined;
|
|
87
|
+
exclusiveMaximum?: number | undefined;
|
|
88
|
+
minimum?: number | undefined;
|
|
89
|
+
exclusiveMinimum?: number | undefined;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
93
|
+
*/
|
|
94
|
+
maxLength?: number | undefined;
|
|
95
|
+
minLength?: number | undefined;
|
|
96
|
+
pattern?: string | undefined;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
100
|
+
*/
|
|
101
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
102
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
|
103
|
+
maxItems?: number | undefined;
|
|
104
|
+
minItems?: number | undefined;
|
|
105
|
+
uniqueItems?: boolean | undefined;
|
|
106
|
+
contains?: JSONSchema7Definition | undefined;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
110
|
+
*/
|
|
111
|
+
maxProperties?: number | undefined;
|
|
112
|
+
minProperties?: number | undefined;
|
|
113
|
+
required?: string[] | undefined;
|
|
114
|
+
properties?: {
|
|
115
|
+
[key: string]: JSONSchema7Definition;
|
|
116
|
+
} | undefined;
|
|
117
|
+
patternProperties?: {
|
|
118
|
+
[key: string]: JSONSchema7Definition;
|
|
119
|
+
} | undefined;
|
|
120
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
|
121
|
+
dependencies?: {
|
|
122
|
+
[key: string]: JSONSchema7Definition | string[];
|
|
123
|
+
} | undefined;
|
|
124
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
128
|
+
*/
|
|
129
|
+
if?: JSONSchema7Definition | undefined;
|
|
130
|
+
then?: JSONSchema7Definition | undefined;
|
|
131
|
+
else?: JSONSchema7Definition | undefined;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
135
|
+
*/
|
|
136
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
|
137
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
|
138
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
|
139
|
+
not?: JSONSchema7Definition | undefined;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
143
|
+
*/
|
|
144
|
+
format?: string | undefined;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
148
|
+
*/
|
|
149
|
+
contentMediaType?: string | undefined;
|
|
150
|
+
contentEncoding?: string | undefined;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
154
|
+
*/
|
|
155
|
+
definitions?: {
|
|
156
|
+
[key: string]: JSONSchema7Definition;
|
|
157
|
+
} | undefined;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
161
|
+
*/
|
|
162
|
+
title?: string | undefined;
|
|
163
|
+
description?: string | undefined;
|
|
164
|
+
default?: JSONSchema7Type | undefined;
|
|
165
|
+
readOnly?: boolean | undefined;
|
|
166
|
+
writeOnly?: boolean | undefined;
|
|
167
|
+
examples?: JSONSchema7Type | undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
5
170
|
interface PropertyMetaData {
|
|
6
171
|
hasDefaultValue: boolean;
|
|
7
172
|
isScalar: boolean;
|
|
@@ -19,7 +184,7 @@ interface TransformOptions {
|
|
|
19
184
|
schemaId?: string;
|
|
20
185
|
}
|
|
21
186
|
|
|
22
|
-
declare const getJSONSchemaProperty: (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (field: DMMF.Field) => [name: string, definition:
|
|
187
|
+
declare const getJSONSchemaProperty: (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (field: DMMF.Field) => [name: string, definition: JSONSchema7Definition, PropertyMetaData];
|
|
23
188
|
|
|
24
189
|
declare const transformDmmf: (dmmf: DMMF.Document, transformOptions?: TransformOptions) => JSONSchema7;
|
|
25
190
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,172 @@
|
|
|
1
|
-
import * as json_schema from 'json-schema';
|
|
2
|
-
import { JSONSchema7Definition, JSONSchema7 } from 'json-schema';
|
|
3
1
|
import { ReadonlyDeep, DMMF } from '@prisma/generator-helper';
|
|
4
2
|
|
|
3
|
+
// ==================================================================================================
|
|
4
|
+
// JSON Schema Draft 07
|
|
5
|
+
// ==================================================================================================
|
|
6
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
7
|
+
// --------------------------------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Primitive type
|
|
11
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
12
|
+
*/
|
|
13
|
+
type JSONSchema7TypeName =
|
|
14
|
+
| "string" //
|
|
15
|
+
| "number"
|
|
16
|
+
| "integer"
|
|
17
|
+
| "boolean"
|
|
18
|
+
| "object"
|
|
19
|
+
| "array"
|
|
20
|
+
| "null";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Primitive type
|
|
24
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
25
|
+
*/
|
|
26
|
+
type JSONSchema7Type =
|
|
27
|
+
| string //
|
|
28
|
+
| number
|
|
29
|
+
| boolean
|
|
30
|
+
| JSONSchema7Object
|
|
31
|
+
| JSONSchema7Array
|
|
32
|
+
| null;
|
|
33
|
+
|
|
34
|
+
// Workaround for infinite type recursion
|
|
35
|
+
interface JSONSchema7Object {
|
|
36
|
+
[key: string]: JSONSchema7Type;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Workaround for infinite type recursion
|
|
40
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
41
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Meta schema
|
|
45
|
+
*
|
|
46
|
+
* Recommended values:
|
|
47
|
+
* - 'http://json-schema.org/schema#'
|
|
48
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
49
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
50
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
51
|
+
*
|
|
52
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
53
|
+
*/
|
|
54
|
+
type JSONSchema7Version = string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* JSON Schema v7
|
|
58
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
59
|
+
*/
|
|
60
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
61
|
+
interface JSONSchema7 {
|
|
62
|
+
$id?: string | undefined;
|
|
63
|
+
$ref?: string | undefined;
|
|
64
|
+
$schema?: JSONSchema7Version | undefined;
|
|
65
|
+
$comment?: string | undefined;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
69
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
70
|
+
*/
|
|
71
|
+
$defs?: {
|
|
72
|
+
[key: string]: JSONSchema7Definition;
|
|
73
|
+
} | undefined;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
77
|
+
*/
|
|
78
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
79
|
+
enum?: JSONSchema7Type[] | undefined;
|
|
80
|
+
const?: JSONSchema7Type | undefined;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
84
|
+
*/
|
|
85
|
+
multipleOf?: number | undefined;
|
|
86
|
+
maximum?: number | undefined;
|
|
87
|
+
exclusiveMaximum?: number | undefined;
|
|
88
|
+
minimum?: number | undefined;
|
|
89
|
+
exclusiveMinimum?: number | undefined;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
93
|
+
*/
|
|
94
|
+
maxLength?: number | undefined;
|
|
95
|
+
minLength?: number | undefined;
|
|
96
|
+
pattern?: string | undefined;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
100
|
+
*/
|
|
101
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
102
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
|
103
|
+
maxItems?: number | undefined;
|
|
104
|
+
minItems?: number | undefined;
|
|
105
|
+
uniqueItems?: boolean | undefined;
|
|
106
|
+
contains?: JSONSchema7Definition | undefined;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
110
|
+
*/
|
|
111
|
+
maxProperties?: number | undefined;
|
|
112
|
+
minProperties?: number | undefined;
|
|
113
|
+
required?: string[] | undefined;
|
|
114
|
+
properties?: {
|
|
115
|
+
[key: string]: JSONSchema7Definition;
|
|
116
|
+
} | undefined;
|
|
117
|
+
patternProperties?: {
|
|
118
|
+
[key: string]: JSONSchema7Definition;
|
|
119
|
+
} | undefined;
|
|
120
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
|
121
|
+
dependencies?: {
|
|
122
|
+
[key: string]: JSONSchema7Definition | string[];
|
|
123
|
+
} | undefined;
|
|
124
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
128
|
+
*/
|
|
129
|
+
if?: JSONSchema7Definition | undefined;
|
|
130
|
+
then?: JSONSchema7Definition | undefined;
|
|
131
|
+
else?: JSONSchema7Definition | undefined;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
135
|
+
*/
|
|
136
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
|
137
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
|
138
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
|
139
|
+
not?: JSONSchema7Definition | undefined;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
143
|
+
*/
|
|
144
|
+
format?: string | undefined;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
148
|
+
*/
|
|
149
|
+
contentMediaType?: string | undefined;
|
|
150
|
+
contentEncoding?: string | undefined;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
154
|
+
*/
|
|
155
|
+
definitions?: {
|
|
156
|
+
[key: string]: JSONSchema7Definition;
|
|
157
|
+
} | undefined;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
161
|
+
*/
|
|
162
|
+
title?: string | undefined;
|
|
163
|
+
description?: string | undefined;
|
|
164
|
+
default?: JSONSchema7Type | undefined;
|
|
165
|
+
readOnly?: boolean | undefined;
|
|
166
|
+
writeOnly?: boolean | undefined;
|
|
167
|
+
examples?: JSONSchema7Type | undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
5
170
|
interface PropertyMetaData {
|
|
6
171
|
hasDefaultValue: boolean;
|
|
7
172
|
isScalar: boolean;
|
|
@@ -19,7 +184,7 @@ interface TransformOptions {
|
|
|
19
184
|
schemaId?: string;
|
|
20
185
|
}
|
|
21
186
|
|
|
22
|
-
declare const getJSONSchemaProperty: (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (field: DMMF.Field) => [name: string, definition:
|
|
187
|
+
declare const getJSONSchemaProperty: (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (field: DMMF.Field) => [name: string, definition: JSONSchema7Definition, PropertyMetaData];
|
|
23
188
|
|
|
24
189
|
declare const transformDmmf: (dmmf: DMMF.Document, transformOptions?: TransformOptions) => JSONSchema7;
|
|
25
190
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
var l=e=>e!=null,S=e=>{switch(e){case"Int":case"BigInt":return "integer";case"DateTime":case"Bytes":case"String":return "string";case"Float":case"Decimal":return "number";case"Json":return ["number","string","boolean","object","array","null"];case"Boolean":return "boolean";default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e)}`)}},g=e=>{let{isList:t,isRequired:r,kind:n,type:i}=e,a="object";return n==="scalar"&&!t?a=S(i):t?a="array":n==="enum"&&(a="string"),r||t?a:Array.isArray(a)?[...new Set([...a,"null"])]:[a,"null"]},F=e=>{let t=e.default;if(!e.hasDefaultValue)return null;if(e.kind==="enum")return typeof t=="string"?t:null;if(e.kind!=="scalar")return null;switch(e.type){case"String":case"BigInt":case"DateTime":return typeof t=="string"?t:null;case"Int":case"Float":case"Decimal":return typeof t=="number"?t:null;case"Boolean":return typeof t=="boolean"?t:null;case"Json":case"Bytes":return null;default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e.type)}`)}},O=e=>{if(e==="DateTime")return "date-time"},D=(e,{persistOriginalType:t,schemaId:r})=>{let n=e.isRequired||e.isList;h.equal(typeof e.type,"string");let i=`#/definitions/${e.type}`,a={$ref:r?`${r}${i}`:i};return n?a:{anyOf:[a,{type:"null"}],...t&&{originalType:e.type}}},N=(e,t)=>{if(!(e.kind==="scalar"&&!e.isList||e.kind==="enum"))return e.kind==="scalar"&&e.isList?{type:S(e.type)}:D(e,t)},J=e=>e.kind!=="scalar"&&!e.isList&&e.kind!=="enum",T=e=>t=>{let r=e.enums.find(({name:n})=>n===t.type);if(r)return r.values.map(n=>n.name)},P=e=>e.documentation,b=(e,t,r)=>{let n=g(r),i=O(r.type),a=N(r,t),o=T(e)(r),p=F(r),c=P(r);return {type:n,...t.persistOriginalType&&{originalType:r.type},...l(p)&&{default:p},...l(i)&&{format:i},...l(a)&&{items:a},...l(o)&&{enum:o},...l(c)&&{description:c}}},$=(e,t)=>r=>{let n={hasDefaultValue:r.hasDefaultValue,isScalar:r.kind==="scalar"||r.kind==="enum",required:r.isRequired},i=J(r)?D(r,t):b(e,t,r);return [r.name,i,n]},f=$;var j=e=>e.fields.flatMap(t=>t.relationFromFields??[]),k=(e,t)=>r=>{let n=r.fields.map(f(e,t)),i=n.map(([s,m])=>[s,m]),a=j(r),o=i.filter(s=>!a.includes(s[0])),c={properties:Object.fromEntries(t.keepRelationScalarFields==="true"?i:o),type:"object"};return t.includeRequiredFields&&(c.required=n.reduce((s,[m,,u])=>(u.required&&u.isScalar&&!u.hasDefaultValue&&s.push(m),s),[])),[r.name,c]},d=k;var R=e=>e.slice(0,1).toLowerCase()+e.slice(1),B=({schemaId:e})=>t=>{let r=`#/definitions/${t.name}`;return [R(t.name),{$ref:e?`${e}${r}`:r}]},q=(e,t={})=>{let{enums:r=[],models:n=[],types:i=[]}=e.datamodel,a={$schema:"http://json-schema.org/draft-07/schema#",definitions:{},type:"object"},{schemaId:o}=t,p=n.map(d({enums:r},t)),c=i.map(d({enums:r},t)),s=n.map(B(t)),m=Object.fromEntries([...p,...c]),u=Object.fromEntries(s);return {...o?{$id:o}:null,...a,definitions:m,properties:u}},w=q;
|
|
4
|
-
|
|
5
|
-
export { f as getJSONSchemaProperty, w as transformDMMF };
|
|
6
|
-
//# sourceMappingURL=out.js.map
|
|
7
|
-
//# sourceMappingURL=index.mjs.map
|
|
1
|
+
import{default as t}from"./packem_shared/getJSONSchemaProperty-2XKY7Nu4.mjs";import{default as o}from"./packem_shared/transformDMMF-KYYhvd46.mjs";export{t as getJSONSchemaProperty,o as transformDMMF};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var d=Object.defineProperty;var y=(e,t)=>d(e,"name",{value:t,configurable:!0});import f from"node:assert";var g=Object.defineProperty,a=y((e,t)=>g(e,"name",{value:t,configurable:!0}),"a");const l=a(e=>e!=null,"isDefined"),m=a(e=>{switch(e){case"Int":case"BigInt":return"integer";case"DateTime":case"Bytes":case"String":return"string";case"Float":case"Decimal":return"number";case"Json":return["number","string","boolean","object","array","null"];case"Boolean":return"boolean";default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e)}`)}},"getJSONSchemaScalar"),S=a(e=>{const{isList:t,isRequired:r,kind:i,type:s}=e;let n="object";return i==="scalar"&&!t?n=m(s):t?n="array":i==="enum"&&(n="string"),r||t?n:Array.isArray(n)?[...new Set([...n,"null"])]:[n,"null"]},"getJSONSchemaType"),D=a(e=>{const t=e.default;if(!e.hasDefaultValue)return null;if(e.kind==="enum")return typeof t=="string"?t:null;if(e.kind!=="scalar")return null;switch(e.type){case"String":case"BigInt":case"DateTime":return typeof t=="string"?t:null;case"Int":case"Float":case"Decimal":return typeof t=="number"?t:null;case"Boolean":return typeof t=="boolean"?t:null;case"Json":case"Bytes":return null;default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e.type)}`)}},"getDefaultValue"),h=a(e=>{if(e==="DateTime")return"date-time"},"getFormatByDMMFType"),p=a((e,{persistOriginalType:t,schemaId:r})=>{const i=e.isRequired||e.isList;f.equal(typeof e.type,"string");const s=`#/definitions/${e.type}`,n={$ref:r?`${r}${s}`:s};return i?n:{anyOf:[n,{type:"null"}],...t&&{originalType:e.type}}},"getJSONSchemaForPropertyReference"),b=a((e,t)=>{if(!(e.kind==="scalar"&&!e.isList||e.kind==="enum"))return e.kind==="scalar"&&e.isList?{type:m(e.type)}:p(e,t)},"getItemsByDMMFType"),O=a(e=>e.kind!=="scalar"&&!e.isList&&e.kind!=="enum","isSingleReference"),T=a(e=>t=>{const r=e.enums.find(({name:i})=>i===t.type);if(r)return r.values.map(i=>i.name)},"getEnumListByDMMFType"),k=a(e=>e.documentation,"getDescription"),B=a((e,t,r)=>{const i=S(r),s=h(r.type),n=b(r,t),u=T(e)(r),c=D(r),o=k(r);return{type:i,...t.persistOriginalType&&{originalType:r.type},...l(c)&&{default:c},...l(s)&&{format:s},...l(n)&&{items:n},...l(u)&&{enum:u},...l(o)&&{description:o}}},"getPropertyDefinition"),w=a((e,t)=>r=>{const i={hasDefaultValue:r.hasDefaultValue,isScalar:r.kind==="scalar"||r.kind==="enum",required:r.isRequired},s=O(r)?p(r,t):B(e,t,r);return[r.name,s,i]},"getJSONSchemaProperty");export{w as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var l=(e,t)=>d(e,"name",{value:t,configurable:!0});const f=require("node:assert"),g=l(e=>e&&typeof e=="object"&&"default"in e?e.default:e,"_interopDefaultCompat"),D=g(f);var S=Object.defineProperty,a=l((e,t)=>S(e,"name",{value:t,configurable:!0}),"a");const u=a(e=>e!=null,"isDefined"),m=a(e=>{switch(e){case"Int":case"BigInt":return"integer";case"DateTime":case"Bytes":case"String":return"string";case"Float":case"Decimal":return"number";case"Json":return["number","string","boolean","object","array","null"];case"Boolean":return"boolean";default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e)}`)}},"getJSONSchemaScalar"),h=a(e=>{const{isList:t,isRequired:r,kind:i,type:s}=e;let n="object";return i==="scalar"&&!t?n=m(s):t?n="array":i==="enum"&&(n="string"),r||t?n:Array.isArray(n)?[...new Set([...n,"null"])]:[n,"null"]},"getJSONSchemaType"),b=a(e=>{const t=e.default;if(!e.hasDefaultValue)return null;if(e.kind==="enum")return typeof t=="string"?t:null;if(e.kind!=="scalar")return null;switch(e.type){case"String":case"BigInt":case"DateTime":return typeof t=="string"?t:null;case"Int":case"Float":case"Decimal":return typeof t=="number"?t:null;case"Boolean":return typeof t=="boolean"?t:null;case"Json":case"Bytes":return null;default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e.type)}`)}},"getDefaultValue"),O=a(e=>{if(e==="DateTime")return"date-time"},"getFormatByDMMFType"),p=a((e,{persistOriginalType:t,schemaId:r})=>{const i=e.isRequired||e.isList;D.equal(typeof e.type,"string");const s=`#/definitions/${e.type}`,n={$ref:r?`${r}${s}`:s};return i?n:{anyOf:[n,{type:"null"}],...t&&{originalType:e.type}}},"getJSONSchemaForPropertyReference"),T=a((e,t)=>{if(!(e.kind==="scalar"&&!e.isList||e.kind==="enum"))return e.kind==="scalar"&&e.isList?{type:m(e.type)}:p(e,t)},"getItemsByDMMFType"),k=a(e=>e.kind!=="scalar"&&!e.isList&&e.kind!=="enum","isSingleReference"),B=a(e=>t=>{const r=e.enums.find(({name:i})=>i===t.type);if(r)return r.values.map(i=>i.name)},"getEnumListByDMMFType"),F=a(e=>e.documentation,"getDescription"),J=a((e,t,r)=>{const i=h(r),s=O(r.type),n=T(r,t),c=B(e)(r),o=b(r),y=F(r);return{type:i,...t.persistOriginalType&&{originalType:r.type},...u(o)&&{default:o},...u(s)&&{format:s},...u(n)&&{items:n},...u(c)&&{enum:c},...u(y)&&{description:y}}},"getPropertyDefinition"),M=a((e,t)=>r=>{const i={hasDefaultValue:r.hasDefaultValue,isScalar:r.kind==="scalar"||r.kind==="enum",required:r.isRequired},s=k(r)?p(r,t):J(e,t,r);return[r.name,s,i]},"getJSONSchemaProperty");module.exports=M;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var b=Object.defineProperty;var d=(r,e)=>b(r,"name",{value:e,configurable:!0});const j=require("./getJSONSchemaProperty-CapSYG-2.cjs");var S=Object.defineProperty,f=d((r,e)=>S(r,"name",{value:e,configurable:!0}),"n$1");const $=f(r=>r.fields.flatMap(e=>e.relationFromFields??[]),"getRelationScalarFields"),p=f((r,e)=>t=>{const s=t.fields.map(j(r,e)),o=s.map(([a,i])=>[a,i]),m=$(t),c=o.filter(a=>!m.includes(a[0])),l={properties:Object.fromEntries(e.keepRelationScalarFields==="true"?o:c),type:"object"};return e.includeRequiredFields&&(l.required=s.reduce((a,[i,,n])=>(n.required&&n.isScalar&&!n.hasDefaultValue&&a.push(i),a),[])),[t.name,l]},"getJSONSchemaModel");var g=Object.defineProperty,u=d((r,e)=>g(r,"name",{value:e,configurable:!0}),"n");const y=u(r=>r.slice(0,1).toLowerCase()+r.slice(1),"toCamelCase"),O=u(({schemaId:r})=>e=>{const t=`#/definitions/${e.name}`;return[y(e.name),{$ref:r?`${r}${t}`:t}]},"getPropertyDefinition"),v=u((r,e={})=>{const{enums:t=[],models:s=[],types:o=[]}=r.datamodel,m={$schema:"http://json-schema.org/draft-07/schema#",definitions:{},type:"object"},{schemaId:c}=e,l=s.map(p({enums:t},e)),a=o.map(p({enums:t},e)),i=s.map(O(e)),n=Object.fromEntries([...l,...a]),h=Object.fromEntries(i);return{...c?{$id:c}:null,...m,definitions:n,properties:h}},"transformDmmf");module.exports=v;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var b=Object.defineProperty;var d=(r,e)=>b(r,"name",{value:e,configurable:!0});import j from"./getJSONSchemaProperty-2XKY7Nu4.mjs";var $=Object.defineProperty,u=d((r,e)=>$(r,"name",{value:e,configurable:!0}),"n$1");const O=u(r=>r.fields.flatMap(e=>e.relationFromFields??[]),"getRelationScalarFields"),p=u((r,e)=>a=>{const s=a.fields.map(j(r,e)),o=s.map(([t,i])=>[t,i]),c=O(a),l=o.filter(t=>!c.includes(t[0])),m={properties:Object.fromEntries(e.keepRelationScalarFields==="true"?o:l),type:"object"};return e.includeRequiredFields&&(m.required=s.reduce((t,[i,,n])=>(n.required&&n.isScalar&&!n.hasDefaultValue&&t.push(i),t),[])),[a.name,m]},"getJSONSchemaModel");var g=Object.defineProperty,f=d((r,e)=>g(r,"name",{value:e,configurable:!0}),"n");const v=f(r=>r.slice(0,1).toLowerCase()+r.slice(1),"toCamelCase"),y=f(({schemaId:r})=>e=>{const a=`#/definitions/${e.name}`;return[v(e.name),{$ref:r?`${r}${a}`:a}]},"getPropertyDefinition"),D=f((r,e={})=>{const{enums:a=[],models:s=[],types:o=[]}=r.datamodel,c={$schema:"http://json-schema.org/draft-07/schema#",definitions:{},type:"object"},{schemaId:l}=e,m=s.map(p({enums:a},e)),t=o.map(p({enums:a},e)),i=s.map(y(e)),n=Object.fromEntries([...m,...t]),h=Object.fromEntries(i);return{...l?{$id:l}:null,...c,definitions:n,properties:h}},"transformDmmf");export{D as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@visulima/prisma-dmmf-transformer",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.22",
|
|
4
4
|
"description": "A generator for Prisma to generate a valid JSON Schema v7.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"anolilab",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"exports": {
|
|
40
40
|
".": {
|
|
41
41
|
"require": {
|
|
42
|
-
"types": "./dist/index.d.
|
|
43
|
-
"default": "./dist/index.
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
44
|
},
|
|
45
45
|
"import": {
|
|
46
46
|
"types": "./dist/index.d.mts",
|
|
@@ -49,10 +49,17 @@
|
|
|
49
49
|
},
|
|
50
50
|
"./package.json": "./package.json"
|
|
51
51
|
},
|
|
52
|
-
"main": "dist/index.
|
|
52
|
+
"main": "dist/index.cjs",
|
|
53
53
|
"module": "dist/index.mjs",
|
|
54
54
|
"source": "src/index.ts",
|
|
55
55
|
"types": "dist/index.d.ts",
|
|
56
|
+
"typesVersions": {
|
|
57
|
+
">=5.0": {
|
|
58
|
+
".": [
|
|
59
|
+
"./dist/index.d.ts"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
},
|
|
56
63
|
"files": [
|
|
57
64
|
"dist/**",
|
|
58
65
|
"README.md",
|
|
@@ -67,19 +74,22 @@
|
|
|
67
74
|
"@anolilab/prettier-config": "^5.0.14",
|
|
68
75
|
"@anolilab/semantic-release-pnpm": "^1.1.3",
|
|
69
76
|
"@anolilab/semantic-release-preset": "^9.0.0",
|
|
70
|
-
"@
|
|
77
|
+
"@arethetypeswrong/cli": "^0.16.4",
|
|
78
|
+
"@babel/core": "^7.25.2",
|
|
71
79
|
"@prisma/client": "5.15.1",
|
|
72
80
|
"@prisma/internals": "5.15.1",
|
|
73
|
-
"@rushstack/eslint-plugin-security": "^0.8.
|
|
81
|
+
"@rushstack/eslint-plugin-security": "^0.8.3",
|
|
74
82
|
"@types/json-schema": "7.0.15",
|
|
75
83
|
"@types/micromatch": "4.0.7",
|
|
76
84
|
"@types/node": "18.19.15",
|
|
85
|
+
"@visulima/packem": "1.0.1",
|
|
77
86
|
"@vitest/coverage-v8": "1.6.0",
|
|
78
87
|
"ajv": "8.16.0",
|
|
79
88
|
"ajv-formats": "3.0.1",
|
|
80
89
|
"conventional-changelog-conventionalcommits": "8.0.0",
|
|
81
90
|
"cross-env": "7.0.3",
|
|
82
|
-
"
|
|
91
|
+
"esbuild": "0.24.0",
|
|
92
|
+
"eslint": "8.57.0",
|
|
83
93
|
"eslint-plugin-deprecation": "^3.0.0",
|
|
84
94
|
"eslint-plugin-etc": "^2.0.3",
|
|
85
95
|
"eslint-plugin-import": "npm:eslint-plugin-i@^2.29.1",
|
|
@@ -88,9 +98,8 @@
|
|
|
88
98
|
"eslint-plugin-vitest-globals": "^1.5.0",
|
|
89
99
|
"prettier": "3.3.2",
|
|
90
100
|
"prisma": "5.15.1",
|
|
91
|
-
"rimraf": "5.0.
|
|
101
|
+
"rimraf": "5.0.9",
|
|
92
102
|
"semantic-release": "24.0.0",
|
|
93
|
-
"tsup": "8.1.0",
|
|
94
103
|
"typescript": "5.4.5",
|
|
95
104
|
"vitest": "1.6.0"
|
|
96
105
|
},
|
|
@@ -123,10 +132,11 @@
|
|
|
123
132
|
}
|
|
124
133
|
},
|
|
125
134
|
"scripts": {
|
|
126
|
-
"build": "cross-env NODE_ENV=development
|
|
127
|
-
"build:prod": "cross-env NODE_ENV=production
|
|
135
|
+
"build": "cross-env NODE_ENV=development packem build",
|
|
136
|
+
"build:prod": "cross-env NODE_ENV=production packem build",
|
|
128
137
|
"clean": "rimraf node_modules dist .eslintcache",
|
|
129
138
|
"dev": "pnpm run build --watch",
|
|
139
|
+
"lint:attw": "attw --pack",
|
|
130
140
|
"lint:eslint": "eslint . --ext js,cjs,mjs,jsx,ts,tsx,json,yaml,yml,md,mdx --max-warnings=0 --config .eslintrc.js",
|
|
131
141
|
"lint:eslint:fix": "eslint . --ext js,cjs,mjs,jsx,ts,tsx,json,yaml,yml,md,mdx --max-warnings=0 --config .eslintrc.js --fix",
|
|
132
142
|
"lint:package-json": "publint --strict",
|
package/dist/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var D = require('assert');
|
|
4
|
-
|
|
5
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
-
|
|
7
|
-
var D__default = /*#__PURE__*/_interopDefault(D);
|
|
8
|
-
|
|
9
|
-
var l=e=>e!=null,d=e=>{switch(e){case"Int":case"BigInt":return "integer";case"DateTime":case"Bytes":case"String":return "string";case"Float":case"Decimal":return "number";case"Json":return ["number","string","boolean","object","array","null"];case"Boolean":return "boolean";default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e)}`)}},h=e=>{let{isList:t,isRequired:r,kind:n,type:i}=e,a="object";return n==="scalar"&&!t?a=d(i):t?a="array":n==="enum"&&(a="string"),r||t?a:Array.isArray(a)?[...new Set([...a,"null"])]:[a,"null"]},g=e=>{let t=e.default;if(!e.hasDefaultValue)return null;if(e.kind==="enum")return typeof t=="string"?t:null;if(e.kind!=="scalar")return null;switch(e.type){case"String":case"BigInt":case"DateTime":return typeof t=="string"?t:null;case"Int":case"Float":case"Decimal":return typeof t=="number"?t:null;case"Boolean":return typeof t=="boolean"?t:null;case"Json":case"Bytes":return null;default:throw new Error(`Unhandled discriminated union member: ${JSON.stringify(e.type)}`)}},F=e=>{if(e==="DateTime")return "date-time"},S=(e,{persistOriginalType:t,schemaId:r})=>{let n=e.isRequired||e.isList;D__default.default.equal(typeof e.type,"string");let i=`#/definitions/${e.type}`,a={$ref:r?`${r}${i}`:i};return n?a:{anyOf:[a,{type:"null"}],...t&&{originalType:e.type}}},O=(e,t)=>{if(!(e.kind==="scalar"&&!e.isList||e.kind==="enum"))return e.kind==="scalar"&&e.isList?{type:d(e.type)}:S(e,t)},N=e=>e.kind!=="scalar"&&!e.isList&&e.kind!=="enum",J=e=>t=>{let r=e.enums.find(({name:n})=>n===t.type);if(r)return r.values.map(n=>n.name)},T=e=>e.documentation,P=(e,t,r)=>{let n=h(r),i=F(r.type),a=O(r,t),o=J(e)(r),p=g(r),c=T(r);return {type:n,...t.persistOriginalType&&{originalType:r.type},...l(p)&&{default:p},...l(i)&&{format:i},...l(a)&&{items:a},...l(o)&&{enum:o},...l(c)&&{description:c}}},b=(e,t)=>r=>{let n={hasDefaultValue:r.hasDefaultValue,isScalar:r.kind==="scalar"||r.kind==="enum",required:r.isRequired},i=N(r)?S(r,t):P(e,t,r);return [r.name,i,n]},M=b;var $=e=>e.fields.flatMap(t=>t.relationFromFields??[]),j=(e,t)=>r=>{let n=r.fields.map(M(e,t)),i=n.map(([s,m])=>[s,m]),a=$(r),o=i.filter(s=>!a.includes(s[0])),c={properties:Object.fromEntries(t.keepRelationScalarFields==="true"?i:o),type:"object"};return t.includeRequiredFields&&(c.required=n.reduce((s,[m,,u])=>(u.required&&u.isScalar&&!u.hasDefaultValue&&s.push(m),s),[])),[r.name,c]},f=j;var k=e=>e.slice(0,1).toLowerCase()+e.slice(1),R=({schemaId:e})=>t=>{let r=`#/definitions/${t.name}`;return [k(t.name),{$ref:e?`${e}${r}`:r}]},B=(e,t={})=>{let{enums:r=[],models:n=[],types:i=[]}=e.datamodel,a={$schema:"http://json-schema.org/draft-07/schema#",definitions:{},type:"object"},{schemaId:o}=t,p=n.map(f({enums:r},t)),c=i.map(f({enums:r},t)),s=n.map(R(t)),m=Object.fromEntries([...p,...c]),u=Object.fromEntries(s);return {...o?{$id:o}:null,...a,definitions:m,properties:u}},q=B;
|
|
10
|
-
|
|
11
|
-
exports.getJSONSchemaProperty = M;
|
|
12
|
-
exports.transformDMMF = q;
|
|
13
|
-
//# sourceMappingURL=out.js.map
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/properties.ts","../src/model.ts","../src/transform-dmmf.ts"],"names":["assert","isDefined","value","getJSONSchemaScalar","fieldType","getJSONSchemaType","field","isList","isRequired","kind","type","scalarFieldType","getDefaultValue","fieldDefault","getFormatByDMMFType","getJSONSchemaForPropertyReference","persistOriginalType","schemaId","notNullable","typeReference","reference","getItemsByDMMFType","transformOptions","isSingleReference","getEnumListByDMMFType","modelMetaData","enumItem","name","item","getDescription","getPropertyDefinition","format","items","enumList","defaultValue","description","getJSONSchemaProperty","propertyMetaData","property","properties_default","getRelationScalarFields","model","getJSONSchemaModel","definitionPropertiesMap","propertiesMap","definition","relationScalarFields","propertiesWithoutRelationScalars","filtered","fieldMetaData","model_default","toCamelCase","transformDmmf","dmmf","enums","models","types","initialJSON","modelDefinitionsMap","typeDefinitionsMap","modelPropertyDefinitionsMap","definitions","properties","transform_dmmf_default"],"mappings":"AAAA,OAAOA,MAAY,SAOnB,IAAMC,EAAgBC,GAAmEA,GAAU,KAE7FC,EAAuBC,GAA4E,CACrG,OAAQA,EAAW,CACf,IAAK,MACL,IAAK,SACD,MAAO,UAEX,IAAK,WACL,IAAK,QACL,IAAK,SACD,MAAO,SAEX,IAAK,QACL,IAAK,UACD,MAAO,SAEX,IAAK,OACD,MAAO,CAAC,SAAU,SAAU,UAAW,SAAU,QAAS,MAAM,EAEpE,IAAK,UACD,MAAO,UAEX,QACI,MAAM,IAAI,MAAM,yCAAyC,KAAK,UAAUA,CAAS,CAAC,EAAE,CAE5F,CACJ,EAEMC,EAAqBC,GAA2C,CAClE,GAAM,CAAE,OAAAC,EAAQ,WAAAC,EAAY,KAAAC,EAAM,KAAAC,CAAK,EAAIJ,EAEvCK,EAAuC,SAU3C,OARIF,IAAS,UAAY,CAACF,EACtBI,EAAkBR,EAAoBO,CAAuB,EACtDH,EACPI,EAAkB,QACXF,IAAS,SAChBE,EAAkB,UAGlBH,GAAcD,EACPI,EAGU,MAAM,QAAQA,CAAe,EAGvC,CAAC,GAAG,IAAI,IAAI,CAAC,GAAGA,EAAiB,MAAM,CAAC,CAAC,EAG7C,CAACA,EAAwC,MAAM,CAC1D,EAEMC,EAAmBN,GAA8C,CACnE,IAAMO,EAAeP,EAAM,QAE3B,GAAI,CAACA,EAAM,gBACP,OAAO,KAGX,GAAIA,EAAM,OAAS,OACf,OAAO,OAAOO,GAAiB,SAAWA,EAAe,KAG7D,GAAIP,EAAM,OAAS,SACf,OAAO,KAGX,OAAQA,EAAM,KAAM,CAChB,IAAK,SACL,IAAK,SACL,IAAK,WACD,OAAO,OAAOO,GAAiB,SAAWA,EAAe,KAE7D,IAAK,MACL,IAAK,QACL,IAAK,UACD,OAAO,OAAOA,GAAiB,SAAWA,EAAe,KAE7D,IAAK,UACD,OA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AChFG,EAAa,OAAO,YAAYF,CAA2B,EAEjE,MAAO,CACH,GAAI3C,EAAW,CAAE,IAAKA,CAAS,EAAI,KACnC,GAAGwC,EACH,YAAAI,EACA,WAAAC,CACJ,CACJ,EAEOC,EAAQX","sourcesContent":["import assert from \"node:assert\";\n\nimport type { DMMF } from \"@prisma/generator-helper\";\nimport type { JSONSchema7, JSONSchema7TypeName } from \"json-schema\";\n\nimport type { ModelMetaData, PrismaPrimitive, PropertyMap, PropertyMetaData, TransformOptions } from \"./types\";\n\nconst isDefined = <T>(value: T | null | undefined): value is T => value !== undefined && value !== null;\n\nconst getJSONSchemaScalar = (fieldType: PrismaPrimitive): JSONSchema7TypeName | JSONSchema7TypeName[] => {\n switch (fieldType) {\n case \"Int\":\n case \"BigInt\": {\n return \"integer\";\n }\n case \"DateTime\":\n case \"Bytes\":\n case \"String\": {\n return \"string\";\n }\n case \"Float\":\n case \"Decimal\": {\n return \"number\";\n }\n case \"Json\": {\n return [\"number\", \"string\", \"boolean\", \"object\", \"array\", \"null\"];\n }\n case \"Boolean\": {\n return \"boolean\";\n }\n default: {\n throw new Error(`Unhandled discriminated union member: ${JSON.stringify(fieldType)}`);\n }\n }\n};\n\nconst getJSONSchemaType = (field: DMMF.Field): JSONSchema7[\"type\"] => {\n const { isList, isRequired, kind, type } = field;\n\n let scalarFieldType: JSONSchema7[\"type\"] = \"object\";\n\n if (kind === \"scalar\" && !isList) {\n scalarFieldType = getJSONSchemaScalar(type as PrismaPrimitive);\n } else if (isList) {\n scalarFieldType = \"array\";\n } else if (kind === \"enum\") {\n scalarFieldType = \"string\";\n }\n\n if (isRequired || isList) {\n return scalarFieldType;\n }\n\n const isFieldUnion = Array.isArray(scalarFieldType);\n\n if (isFieldUnion) {\n return [...new Set([...scalarFieldType, \"null\"])] as JSONSchema7[\"type\"];\n }\n\n return [scalarFieldType as JSONSchema7TypeName, \"null\"];\n};\n\nconst getDefaultValue = (field: DMMF.Field): JSONSchema7[\"default\"] => {\n const fieldDefault = field.default;\n\n if (!field.hasDefaultValue) {\n return null;\n }\n\n if (field.kind === \"enum\") {\n return typeof fieldDefault === \"string\" ? fieldDefault : null;\n }\n\n if (field.kind !== \"scalar\") {\n return null;\n }\n\n switch (field.type) {\n case \"String\":\n case \"BigInt\":\n case \"DateTime\": {\n return typeof fieldDefault === \"string\" ? fieldDefault : null;\n }\n case \"Int\":\n case \"Float\":\n case \"Decimal\": {\n return typeof fieldDefault === \"number\" ? fieldDefault : null;\n }\n case \"Boolean\": {\n return typeof fieldDefault === \"boolean\" ? fieldDefault : null;\n }\n case \"Json\":\n case \"Bytes\": {\n return null;\n }\n default: {\n throw new Error(`Unhandled discriminated union member: ${JSON.stringify(field.type)}`);\n }\n }\n};\n\nconst getFormatByDMMFType = (fieldType: DMMF.Field[\"type\"]): string | undefined => {\n if (fieldType === \"DateTime\") {\n return \"date-time\";\n }\n\n return undefined;\n};\n\nconst getJSONSchemaForPropertyReference = (field: DMMF.Field, { persistOriginalType, schemaId }: TransformOptions): JSONSchema7 => {\n const notNullable = field.isRequired || field.isList;\n\n assert.equal(typeof field.type, \"string\");\n\n const typeReference = `#/definitions/${field.type}`;\n const reference = { $ref: schemaId ? `${schemaId}${typeReference}` : typeReference };\n\n return notNullable\n ? reference\n : {\n anyOf: [reference, { type: \"null\" }],\n ...(persistOriginalType && {\n originalType: field.type,\n }),\n };\n};\n\nconst getItemsByDMMFType = (field: DMMF.Field, transformOptions: TransformOptions): JSONSchema7[\"items\"] => {\n if ((field.kind === \"scalar\" && !field.isList) || field.kind === \"enum\") {\n return undefined;\n }\n\n if (field.kind === \"scalar\" && field.isList) {\n return { type: getJSONSchemaScalar(field.type as PrismaPrimitive) };\n }\n\n return getJSONSchemaForPropertyReference(field, transformOptions);\n};\n\nconst isSingleReference = (field: DMMF.Field) => field.kind !== \"scalar\" && !field.isList && field.kind !== \"enum\";\n\nconst getEnumListByDMMFType =\n (modelMetaData: ModelMetaData) =>\n (field: DMMF.Field): string[] | undefined => {\n const enumItem = modelMetaData.enums.find(({ name }) => name === field.type);\n\n if (!enumItem) {\n return undefined;\n }\n\n return enumItem.values.map((item) => item.name);\n };\n\nconst getDescription = (field: DMMF.Field) => field.documentation;\n\nconst getPropertyDefinition = (modelMetaData: ModelMetaData, transformOptions: TransformOptions, field: DMMF.Field) => {\n const type = getJSONSchemaType(field);\n const format = getFormatByDMMFType(field.type);\n const items = getItemsByDMMFType(field, transformOptions);\n const enumList = getEnumListByDMMFType(modelMetaData)(field);\n const defaultValue = getDefaultValue(field);\n const description = getDescription(field);\n\n return {\n type,\n ...(transformOptions.persistOriginalType && {\n originalType: field.type,\n }),\n ...(isDefined(defaultValue) && { default: defaultValue }),\n ...(isDefined(format) && { format }),\n ...(isDefined(items) && { items }),\n ...(isDefined(enumList) && { enum: enumList }),\n ...(isDefined(description) && { description }),\n };\n};\n\nconst getJSONSchemaProperty =\n (modelMetaData: ModelMetaData, transformOptions: TransformOptions) =>\n (field: DMMF.Field): PropertyMap => {\n const propertyMetaData: PropertyMetaData = {\n hasDefaultValue: field.hasDefaultValue,\n isScalar: field.kind === \"scalar\" || field.kind === \"enum\",\n required: field.isRequired,\n };\n\n const property = isSingleReference(field)\n ? getJSONSchemaForPropertyReference(field, transformOptions)\n : getPropertyDefinition(modelMetaData, transformOptions, field);\n\n return [field.name, property, propertyMetaData];\n };\n\nexport default getJSONSchemaProperty;\n","import type { DMMF } from \"@prisma/generator-helper\";\nimport type { JSONSchema7Definition } from \"json-schema\";\n\nimport getJSONSchemaProperty from \"./properties\";\nimport type { DefinitionMap, ModelMetaData, TransformOptions } from \"./types\";\n\nconst getRelationScalarFields = (model: DMMF.Model): string[] => model.fields.flatMap((field) => field.relationFromFields ?? []);\n\nconst getJSONSchemaModel =\n (modelMetaData: ModelMetaData, transformOptions: TransformOptions) =>\n (model: DMMF.Model): DefinitionMap => {\n const definitionPropertiesMap = model.fields.map(getJSONSchemaProperty(modelMetaData, transformOptions));\n\n const propertiesMap = definitionPropertiesMap.map(([name, definition]) => [name, definition] as DefinitionMap);\n const relationScalarFields = getRelationScalarFields(model);\n const propertiesWithoutRelationScalars = propertiesMap.filter((property) => !relationScalarFields.includes(property[0]));\n\n const properties = Object.fromEntries(transformOptions.keepRelationScalarFields === \"true\" ? propertiesMap : propertiesWithoutRelationScalars);\n\n const definition: JSONSchema7Definition = {\n properties,\n type: \"object\",\n };\n\n if (transformOptions.includeRequiredFields) {\n // eslint-disable-next-line unicorn/no-array-reduce\n definition.required = definitionPropertiesMap.reduce((filtered: string[], [name, , fieldMetaData]) => {\n if (fieldMetaData.required && fieldMetaData.isScalar && !fieldMetaData.hasDefaultValue) {\n filtered.push(name);\n }\n return filtered;\n }, []);\n }\n\n return [model.name, definition];\n };\n\nexport default getJSONSchemaModel;\n","import type { DMMF } from \"@prisma/generator-helper\";\nimport type { JSONSchema7, JSONSchema7Definition } from \"json-schema\";\n\nimport getJSONSchemaModel from \"./model\";\nimport type { TransformOptions } from \"./types\";\n\nconst toCamelCase = (name: string): string => name.slice(0, 1).toLowerCase() + name.slice(1);\n\nconst getPropertyDefinition =\n ({ schemaId }: TransformOptions) =>\n (model: DMMF.Model): [name: string, reference: JSONSchema7Definition] => {\n const reference = `#/definitions/${model.name}`;\n\n return [\n toCamelCase(model.name),\n {\n $ref: schemaId ? `${schemaId}${reference}` : reference,\n },\n ];\n };\n\nconst transformDmmf = (dmmf: DMMF.Document, transformOptions: TransformOptions = {}): JSONSchema7 => {\n // TODO: Remove default values as soon as prisma version < 3.10.0 doesn't have to be supported anymore\n const { enums = [], models = [], types = [] } = dmmf.datamodel;\n const initialJSON = {\n $schema: \"http://json-schema.org/draft-07/schema#\",\n definitions: {},\n type: \"object\",\n } as JSONSchema7;\n const { schemaId } = transformOptions;\n\n const modelDefinitionsMap = models.map(getJSONSchemaModel({ enums }, transformOptions));\n\n const typeDefinitionsMap = types.map(getJSONSchemaModel({ enums }, transformOptions));\n const modelPropertyDefinitionsMap = models.map(getPropertyDefinition(transformOptions));\n const definitions = Object.fromEntries([...modelDefinitionsMap, ...typeDefinitionsMap]);\n const properties = Object.fromEntries(modelPropertyDefinitionsMap);\n\n return {\n ...(schemaId ? { $id: schemaId } : null),\n ...initialJSON,\n definitions,\n properties,\n };\n};\n\nexport default transformDmmf;\n"]}
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/properties.ts","../src/model.ts","../src/transform-dmmf.ts"],"names":["assert","isDefined","value","getJSONSchemaScalar","fieldType","getJSONSchemaType","field","isList","isRequired","kind","type","scalarFieldType","getDefaultValue","fieldDefault","getFormatByDMMFType","getJSONSchemaForPropertyReference","persistOriginalType","schemaId","notNullable","typeReference","reference","getItemsByDMMFType","transformOptions","isSingleReference","getEnumListByDMMFType","modelMetaData","enumItem","name","item","getDescription","getPropertyDefinition","format","items","enumList","defaultValue","description","getJSONSchemaProperty","propertyMetaData","property","properties_default","getRelationScalarFields","model","getJSONSchemaModel","definitionPropertiesMap","propertiesMap","definition","relationScalarFields","propertiesWithoutRelationScalars","filtered","fieldMetaData","model_default","toCamelCase","transformDmmf","dmmf","enums","models","types","initialJSON","modelDefinitionsMap","typeDefinitionsMap","modelPropertyDefinitionsMap","definitions","properties","transform_dmmf_default"],"mappings":"AAAA,OAAOA,MAAY,cAOnB,IAAMC,EAAgBC,GAAmEA,GAAU,KAE7FC,EAAuBC,GAA4E,CACrG,OAAQA,EAAW,CACf,IAAK,MACL,IAAK,SACD,MAAO,UAEX,IAAK,WACL,IAAK,QACL,IAAK,SACD,MAAO,SAEX,IAAK,QACL,IAAK,UACD,MAAO,SAEX,IAAK,OACD,MAAO,CAAC,SAAU,SAAU,UAAW,SAAU,QAAS,MAAM,EAEpE,IAAK,UACD,MAAO,UAEX,QACI,MAAM,IAAI,MAAM,yCAAyC,KAAK,UAAUA,CAAS,CAAC,EAAE,CAE5F,CACJ,EAEMC,EAAqBC,GAA2C,CAClE,GAAM,CAAE,OAAAC,EAAQ,WAAAC,EAAY,K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AMrB,EAAY,iBAAiBqB,EAAM,IAAI,GAE7C,MAAO,CACHU,EAAYV,EAAM,IAAI,EACtB,CACI,KAAMxB,EAAW,GAAGA,CAAQ,GAAGG,CAAS,GAAKA,CACjD,CACJ,CACJ,EAEEgC,EAAgB,CAACC,EAAqB/B,EAAqC,CAAC,IAAmB,CAEjG,GAAM,CAAE,MAAAgC,EAAQ,CAAC,EAAG,OAAAC,EAAS,CAAC,EAAG,MAAAC,EAAQ,CAAC,CAAE,EAAIH,EAAK,UAC/CI,EAAc,CAChB,QAAS,0CACT,YAAa,CAAC,EACd,KAAM,QACV,EACM,CAAE,SAAAxC,CAAS,EAAIK,EAEfoC,EAAsBH,EAAO,IAAIL,EAAmB,CAAE,MAAAI,CAAM,EAAGhC,CAAgB,CAAC,EAEhFqC,EAAqBH,EAAM,IAAIN,EAAmB,CAAE,MAAAI,CAAM,EAAGhC,CAAgB,CAAC,EAC9EsC,EAA8BL,EAAO,IAAIzB,EAAsBR,CAAgB,CAAC,EAChFuC,EAAc,OAAO,YAAY,CAAC,GAAGH,EAAqB,GAAGC,CAAkB,CAAC,EAChFG,EAAa,OAAO,YAAYF,CAA2B,EAEjE,MAAO,CACH,GAAI3C,EAAW,CAAE,IAAKA,CAAS,EAAI,KACnC,GAAGwC,EACH,YAAAI,EACA,WAAAC,CACJ,CACJ,EAEOC,EAAQX","sourcesContent":["import assert from \"node:assert\";\n\nimport type { DMMF } from \"@prisma/generator-helper\";\nimport type { JSONSchema7, JSONSchema7TypeName } from \"json-schema\";\n\nimport type { ModelMetaData, PrismaPrimitive, PropertyMap, PropertyMetaData, TransformOptions } from \"./types\";\n\nconst isDefined = <T>(value: T | null | undefined): value is T => value !== undefined && value !== null;\n\nconst getJSONSchemaScalar = (fieldType: PrismaPrimitive): JSONSchema7TypeName | JSONSchema7TypeName[] => {\n switch (fieldType) {\n case \"Int\":\n case \"BigInt\": {\n return \"integer\";\n }\n case \"DateTime\":\n case \"Bytes\":\n case \"String\": {\n return \"string\";\n }\n case \"Float\":\n case \"Decimal\": {\n return \"number\";\n }\n case \"Json\": {\n return [\"number\", \"string\", \"boolean\", \"object\", \"array\", \"null\"];\n }\n case \"Boolean\": {\n return \"boolean\";\n }\n default: {\n throw new Error(`Unhandled discriminated union member: ${JSON.stringify(fieldType)}`);\n }\n }\n};\n\nconst getJSONSchemaType = (field: DMMF.Field): JSONSchema7[\"type\"] => {\n const { isList, isRequired, kind, type } = field;\n\n let scalarFieldType: JSONSchema7[\"type\"] = \"object\";\n\n if (kind === \"scalar\" && !isList) {\n scalarFieldType = getJSONSchemaScalar(type as PrismaPrimitive);\n } else if (isList) {\n scalarFieldType = \"array\";\n } else if (kind === \"enum\") {\n scalarFieldType = \"string\";\n }\n\n if (isRequired || isList) {\n return scalarFieldType;\n }\n\n const isFieldUnion = Array.isArray(scalarFieldType);\n\n if (isFieldUnion) {\n return [...new Set([...scalarFieldType, \"null\"])] as JSONSchema7[\"type\"];\n }\n\n return [scalarFieldType as JSONSchema7TypeName, \"null\"];\n};\n\nconst getDefaultValue = (field: DMMF.Field): JSONSchema7[\"default\"] => {\n const fieldDefault = field.default;\n\n if (!field.hasDefaultValue) {\n return null;\n }\n\n if (field.kind === \"enum\") {\n return typeof fieldDefault === \"string\" ? fieldDefault : null;\n }\n\n if (field.kind !== \"scalar\") {\n return null;\n }\n\n switch (field.type) {\n case \"String\":\n case \"BigInt\":\n case \"DateTime\": {\n return typeof fieldDefault === \"string\" ? fieldDefault : null;\n }\n case \"Int\":\n case \"Float\":\n case \"Decimal\": {\n return typeof fieldDefault === \"number\" ? fieldDefault : null;\n }\n case \"Boolean\": {\n return typeof fieldDefault === \"boolean\" ? fieldDefault : null;\n }\n case \"Json\":\n case \"Bytes\": {\n return null;\n }\n default: {\n throw new Error(`Unhandled discriminated union member: ${JSON.stringify(field.type)}`);\n }\n }\n};\n\nconst getFormatByDMMFType = (fieldType: DMMF.Field[\"type\"]): string | undefined => {\n if (fieldType === \"DateTime\") {\n return \"date-time\";\n }\n\n return undefined;\n};\n\nconst getJSONSchemaForPropertyReference = (field: DMMF.Field, { persistOriginalType, schemaId }: TransformOptions): JSONSchema7 => {\n const notNullable = field.isRequired || field.isList;\n\n assert.equal(typeof field.type, \"string\");\n\n const typeReference = `#/definitions/${field.type}`;\n const reference = { $ref: schemaId ? `${schemaId}${typeReference}` : typeReference };\n\n return notNullable\n ? reference\n : {\n anyOf: [reference, { type: \"null\" }],\n ...(persistOriginalType && {\n originalType: field.type,\n }),\n };\n};\n\nconst getItemsByDMMFType = (field: DMMF.Field, transformOptions: TransformOptions): JSONSchema7[\"items\"] => {\n if ((field.kind === \"scalar\" && !field.isList) || field.kind === \"enum\") {\n return undefined;\n }\n\n if (field.kind === \"scalar\" && field.isList) {\n return { type: getJSONSchemaScalar(field.type as PrismaPrimitive) };\n }\n\n return getJSONSchemaForPropertyReference(field, transformOptions);\n};\n\nconst isSingleReference = (field: DMMF.Field) => field.kind !== \"scalar\" && !field.isList && field.kind !== \"enum\";\n\nconst getEnumListByDMMFType =\n (modelMetaData: ModelMetaData) =>\n (field: DMMF.Field): string[] | undefined => {\n const enumItem = modelMetaData.enums.find(({ name }) => name === field.type);\n\n if (!enumItem) {\n return undefined;\n }\n\n return enumItem.values.map((item) => item.name);\n };\n\nconst getDescription = (field: DMMF.Field) => field.documentation;\n\nconst getPropertyDefinition = (modelMetaData: ModelMetaData, transformOptions: TransformOptions, field: DMMF.Field) => {\n const type = getJSONSchemaType(field);\n const format = getFormatByDMMFType(field.type);\n const items = getItemsByDMMFType(field, transformOptions);\n const enumList = getEnumListByDMMFType(modelMetaData)(field);\n const defaultValue = getDefaultValue(field);\n const description = getDescription(field);\n\n return {\n type,\n ...(transformOptions.persistOriginalType && {\n originalType: field.type,\n }),\n ...(isDefined(defaultValue) && { default: defaultValue }),\n ...(isDefined(format) && { format }),\n ...(isDefined(items) && { items }),\n ...(isDefined(enumList) && { enum: enumList }),\n ...(isDefined(description) && { description }),\n };\n};\n\nconst getJSONSchemaProperty =\n (modelMetaData: ModelMetaData, transformOptions: TransformOptions) =>\n (field: DMMF.Field): PropertyMap => {\n const propertyMetaData: PropertyMetaData = {\n hasDefaultValue: field.hasDefaultValue,\n isScalar: field.kind === \"scalar\" || field.kind === \"enum\",\n required: field.isRequired,\n };\n\n const property = isSingleReference(field)\n ? getJSONSchemaForPropertyReference(field, transformOptions)\n : getPropertyDefinition(modelMetaData, transformOptions, field);\n\n return [field.name, property, propertyMetaData];\n };\n\nexport default getJSONSchemaProperty;\n","import type { DMMF } from \"@prisma/generator-helper\";\nimport type { JSONSchema7Definition } from \"json-schema\";\n\nimport getJSONSchemaProperty from \"./properties\";\nimport type { DefinitionMap, ModelMetaData, TransformOptions } from \"./types\";\n\nconst getRelationScalarFields = (model: DMMF.Model): string[] => model.fields.flatMap((field) => field.relationFromFields ?? []);\n\nconst getJSONSchemaModel =\n (modelMetaData: ModelMetaData, transformOptions: TransformOptions) =>\n (model: DMMF.Model): DefinitionMap => {\n const definitionPropertiesMap = model.fields.map(getJSONSchemaProperty(modelMetaData, transformOptions));\n\n const propertiesMap = definitionPropertiesMap.map(([name, definition]) => [name, definition] as DefinitionMap);\n const relationScalarFields = getRelationScalarFields(model);\n const propertiesWithoutRelationScalars = propertiesMap.filter((property) => !relationScalarFields.includes(property[0]));\n\n const properties = Object.fromEntries(transformOptions.keepRelationScalarFields === \"true\" ? propertiesMap : propertiesWithoutRelationScalars);\n\n const definition: JSONSchema7Definition = {\n properties,\n type: \"object\",\n };\n\n if (transformOptions.includeRequiredFields) {\n // eslint-disable-next-line unicorn/no-array-reduce\n definition.required = definitionPropertiesMap.reduce((filtered: string[], [name, , fieldMetaData]) => {\n if (fieldMetaData.required && fieldMetaData.isScalar && !fieldMetaData.hasDefaultValue) {\n filtered.push(name);\n }\n return filtered;\n }, []);\n }\n\n return [model.name, definition];\n };\n\nexport default getJSONSchemaModel;\n","import type { DMMF } from \"@prisma/generator-helper\";\nimport type { JSONSchema7, JSONSchema7Definition } from \"json-schema\";\n\nimport getJSONSchemaModel from \"./model\";\nimport type { TransformOptions } from \"./types\";\n\nconst toCamelCase = (name: string): string => name.slice(0, 1).toLowerCase() + name.slice(1);\n\nconst getPropertyDefinition =\n ({ schemaId }: TransformOptions) =>\n (model: DMMF.Model): [name: string, reference: JSONSchema7Definition] => {\n const reference = `#/definitions/${model.name}`;\n\n return [\n toCamelCase(model.name),\n {\n $ref: schemaId ? `${schemaId}${reference}` : reference,\n },\n ];\n };\n\nconst transformDmmf = (dmmf: DMMF.Document, transformOptions: TransformOptions = {}): JSONSchema7 => {\n // TODO: Remove default values as soon as prisma version < 3.10.0 doesn't have to be supported anymore\n const { enums = [], models = [], types = [] } = dmmf.datamodel;\n const initialJSON = {\n $schema: \"http://json-schema.org/draft-07/schema#\",\n definitions: {},\n type: \"object\",\n } as JSONSchema7;\n const { schemaId } = transformOptions;\n\n const modelDefinitionsMap = models.map(getJSONSchemaModel({ enums }, transformOptions));\n\n const typeDefinitionsMap = types.map(getJSONSchemaModel({ enums }, transformOptions));\n const modelPropertyDefinitionsMap = models.map(getPropertyDefinition(transformOptions));\n const definitions = Object.fromEntries([...modelDefinitionsMap, ...typeDefinitionsMap]);\n const properties = Object.fromEntries(modelPropertyDefinitionsMap);\n\n return {\n ...(schemaId ? { $id: schemaId } : null),\n ...initialJSON,\n definitions,\n properties,\n };\n};\n\nexport default transformDmmf;\n"]}
|