mutano 3.0.23 → 3.1.0
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/README.md +10 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +38 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -145,6 +145,7 @@ export type UpdateableUser = Updateable<User>;
|
|
|
145
145
|
|
|
146
146
|
// Zod specific
|
|
147
147
|
useDateType?: boolean,
|
|
148
|
+
useBooleanType?: boolean,
|
|
148
149
|
useTrim?: boolean,
|
|
149
150
|
nullish?: boolean,
|
|
150
151
|
requiredString?: boolean,
|
|
@@ -159,6 +160,15 @@ export type UpdateableUser = Updateable<User>;
|
|
|
159
160
|
}
|
|
160
161
|
```
|
|
161
162
|
|
|
163
|
+
### Zod Configuration Options
|
|
164
|
+
|
|
165
|
+
- **`useDateType`**: When `true`, generates `z.union([z.number(), z.string(), z.date()]).pipe(z.coerce.date())` instead of `z.date()` for date fields
|
|
166
|
+
- **`useBooleanType`**: When `true`, generates `z.union([z.number(), z.string(), z.boolean()]).pipe(z.coerce.boolean())` instead of `z.boolean()` for boolean fields
|
|
167
|
+
- **`useTrim`**: When `true`, adds `.trim()` to string fields
|
|
168
|
+
- **`nullish`**: When `true`, uses `.nullish()` instead of `.nullable()` for nullable fields (except selectable schemas)
|
|
169
|
+
- **`requiredString`**: When `true`, adds `.min(1)` validation to required string fields
|
|
170
|
+
- **`version`**: Zod version (3 or 4) for compatibility
|
|
171
|
+
|
|
162
172
|
### Global Options
|
|
163
173
|
| Option | Description |
|
|
164
174
|
|--------|-------------|
|
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -311,9 +311,13 @@ function getType(op, desc, config, destination) {
|
|
|
311
311
|
const enumString = `z.enum([${enumValues.map((v) => `'${v}'`).join(",")}])`;
|
|
312
312
|
const nullishOption = destination.nullish;
|
|
313
313
|
const nullableMethod = nullishOption && op !== "selectable" ? "nullish" : "nullable";
|
|
314
|
-
if ((op === "table" || op === "insertable") && hasDefaultValue && Default !== null && !isGenerated) {
|
|
315
|
-
if (shouldBeNullable) {
|
|
314
|
+
if ((op === "table" || op === "insertable" || op === "updateable") && hasDefaultValue && Default !== null && !isGenerated) {
|
|
315
|
+
if (shouldBeNullable && shouldBeOptional) {
|
|
316
316
|
return `${enumString}.${nullableMethod}().default('${Default}')`;
|
|
317
|
+
} else if (shouldBeNullable) {
|
|
318
|
+
return `${enumString}.${nullableMethod}().default('${Default}')`;
|
|
319
|
+
} else if (shouldBeOptional) {
|
|
320
|
+
return `${enumString}.optional().default('${Default}')`;
|
|
317
321
|
} else {
|
|
318
322
|
return `${enumString}.default('${Default}')`;
|
|
319
323
|
}
|
|
@@ -392,14 +396,23 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
|
|
|
392
396
|
baseType = "number";
|
|
393
397
|
}
|
|
394
398
|
} else if (typeMappings.booleanTypes.includes(type)) {
|
|
395
|
-
|
|
399
|
+
if (isZodDestination) {
|
|
400
|
+
const useBooleanType = destination.useBooleanType;
|
|
401
|
+
if (useBooleanType) {
|
|
402
|
+
baseType = "z.union([z.number(), z.string(), z.boolean()]).pipe(z.coerce.boolean())";
|
|
403
|
+
} else {
|
|
404
|
+
baseType = "z.boolean()";
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
baseType = "boolean";
|
|
408
|
+
}
|
|
396
409
|
} else if (typeMappings.stringTypes.includes(type)) {
|
|
397
410
|
if (isZodDestination) {
|
|
398
411
|
const useTrim = destination.useTrim;
|
|
399
412
|
const requiredString = destination.requiredString;
|
|
400
413
|
baseType = "z.string()";
|
|
401
414
|
if (useTrim && op !== "selectable") baseType += ".trim()";
|
|
402
|
-
if (requiredString && !shouldBeNullable && op !== "selectable") baseType += ".min(1)";
|
|
415
|
+
if (requiredString && !shouldBeNullable && op !== "selectable" && !(op === "updateable" && hasDefaultValue)) baseType += ".min(1)";
|
|
403
416
|
} else {
|
|
404
417
|
baseType = "string";
|
|
405
418
|
}
|
|
@@ -409,7 +422,7 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
|
|
|
409
422
|
if (isZodDestination) {
|
|
410
423
|
const nullishOption = destination.nullish;
|
|
411
424
|
const nullableMethod = nullishOption && op !== "selectable" ? "nullish" : "nullable";
|
|
412
|
-
if ((op === "table" || op === "insertable") && hasDefaultValue && Default !== null && !isGenerated) {
|
|
425
|
+
if ((op === "table" || op === "insertable" || op === "updateable") && hasDefaultValue && Default !== null && !isGenerated) {
|
|
413
426
|
let defaultValueFormatted = Default;
|
|
414
427
|
if (typeMappings.stringTypes.includes(type) || typeMappings.dateTypes.includes(type)) {
|
|
415
428
|
defaultValueFormatted = `'${Default}'`;
|
|
@@ -420,8 +433,12 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
|
|
|
420
433
|
} else {
|
|
421
434
|
defaultValueFormatted = `'${Default}'`;
|
|
422
435
|
}
|
|
423
|
-
if (shouldBeNullable) {
|
|
436
|
+
if (shouldBeNullable && shouldBeOptional) {
|
|
437
|
+
return `${baseType}.${nullableMethod}().default(${defaultValueFormatted})`;
|
|
438
|
+
} else if (shouldBeNullable) {
|
|
424
439
|
return `${baseType}.${nullableMethod}().default(${defaultValueFormatted})`;
|
|
440
|
+
} else if (shouldBeOptional) {
|
|
441
|
+
return `${baseType}.optional().default(${defaultValueFormatted})`;
|
|
425
442
|
} else {
|
|
426
443
|
return `${baseType}.default(${defaultValueFormatted})`;
|
|
427
444
|
}
|
|
@@ -453,6 +470,17 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
|
|
|
453
470
|
}
|
|
454
471
|
}
|
|
455
472
|
|
|
473
|
+
function isAutoGeneratedDateTimeField(desc, schemaType) {
|
|
474
|
+
const { Type, Extra } = desc;
|
|
475
|
+
const type = schemaType === "prisma" ? Type : Type.toLowerCase();
|
|
476
|
+
const typeMappings = getTypeMappings(schemaType);
|
|
477
|
+
const isDateField = typeMappings.dateTypes.includes(type);
|
|
478
|
+
if (!isDateField) {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
const isGenerated = Extra.toLowerCase().includes("auto_increment") || Extra.toLowerCase().includes("default_generated");
|
|
482
|
+
return isGenerated;
|
|
483
|
+
}
|
|
456
484
|
function toSnakeCase(str) {
|
|
457
485
|
return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
|
|
458
486
|
}
|
|
@@ -691,6 +719,10 @@ function generateZodContent({
|
|
|
691
719
|
content += `export const updateable_${snakeTable} = z.object({
|
|
692
720
|
`;
|
|
693
721
|
for (const desc of describes) {
|
|
722
|
+
const isAutoGeneratedDatetime = isAutoGeneratedDateTimeField(desc, config.origin.type);
|
|
723
|
+
if (isAutoGeneratedDatetime) {
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
694
726
|
const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
|
|
695
727
|
const fieldType = getType("updateable", desc, config, destination);
|
|
696
728
|
content += ` ${fieldName}: ${fieldType},
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mutano",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.0
|
|
4
|
+
"version": "3.1.0",
|
|
5
5
|
"description": "Converts Prisma/MySQL/PostgreSQL/SQLite schemas to Zod/TS/Kysely interfaces",
|
|
6
6
|
"author": "Alisson Cavalcante Agiani <thelinuxlich@gmail.com>",
|
|
7
7
|
"license": "MIT",
|