mutano 3.0.9 → 3.0.11
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/main.js +58 -13
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -282,6 +282,14 @@ function getType(op, desc, config, destination) {
|
|
|
282
282
|
if (isZodDestination) {
|
|
283
283
|
const enumString = `z.enum([${enumValues.map((v) => `'${v}'`).join(",")}])`;
|
|
284
284
|
const nullishOption = destination.nullish;
|
|
285
|
+
if ((op === "table" || op === "insertable" || op === "selectable") && hasDefaultValue && Default !== null && !isGenerated) {
|
|
286
|
+
if (shouldBeNullable) {
|
|
287
|
+
const nullableMethod = nullishOption ? "nullish" : "nullable";
|
|
288
|
+
return `${enumString}.${nullableMethod}().default('${Default}')`;
|
|
289
|
+
} else {
|
|
290
|
+
return `${enumString}.default('${Default}')`;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
285
293
|
if (shouldBeNullable && shouldBeOptional) {
|
|
286
294
|
const nullableMethod = nullishOption ? "nullish" : "nullable";
|
|
287
295
|
return `${enumString}.${nullableMethod}()`;
|
|
@@ -376,6 +384,25 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
|
|
|
376
384
|
baseType = isZodDestination ? "z.string()" : "string";
|
|
377
385
|
}
|
|
378
386
|
if (isZodDestination) {
|
|
387
|
+
if ((op === "table" || op === "insertable" || op === "selectable") && hasDefaultValue && Default !== null && !isGenerated) {
|
|
388
|
+
let defaultValueFormatted = Default;
|
|
389
|
+
if (typeMappings.stringTypes.includes(type) || typeMappings.dateTypes.includes(type)) {
|
|
390
|
+
defaultValueFormatted = `'${Default}'`;
|
|
391
|
+
} else if (typeMappings.booleanTypes.includes(type)) {
|
|
392
|
+
defaultValueFormatted = Default.toLowerCase() === "true" ? "true" : "false";
|
|
393
|
+
} else if (typeMappings.numberTypes.includes(type)) {
|
|
394
|
+
defaultValueFormatted = Default;
|
|
395
|
+
} else {
|
|
396
|
+
defaultValueFormatted = `'${Default}'`;
|
|
397
|
+
}
|
|
398
|
+
if (shouldBeNullable) {
|
|
399
|
+
const nullishOption = destination.nullish;
|
|
400
|
+
const nullableMethod = nullishOption ? "nullish" : "nullable";
|
|
401
|
+
return `${baseType}.${nullableMethod}().default(${defaultValueFormatted})`;
|
|
402
|
+
} else {
|
|
403
|
+
return `${baseType}.default(${defaultValueFormatted})`;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
379
406
|
if (shouldBeNullable && shouldBeOptional) {
|
|
380
407
|
const nullishOption = destination.nullish;
|
|
381
408
|
const nullableMethod = nullishOption ? "nullish" : "nullable";
|
|
@@ -398,6 +425,9 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
|
|
|
398
425
|
}
|
|
399
426
|
}
|
|
400
427
|
|
|
428
|
+
function toSnakeCase(str) {
|
|
429
|
+
return str.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
|
|
430
|
+
}
|
|
401
431
|
function generateViewContent({
|
|
402
432
|
view,
|
|
403
433
|
describes,
|
|
@@ -449,7 +479,8 @@ function generateViewContent({
|
|
|
449
479
|
}
|
|
450
480
|
content += `// View schema (read-only)
|
|
451
481
|
`;
|
|
452
|
-
|
|
482
|
+
const snakeView = toSnakeCase(view);
|
|
483
|
+
content += `export const ${snakeView}_view = z.object({
|
|
453
484
|
`;
|
|
454
485
|
for (const desc of describes) {
|
|
455
486
|
const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
|
|
@@ -461,7 +492,7 @@ function generateViewContent({
|
|
|
461
492
|
const pascalView = camelCase(view, { pascalCase: true });
|
|
462
493
|
content += `export type ${camelCase(`${pascalView}ViewType`, {
|
|
463
494
|
pascalCase: true
|
|
464
|
-
})} = z.infer<typeof ${
|
|
495
|
+
})} = z.infer<typeof ${snakeView}_view>
|
|
465
496
|
`;
|
|
466
497
|
}
|
|
467
498
|
return content;
|
|
@@ -606,7 +637,8 @@ function generateZodContent({
|
|
|
606
637
|
if (!content.includes(header)) {
|
|
607
638
|
content += header;
|
|
608
639
|
}
|
|
609
|
-
|
|
640
|
+
const snakeTable = toSnakeCase(table);
|
|
641
|
+
content += `export const ${snakeTable} = z.object({
|
|
610
642
|
`;
|
|
611
643
|
for (const desc of describes) {
|
|
612
644
|
const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
|
|
@@ -615,7 +647,7 @@ function generateZodContent({
|
|
|
615
647
|
`;
|
|
616
648
|
}
|
|
617
649
|
content += "})\n\n";
|
|
618
|
-
content += `export const insertable_${
|
|
650
|
+
content += `export const insertable_${snakeTable} = z.object({
|
|
619
651
|
`;
|
|
620
652
|
for (const desc of describes) {
|
|
621
653
|
const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
|
|
@@ -624,7 +656,7 @@ function generateZodContent({
|
|
|
624
656
|
`;
|
|
625
657
|
}
|
|
626
658
|
content += "})\n\n";
|
|
627
|
-
content += `export const updateable_${
|
|
659
|
+
content += `export const updateable_${snakeTable} = z.object({
|
|
628
660
|
`;
|
|
629
661
|
for (const desc of describes) {
|
|
630
662
|
const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
|
|
@@ -633,7 +665,7 @@ function generateZodContent({
|
|
|
633
665
|
`;
|
|
634
666
|
}
|
|
635
667
|
content += "})\n\n";
|
|
636
|
-
content += `export const selectable_${
|
|
668
|
+
content += `export const selectable_${snakeTable} = z.object({
|
|
637
669
|
`;
|
|
638
670
|
for (const desc of describes) {
|
|
639
671
|
const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
|
|
@@ -642,13 +674,13 @@ function generateZodContent({
|
|
|
642
674
|
`;
|
|
643
675
|
}
|
|
644
676
|
content += "})\n\n";
|
|
645
|
-
content += `export type ${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof ${
|
|
677
|
+
content += `export type ${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof ${snakeTable}>
|
|
646
678
|
`;
|
|
647
|
-
content += `export type Insertable${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof insertable_${
|
|
679
|
+
content += `export type Insertable${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof insertable_${snakeTable}>
|
|
648
680
|
`;
|
|
649
|
-
content += `export type Updateable${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof updateable_${
|
|
681
|
+
content += `export type Updateable${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof updateable_${snakeTable}>
|
|
650
682
|
`;
|
|
651
|
-
content += `export type Selectable${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof selectable_${
|
|
683
|
+
content += `export type Selectable${camelCase(`${table}Type`, { pascalCase: true })} = z.infer<typeof selectable_${snakeTable}>
|
|
652
684
|
`;
|
|
653
685
|
return content;
|
|
654
686
|
}
|
|
@@ -854,15 +886,28 @@ function extractPrismaColumnDescriptions(config, entityName, enumDeclarations) {
|
|
|
854
886
|
let defaultValue = null;
|
|
855
887
|
if (field.attributes) {
|
|
856
888
|
for (const attr of field.attributes) {
|
|
857
|
-
if (attr.name === "
|
|
889
|
+
if (attr.name === "updatedAt") {
|
|
890
|
+
defaultGenerated = true;
|
|
891
|
+
} else if (attr.name === "default") {
|
|
858
892
|
if (attr.args && attr.args.length > 0) {
|
|
859
893
|
const arg = attr.args[0];
|
|
860
894
|
if (typeof arg === "object" && "value" in arg) {
|
|
861
|
-
if (arg.value === "
|
|
862
|
-
|
|
895
|
+
if (typeof arg.value === "object" && arg.value.type === "function") {
|
|
896
|
+
const functionName = arg.value.name;
|
|
897
|
+
if (functionName === "autoincrement" || functionName === "cuid" || functionName === "uuid" || functionName === "now") {
|
|
898
|
+
defaultGenerated = true;
|
|
899
|
+
}
|
|
900
|
+
} else if (typeof arg.value === "string") {
|
|
901
|
+
let cleanValue = arg.value;
|
|
902
|
+
if (cleanValue.startsWith('"') && cleanValue.endsWith('"')) {
|
|
903
|
+
cleanValue = cleanValue.slice(1, -1);
|
|
904
|
+
}
|
|
905
|
+
defaultValue = cleanValue;
|
|
863
906
|
} else {
|
|
864
907
|
defaultValue = String(arg.value);
|
|
865
908
|
}
|
|
909
|
+
} else if (typeof arg === "string") {
|
|
910
|
+
defaultValue = arg;
|
|
866
911
|
}
|
|
867
912
|
}
|
|
868
913
|
}
|
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.0.11",
|
|
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",
|