prisma-to-zod-v4 0.6.4 → 0.6.5
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/dist/index.js +83 -23
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -213047,7 +213047,7 @@ Additional information: BADCLIENT: Bad error code, ${badCode} not found in range
|
|
|
213047
213047
|
});
|
|
213048
213048
|
|
|
213049
213049
|
// package.json
|
|
213050
|
-
var version = "0.6.
|
|
213050
|
+
var version = "0.6.5";
|
|
213051
213051
|
|
|
213052
213052
|
// src/index.ts
|
|
213053
213053
|
var import_generator_helper = require("@prisma/generator-helper");
|
|
@@ -213057,15 +213057,16 @@ var import_path2 = __toESM(require("path"));
|
|
|
213057
213057
|
// src/config.ts
|
|
213058
213058
|
var import_zod = require("zod");
|
|
213059
213059
|
var stringBoolean = import_zod.z.enum(["true", "false"]);
|
|
213060
|
-
var configBoolean = stringBoolean.
|
|
213060
|
+
var configBoolean = stringBoolean.transform((arg) => arg === "true");
|
|
213061
213061
|
var configSchema = import_zod.z.object({
|
|
213062
|
-
|
|
213062
|
+
imports: import_zod.z.string().optional(),
|
|
213063
213063
|
modelSuffix: import_zod.z.string().default("Model"),
|
|
213064
213064
|
modelCase: import_zod.z.enum(["PascalCase", "camelCase"]).default("PascalCase"),
|
|
213065
|
-
useDecimalJs: configBoolean,
|
|
213066
213065
|
useCoerce: configBoolean.default(false),
|
|
213067
|
-
|
|
213068
|
-
|
|
213066
|
+
useDecimalJs: configBoolean.default(true),
|
|
213067
|
+
useStandaloneEnums: configBoolean.default(true),
|
|
213068
|
+
relationModel: import_zod.z.union([import_zod.z.literal("default"), configBoolean.default(true)]),
|
|
213069
|
+
prismaJsonNullability: configBoolean.default(true)
|
|
213069
213070
|
});
|
|
213070
213071
|
|
|
213071
213072
|
// src/generator.ts
|
|
@@ -213133,9 +213134,10 @@ var computeModifiers = (docString) => {
|
|
|
213133
213134
|
};
|
|
213134
213135
|
|
|
213135
213136
|
// src/types.ts
|
|
213136
|
-
var getZodConstructor = (field, getRelatedModelName = (name) => name.toString(), nativeType,
|
|
213137
|
+
var getZodConstructor = (field, getRelatedModelName = (name) => name.toString(), nativeType, config) => {
|
|
213137
213138
|
var _a, _b, _c, _d;
|
|
213138
213139
|
let zodType = "z.unknown()";
|
|
213140
|
+
const useCoerce = (config == null ? void 0 : config.useCoerce) ?? false;
|
|
213139
213141
|
const zodVar = useCoerce ? "z.coerce" : "z";
|
|
213140
213142
|
const extraModifiers = [""];
|
|
213141
213143
|
if (field.kind === "scalar") {
|
|
@@ -213232,7 +213234,12 @@ var getZodConstructor = (field, getRelatedModelName = (name) => name.toString(),
|
|
|
213232
213234
|
break;
|
|
213233
213235
|
}
|
|
213234
213236
|
} else if (field.kind === "enum") {
|
|
213235
|
-
|
|
213237
|
+
if (config == null ? void 0 : config.useStandaloneEnums) {
|
|
213238
|
+
const camelCase = field.type.charAt(0).toLowerCase() + field.type.slice(1);
|
|
213239
|
+
zodType = `${camelCase}Schema`;
|
|
213240
|
+
} else {
|
|
213241
|
+
zodType = `z.enum(${field.type})`;
|
|
213242
|
+
}
|
|
213236
213243
|
} else if (field.kind === "object") {
|
|
213237
213244
|
zodType = getRelatedModelName(field.type);
|
|
213238
213245
|
}
|
|
@@ -213342,12 +213349,28 @@ var writeImportsForModel = (model, sourceFile, config, { schemaPath, outputPath,
|
|
|
213342
213349
|
const relationFields = model.fields.filter((f) => f.kind === "object");
|
|
213343
213350
|
const relativePath = import_path.default.relative(outputPath, clientPath);
|
|
213344
213351
|
if (enumFields.length > 0) {
|
|
213345
|
-
|
|
213346
|
-
|
|
213347
|
-
|
|
213348
|
-
|
|
213349
|
-
|
|
213350
|
-
|
|
213352
|
+
if (config.useStandaloneEnums) {
|
|
213353
|
+
const uniqueEnumSchemas = Array.from(
|
|
213354
|
+
new Set(
|
|
213355
|
+
enumFields.map((f) => {
|
|
213356
|
+
const camelCase = f.type.charAt(0).toLowerCase() + f.type.slice(1);
|
|
213357
|
+
return `${camelCase}Schema`;
|
|
213358
|
+
})
|
|
213359
|
+
)
|
|
213360
|
+
);
|
|
213361
|
+
importList.push({
|
|
213362
|
+
kind: import_ts_morph.StructureKind.ImportDeclaration,
|
|
213363
|
+
moduleSpecifier: "./enums",
|
|
213364
|
+
namedImports: uniqueEnumSchemas
|
|
213365
|
+
});
|
|
213366
|
+
} else {
|
|
213367
|
+
importList.push({
|
|
213368
|
+
kind: import_ts_morph.StructureKind.ImportDeclaration,
|
|
213369
|
+
isTypeOnly: false,
|
|
213370
|
+
moduleSpecifier: dotSlash(relativePath),
|
|
213371
|
+
namedImports: enumFields.map((f) => f.type)
|
|
213372
|
+
});
|
|
213373
|
+
}
|
|
213351
213374
|
}
|
|
213352
213375
|
if (config.relationModel !== false && relationFields.length > 0) {
|
|
213353
213376
|
const filteredFields = relationFields.filter((f) => f.type !== model.name);
|
|
@@ -213417,12 +213440,7 @@ var generateSchemaForModel = (model, sourceFile, config, { schemaPath }) => {
|
|
|
213417
213440
|
writeArray(writer, getJSDocs(field.documentation));
|
|
213418
213441
|
const nativeType = modelNativeTypes.get(field.name);
|
|
213419
213442
|
writer.write(
|
|
213420
|
-
`${field.name}: ${getZodConstructor(
|
|
213421
|
-
field,
|
|
213422
|
-
void 0,
|
|
213423
|
-
nativeType,
|
|
213424
|
-
config.useCoerce
|
|
213425
|
-
)}`
|
|
213443
|
+
`${field.name}: ${getZodConstructor(field, void 0, nativeType, config)}`
|
|
213426
213444
|
).write(",").newLine();
|
|
213427
213445
|
});
|
|
213428
213446
|
}).write(")");
|
|
@@ -213473,7 +213491,7 @@ var generateRelatedSchemaForModel = (model, sourceFile, config, _prismaOptions)
|
|
|
213473
213491
|
field,
|
|
213474
213492
|
relatedModelName,
|
|
213475
213493
|
null,
|
|
213476
|
-
config
|
|
213494
|
+
config
|
|
213477
213495
|
)},`
|
|
213478
213496
|
).newLine();
|
|
213479
213497
|
});
|
|
@@ -213491,13 +213509,45 @@ var populateModelFile = (model, sourceFile, config, prismaOptions) => {
|
|
|
213491
213509
|
if (needsRelatedModel(model, config))
|
|
213492
213510
|
generateRelatedSchemaForModel(model, sourceFile, config, prismaOptions);
|
|
213493
213511
|
};
|
|
213494
|
-
var generateBarrelFile = (models, indexFile) => {
|
|
213512
|
+
var generateBarrelFile = (models, indexFile, config, hasEnums) => {
|
|
213513
|
+
if ((config == null ? void 0 : config.useStandaloneEnums) && hasEnums) {
|
|
213514
|
+
indexFile.addExportDeclaration({
|
|
213515
|
+
moduleSpecifier: "./enums"
|
|
213516
|
+
});
|
|
213517
|
+
}
|
|
213495
213518
|
models.forEach((model) => {
|
|
213496
213519
|
indexFile.addExportDeclaration({
|
|
213497
213520
|
moduleSpecifier: `./${model.name.toLowerCase()}`
|
|
213498
213521
|
});
|
|
213499
213522
|
});
|
|
213500
213523
|
};
|
|
213524
|
+
var generateEnumsFile = (enums, sourceFile) => {
|
|
213525
|
+
sourceFile.addImportDeclaration({
|
|
213526
|
+
kind: import_ts_morph.StructureKind.ImportDeclaration,
|
|
213527
|
+
namespaceImport: "z",
|
|
213528
|
+
moduleSpecifier: "zod"
|
|
213529
|
+
});
|
|
213530
|
+
enums.forEach((enumDef) => {
|
|
213531
|
+
const values = enumDef.values.map((v) => v.name);
|
|
213532
|
+
const enumInitializer = `z.enum([${values.map((v) => `'${v}'`).join(", ")}])`;
|
|
213533
|
+
const camelCaseName = enumDef.name.charAt(0).toLowerCase() + enumDef.name.slice(1);
|
|
213534
|
+
sourceFile.addVariableStatement({
|
|
213535
|
+
isExported: true,
|
|
213536
|
+
declarationKind: import_ts_morph.VariableDeclarationKind.Const,
|
|
213537
|
+
declarations: [
|
|
213538
|
+
{
|
|
213539
|
+
name: `${camelCaseName}Schema`,
|
|
213540
|
+
initializer: enumInitializer
|
|
213541
|
+
}
|
|
213542
|
+
]
|
|
213543
|
+
});
|
|
213544
|
+
sourceFile.addTypeAlias({
|
|
213545
|
+
isExported: true,
|
|
213546
|
+
name: `${enumDef.name}Schema`,
|
|
213547
|
+
type: `z.infer<typeof ${camelCaseName}Schema>`
|
|
213548
|
+
});
|
|
213549
|
+
});
|
|
213550
|
+
};
|
|
213501
213551
|
|
|
213502
213552
|
// src/index.ts
|
|
213503
213553
|
var import_ts_morph2 = require("ts-morph");
|
|
@@ -213516,6 +213566,7 @@ var import_ts_morph2 = require("ts-morph");
|
|
|
213516
213566
|
}
|
|
213517
213567
|
});
|
|
213518
213568
|
const models = [...options.dmmf.datamodel.models];
|
|
213569
|
+
const enums = [...options.dmmf.datamodel.enums];
|
|
213519
213570
|
const schemaPath = options.schemaPath;
|
|
213520
213571
|
const schemaDir = import_path2.default.dirname(schemaPath);
|
|
213521
213572
|
const outputPath = options.generator.output.value;
|
|
@@ -213535,12 +213586,21 @@ var import_ts_morph2 = require("ts-morph");
|
|
|
213535
213586
|
// Pass the directory, not the file
|
|
213536
213587
|
};
|
|
213537
213588
|
const indexFile = project.createSourceFile(`${outputPath}/index.ts`, {}, { overwrite: true });
|
|
213538
|
-
generateBarrelFile(models, indexFile);
|
|
213589
|
+
generateBarrelFile(models, indexFile, config, enums.length > 0);
|
|
213539
213590
|
indexFile.formatText({
|
|
213540
213591
|
indentSize: 2,
|
|
213541
213592
|
convertTabsToSpaces: true,
|
|
213542
213593
|
semicolons: import_typescript.SemicolonPreference.Remove
|
|
213543
213594
|
});
|
|
213595
|
+
if (config.useStandaloneEnums && enums.length > 0) {
|
|
213596
|
+
const enumsFile = project.createSourceFile(`${outputPath}/enums.ts`, {}, { overwrite: true });
|
|
213597
|
+
generateEnumsFile(enums, enumsFile);
|
|
213598
|
+
enumsFile.formatText({
|
|
213599
|
+
indentSize: 2,
|
|
213600
|
+
convertTabsToSpaces: true,
|
|
213601
|
+
semicolons: import_typescript.SemicolonPreference.Remove
|
|
213602
|
+
});
|
|
213603
|
+
}
|
|
213544
213604
|
models.forEach((model) => {
|
|
213545
213605
|
const sourceFile = project.createSourceFile(
|
|
213546
213606
|
`${outputPath}/${model.name.toLowerCase()}.ts`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-to-zod-v4",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.5",
|
|
4
4
|
"description": "A Prisma generator that creates Zod schemas for all of your models",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "yassinrais",
|
|
@@ -84,4 +84,4 @@
|
|
|
84
84
|
"engines": {
|
|
85
85
|
"node": ">=14"
|
|
86
86
|
}
|
|
87
|
-
}
|
|
87
|
+
}
|