@visulima/prisma-dmmf-transformer 1.0.0 → 1.0.1

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 CHANGED
@@ -1,3 +1,10 @@
1
+ ## @visulima/prisma-dmmf-transformer [1.0.1](https://github.com/visulima/visulima/compare/@visulima/prisma-dmmf-transformer@1.0.0...@visulima/prisma-dmmf-transformer@1.0.1) (2022-12-01)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * added correct folder to files key in package.json ([da9f987](https://github.com/visulima/visulima/commit/da9f9871462a0b2663046cde5f05e9a90df4c496))
7
+
1
8
  ## @visulima/prisma-dmmf-transformer 1.0.0 (2022-11-15)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/prisma-dmmf-transformer",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A generator for Prisma to generate a valid JSON Schema v7.",
5
5
  "keywords": [
6
6
  "anolilab",
@@ -49,8 +49,7 @@
49
49
  "source": "src/index.ts",
50
50
  "types": "dist/index.d.ts",
51
51
  "files": [
52
- "src",
53
- "dist",
52
+ "dist/**",
54
53
  "README.md",
55
54
  "CHANGELOG.md",
56
55
  "LICENSE.md"
@@ -61,25 +60,25 @@
61
60
  "clean": "rimraf node_modules dist",
62
61
  "coverage": "vitest run --coverage",
63
62
  "dev": "pnpm predev && pnpm run build --watch",
64
- "lint:eslint": "cross-env NO_LOGS=true eslint --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs",
63
+ "lint:eslint": "cross-env NO_LOGS=true eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs --cache --cache-strategy content .",
65
64
  "lint:eslint:fix": "pnpm run lint:eslint --fix",
66
65
  "test": "vitest"
67
66
  },
68
67
  "dependencies": {
69
- "@prisma/generator-helper": "4.6.1",
70
- "@prisma/internals": "4.6.1"
68
+ "@prisma/generator-helper": "4.7.0",
69
+ "@prisma/internals": "4.7.0"
71
70
  },
72
71
  "devDependencies": {
73
72
  "@anolilab/eslint-config": "^4.0.9",
74
73
  "@anolilab/semantic-release-preset": "^2.0.7",
75
- "@prisma/client": "4.6.1",
74
+ "@prisma/client": "4.7.0",
76
75
  "@rushstack/eslint-plugin-security": "^0.5.0",
77
76
  "@types/json-schema": "7.0.11",
78
77
  "@types/micromatch": "^4.0.2",
79
78
  "@types/node": "^18.8.4",
80
79
  "@typescript-eslint/eslint-plugin": "^5.40.0",
81
80
  "@typescript-eslint/parser": "^5.40.0",
82
- "ajv": "8.11.0",
81
+ "ajv": "8.11.2",
83
82
  "ajv-formats": "2.1.1",
84
83
  "cross-env": "^7.0.3",
85
84
  "eslint": "^8.25.0",
@@ -97,22 +96,22 @@
97
96
  "eslint-plugin-optimize-regex": "^1.2.1",
98
97
  "eslint-plugin-promise": "^6.0.1",
99
98
  "eslint-plugin-radar": "^0.2.1",
100
- "eslint-plugin-react": "7.31.10",
99
+ "eslint-plugin-react": "7.31.11",
101
100
  "eslint-plugin-react-hooks": "4.6.0",
102
101
  "eslint-plugin-simple-import-sort": "^8.0.0",
103
102
  "eslint-plugin-sort-keys-fix": "^1.1.2",
104
103
  "eslint-plugin-testing-library": "^5.7.2",
105
- "eslint-plugin-unicorn": "^44.0.2",
104
+ "eslint-plugin-unicorn": "^45.0.0",
106
105
  "eslint-plugin-you-dont-need-lodash-underscore": "^6.12.0",
107
106
  "eslint-plugin-you-dont-need-momentjs": "^1.6.0",
108
107
  "prettier": "^2.7.1",
109
- "prisma": "4.6.1",
108
+ "prisma": "4.7.0",
110
109
  "read-pkg": "^7.1.0",
111
110
  "rimraf": "^3.0.2",
112
111
  "semantic-release": "^19.0.5",
113
112
  "tsup": "^6.2.3",
114
113
  "typescript": "^4.8.4",
115
- "vitest": "^0.24.1"
114
+ "vitest": "^0.25.0"
116
115
  },
117
116
  "peerDependencies": {
118
117
  "@prisma/client": "3.* || 4.*",
package/src/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export { default as transformDMMF } from "./transform-dmmf";
2
- export { default as getJSONSchemaProperty } from "./properties";
3
-
4
- export type {
5
- DefinitionMap, PropertyMap, PropertyMetaData, TransformOptions, ModelMetaData,
6
- } from "./types.d";
package/src/model.ts DELETED
@@ -1,37 +0,0 @@
1
- import type { DMMF } from "@prisma/generator-helper";
2
- import type { JSONSchema7Definition } from "json-schema";
3
-
4
- import getJSONSchemaProperty from "./properties";
5
- import type { DefinitionMap, ModelMetaData, TransformOptions } from "./types.d";
6
-
7
- function getRelationScalarFields(model: DMMF.Model): string[] {
8
- return model.fields.flatMap((field) => field.relationFromFields || []);
9
- }
10
-
11
- const getJSONSchemaModel = (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (model: DMMF.Model): DefinitionMap => {
12
- const definitionPropertiesMap = model.fields.map(getJSONSchemaProperty(modelMetaData, transformOptions));
13
-
14
- const propertiesMap = definitionPropertiesMap.map(([name, definition]) => [name, definition] as DefinitionMap);
15
- const relationScalarFields = getRelationScalarFields(model);
16
- const propertiesWithoutRelationScalars = propertiesMap.filter((property) => !relationScalarFields.includes(property[0]));
17
-
18
- const properties = Object.fromEntries(transformOptions?.keepRelationScalarFields === "true" ? propertiesMap : propertiesWithoutRelationScalars);
19
-
20
- const definition: JSONSchema7Definition = {
21
- type: "object",
22
- properties,
23
- };
24
-
25
- if (transformOptions.includeRequiredFields) {
26
- definition.required = definitionPropertiesMap.reduce((filtered: string[], [name, , fieldMetaData]) => {
27
- if (fieldMetaData.required && fieldMetaData.isScalar && !fieldMetaData.hasDefaultValue) {
28
- filtered.push(name);
29
- }
30
- return filtered;
31
- }, []);
32
- }
33
-
34
- return [model.name, definition];
35
- };
36
-
37
- export default getJSONSchemaModel;
package/src/properties.ts DELETED
@@ -1,194 +0,0 @@
1
- import type { DMMF } from "@prisma/generator-helper";
2
- import type { JSONSchema7, JSONSchema7TypeName } from "json-schema";
3
- import assert from "node:assert";
4
-
5
- import type {
6
- ModelMetaData, PrismaPrimitive, PropertyMap, PropertyMetaData, TransformOptions,
7
- } from "./types.d";
8
-
9
- function isDefined<T>(value: T | undefined | null): value is T {
10
- return value !== undefined && value !== null;
11
- }
12
-
13
- const getJSONSchemaScalar = (fieldType: PrismaPrimitive): JSONSchema7TypeName | Array<JSONSchema7TypeName> => {
14
- switch (fieldType) {
15
- case "Int":
16
- case "BigInt": {
17
- return "integer";
18
- }
19
- case "DateTime":
20
- case "Bytes":
21
- case "String": {
22
- return "string";
23
- }
24
- case "Float":
25
- case "Decimal": {
26
- return "number";
27
- }
28
- case "Json": {
29
- return ["number", "string", "boolean", "object", "array", "null"];
30
- }
31
- case "Boolean": {
32
- return "boolean";
33
- }
34
- default: {
35
- throw new Error(`Unhandled discriminated union member: ${JSON.stringify(fieldType)}`);
36
- }
37
- }
38
- };
39
-
40
- const getJSONSchemaType = (field: DMMF.Field): JSONSchema7["type"] => {
41
- const {
42
- isList, isRequired, kind, type,
43
- } = field;
44
-
45
- let scalarFieldType: JSONSchema7["type"] = "object";
46
-
47
- if (kind === "scalar" && !isList) {
48
- scalarFieldType = getJSONSchemaScalar(type as PrismaPrimitive);
49
- } else if (isList) {
50
- scalarFieldType = "array";
51
- } else if (kind === "enum") {
52
- scalarFieldType = "string";
53
- }
54
-
55
- if (isRequired || isList) {
56
- return scalarFieldType;
57
- }
58
-
59
- const isFieldUnion = Array.isArray(scalarFieldType);
60
-
61
- if (isFieldUnion) {
62
- return [...new Set([...scalarFieldType, "null"])] as JSONSchema7["type"];
63
- }
64
-
65
- return [scalarFieldType as JSONSchema7TypeName, "null"];
66
- };
67
-
68
- const getDefaultValue = (field: DMMF.Field): JSONSchema7["default"] => {
69
- const fieldDefault = field.default;
70
-
71
- if (!field.hasDefaultValue) {
72
- return null;
73
- }
74
-
75
- if (field.kind === "enum") {
76
- return typeof fieldDefault === "string" ? fieldDefault : null;
77
- }
78
-
79
- if (field.kind !== "scalar") {
80
- return null;
81
- }
82
-
83
- switch (field.type) {
84
- case "String":
85
- case "BigInt":
86
- case "DateTime": {
87
- return typeof fieldDefault === "string" ? fieldDefault : null;
88
- }
89
- case "Int":
90
- case "Float":
91
- case "Decimal": {
92
- return typeof fieldDefault === "number" ? fieldDefault : null;
93
- }
94
- case "Boolean": {
95
- return typeof fieldDefault === "boolean" ? fieldDefault : null;
96
- }
97
- case "Json":
98
- case "Bytes": {
99
- return null;
100
- }
101
- default: {
102
- throw new Error(`Unhandled discriminated union member: ${JSON.stringify(field.type)}`);
103
- }
104
- }
105
- };
106
-
107
- const getFormatByDMMFType = (fieldType: DMMF.Field["type"]): string | undefined => {
108
- if (fieldType === "DateTime") {
109
- return "date-time";
110
- }
111
-
112
- return undefined;
113
- };
114
-
115
- const getJSONSchemaForPropertyReference = (field: DMMF.Field, { schemaId, persistOriginalType }: TransformOptions): JSONSchema7 => {
116
- const notNullable = field.isRequired || field.isList;
117
-
118
- assert.equal(typeof field.type, "string");
119
-
120
- const typeReference = `#/definitions/${field.type}`;
121
- const reference = { $ref: schemaId ? `${schemaId}${typeReference}` : typeReference };
122
-
123
- return notNullable
124
- ? reference
125
- : {
126
- anyOf: [reference, { type: "null" }],
127
- ...(persistOriginalType && {
128
- originalType: field.type,
129
- }),
130
- };
131
- };
132
-
133
- const getItemsByDMMFType = (field: DMMF.Field, transformOptions: TransformOptions): JSONSchema7["items"] => {
134
- if ((field.kind === "scalar" && !field.isList) || field.kind === "enum") {
135
- return undefined;
136
- }
137
-
138
- if (field.kind === "scalar" && field.isList) {
139
- return { type: getJSONSchemaScalar(field.type as PrismaPrimitive) };
140
- }
141
-
142
- return getJSONSchemaForPropertyReference(field, transformOptions);
143
- };
144
-
145
- const isSingleReference = (field: DMMF.Field) => field.kind !== "scalar" && !field.isList && field.kind !== "enum";
146
-
147
- const getEnumListByDMMFType = (modelMetaData: ModelMetaData) => (field: DMMF.Field): string[] | undefined => {
148
- const enumItem = modelMetaData.enums.find(({ name }) => name === field.type);
149
-
150
- if (!enumItem) {
151
- return undefined;
152
- }
153
-
154
- return enumItem.values.map((item) => item.name);
155
- };
156
-
157
- const getDescription = (field: DMMF.Field) => field.documentation;
158
-
159
- const getPropertyDefinition = (modelMetaData: ModelMetaData, transformOptions: TransformOptions, field: DMMF.Field) => {
160
- const type = getJSONSchemaType(field);
161
- const format = getFormatByDMMFType(field.type);
162
- const items = getItemsByDMMFType(field, transformOptions);
163
- const enumList = getEnumListByDMMFType(modelMetaData)(field);
164
- const defaultValue = getDefaultValue(field);
165
- const description = getDescription(field);
166
-
167
- return {
168
- type,
169
- ...(transformOptions.persistOriginalType && {
170
- originalType: field.type,
171
- }),
172
- ...(isDefined(defaultValue) && { default: defaultValue }),
173
- ...(isDefined(format) && { format }),
174
- ...(isDefined(items) && { items }),
175
- ...(isDefined(enumList) && { enum: enumList }),
176
- ...(isDefined(description) && { description }),
177
- };
178
- };
179
-
180
- const getJSONSchemaProperty = (modelMetaData: ModelMetaData, transformOptions: TransformOptions) => (field: DMMF.Field): PropertyMap => {
181
- const propertyMetaData: PropertyMetaData = {
182
- required: field.isRequired,
183
- hasDefaultValue: field.hasDefaultValue,
184
- isScalar: field.kind === "scalar" || field.kind === "enum",
185
- };
186
-
187
- const property = isSingleReference(field)
188
- ? getJSONSchemaForPropertyReference(field, transformOptions)
189
- : getPropertyDefinition(modelMetaData, transformOptions, field);
190
-
191
- return [field.name, property, propertyMetaData];
192
- };
193
-
194
- export default getJSONSchemaProperty;
@@ -1,44 +0,0 @@
1
- import type { DMMF } from "@prisma/generator-helper";
2
- import type { JSONSchema7, JSONSchema7Definition } from "json-schema";
3
-
4
- import getJSONSchemaModel from "./model";
5
- import type { TransformOptions } from "./types.d";
6
-
7
- const toCamelCase = (name: string): string => name.slice(0, 1).toLowerCase() + name.slice(1);
8
-
9
- const getPropertyDefinition = ({ schemaId }: TransformOptions) => (model: DMMF.Model): [name: string, reference: JSONSchema7Definition] => {
10
- const reference = `#/definitions/${model.name}`;
11
-
12
- return [
13
- toCamelCase(model.name),
14
- {
15
- $ref: schemaId ? `${schemaId}${reference}` : reference,
16
- },
17
- ];
18
- };
19
-
20
- const transformDmmf = (dmmf: DMMF.Document, transformOptions: TransformOptions = {}): JSONSchema7 => {
21
- // TODO: Remove default values as soon as prisma version < 3.10.0 doesn't have to be supported anymore
22
- const { models = [], enums = [], types = [] } = dmmf.datamodel;
23
- const initialJSON = {
24
- $schema: "http://json-schema.org/draft-07/schema#",
25
- definitions: {},
26
- type: "object",
27
- } as JSONSchema7;
28
- const { schemaId } = transformOptions;
29
-
30
- const modelDefinitionsMap = models.map(getJSONSchemaModel({ enums }, transformOptions));
31
- const typeDefinitionsMap = types.map(getJSONSchemaModel({ enums }, transformOptions));
32
- const modelPropertyDefinitionsMap = models.map(getPropertyDefinition(transformOptions));
33
- const definitions = Object.fromEntries([...modelDefinitionsMap, ...typeDefinitionsMap]);
34
- const properties = Object.fromEntries(modelPropertyDefinitionsMap);
35
-
36
- return {
37
- ...(schemaId ? { $id: schemaId } : null),
38
- ...initialJSON,
39
- definitions,
40
- properties,
41
- };
42
- };
43
-
44
- export default transformDmmf;
package/src/types.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import type { DMMF } from "@prisma/generator-helper";
2
- import type { JSONSchema7Definition } from "json-schema";
3
-
4
- export type PrismaPrimitive = "String" | "BigInt" | "Bytes" | "Decimal" | "Boolean" | "Int" | "Float" | "Json" | "DateTime";
5
-
6
- export interface PropertyMetaData {
7
- required: boolean;
8
- hasDefaultValue: boolean;
9
- isScalar: boolean;
10
- }
11
-
12
- export interface ModelMetaData {
13
- enums: DMMF.DatamodelEnum[];
14
- }
15
-
16
- export type DefinitionMap = [name: string, definition: JSONSchema7Definition];
17
- export type PropertyMap = [...DefinitionMap, PropertyMetaData];
18
-
19
- export interface TransformOptions {
20
- keepRelationScalarFields?: "true" | "false";
21
- schemaId?: string;
22
- includeRequiredFields?: "true" | "false";
23
- persistOriginalType?: "true" | "false";
24
- }