mutano 3.0.8 → 3.0.10

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.
Files changed (2) hide show
  1. package/dist/main.js +81 -17
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -277,24 +277,44 @@ function getType(op, desc, config, destination) {
277
277
  enumValues = config.enumDeclarations[type];
278
278
  }
279
279
  if (enumValues.length > 0) {
280
- const shouldBeNullable = isNull || ["insertable", "updateable"].includes(op) && (hasDefaultValue || isGenerated) || op === "updateable" && !isNull && !hasDefaultValue;
280
+ const shouldBeNullable = isNull;
281
+ const shouldBeOptional = op === "insertable" && (hasDefaultValue || isGenerated) || op === "updateable";
281
282
  if (isZodDestination) {
282
283
  const enumString = `z.enum([${enumValues.map((v) => `'${v}'`).join(",")}])`;
283
284
  const nullishOption = destination.nullish;
284
- const nullableMethod = nullishOption ? "nullish" : "nullable";
285
- return shouldBeNullable ? `${enumString}.${nullableMethod}()` : enumString;
285
+ if (op === "insertable" && 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
+ }
293
+ if (shouldBeNullable && shouldBeOptional) {
294
+ const nullableMethod = nullishOption ? "nullish" : "nullable";
295
+ return `${enumString}.${nullableMethod}()`;
296
+ } else if (shouldBeNullable) {
297
+ const nullableMethod = nullishOption ? "nullish" : "nullable";
298
+ return `${enumString}.${nullableMethod}()`;
299
+ } else if (shouldBeOptional) {
300
+ return `${enumString}.optional()`;
301
+ } else {
302
+ return enumString;
303
+ }
286
304
  } else if (isTsDestination) {
287
- const enumType = destination.enumType;
288
- if (enumType === "enum") {
289
- const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
290
- return shouldBeNullable ? `${enumString} | null` : enumString;
305
+ const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
306
+ if (shouldBeNullable) {
307
+ return `${enumString} | null`;
291
308
  } else {
292
- const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
293
- return shouldBeNullable ? `${enumString} | null` : enumString;
309
+ return enumString;
294
310
  }
295
311
  } else if (isKyselyDestination) {
296
312
  const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
297
- return shouldBeNullable ? `${enumString} | null` : enumString;
313
+ if (shouldBeNullable) {
314
+ return `${enumString} | null`;
315
+ } else {
316
+ return enumString;
317
+ }
298
318
  }
299
319
  }
300
320
  }
@@ -309,7 +329,8 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
309
329
  const isGenerated = Extra.toLowerCase().includes("auto_increment") || Extra.toLowerCase().includes("default_generated");
310
330
  const isZodDestination = destination.type === "zod";
311
331
  const isKyselyDestination = destination.type === "kysely";
312
- const shouldBeNullable = isNull || ["insertable", "updateable"].includes(op) && (hasDefaultValue || isGenerated) || op === "updateable" && !isNull && !hasDefaultValue;
332
+ const shouldBeNullable = isNull;
333
+ const shouldBeOptional = op === "insertable" && (hasDefaultValue || isGenerated) || op === "updateable";
313
334
  let baseType;
314
335
  if (typeMappings.dateTypes.includes(type)) {
315
336
  if (isZodDestination) {
@@ -362,16 +383,46 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
362
383
  } else {
363
384
  baseType = isZodDestination ? "z.string()" : "string";
364
385
  }
365
- if (shouldBeNullable) {
366
- if (isZodDestination) {
386
+ if (isZodDestination) {
387
+ if (op === "insertable" && 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
+ }
406
+ if (shouldBeNullable && shouldBeOptional) {
367
407
  const nullishOption = destination.nullish;
368
408
  const nullableMethod = nullishOption ? "nullish" : "nullable";
369
409
  return `${baseType}.${nullableMethod}()`;
410
+ } else if (shouldBeNullable) {
411
+ const nullishOption = destination.nullish;
412
+ const nullableMethod = nullishOption ? "nullish" : "nullable";
413
+ return `${baseType}.${nullableMethod}()`;
414
+ } else if (shouldBeOptional) {
415
+ return `${baseType}.optional()`;
370
416
  } else {
417
+ return baseType;
418
+ }
419
+ } else {
420
+ if (shouldBeNullable) {
371
421
  return `${baseType} | null`;
422
+ } else {
423
+ return baseType;
372
424
  }
373
425
  }
374
- return baseType;
375
426
  }
376
427
 
377
428
  function generateViewContent({
@@ -830,15 +881,28 @@ function extractPrismaColumnDescriptions(config, entityName, enumDeclarations) {
830
881
  let defaultValue = null;
831
882
  if (field.attributes) {
832
883
  for (const attr of field.attributes) {
833
- if (attr.name === "default") {
884
+ if (attr.name === "updatedAt") {
885
+ defaultGenerated = true;
886
+ } else if (attr.name === "default") {
834
887
  if (attr.args && attr.args.length > 0) {
835
888
  const arg = attr.args[0];
836
889
  if (typeof arg === "object" && "value" in arg) {
837
- if (arg.value === "autoincrement()" || arg.value === "cuid()" || arg.value === "uuid()") {
838
- defaultGenerated = true;
890
+ if (typeof arg.value === "object" && arg.value.type === "function") {
891
+ const functionName = arg.value.name;
892
+ if (functionName === "autoincrement" || functionName === "cuid" || functionName === "uuid" || functionName === "now") {
893
+ defaultGenerated = true;
894
+ }
895
+ } else if (typeof arg.value === "string") {
896
+ let cleanValue = arg.value;
897
+ if (cleanValue.startsWith('"') && cleanValue.endsWith('"')) {
898
+ cleanValue = cleanValue.slice(1, -1);
899
+ }
900
+ defaultValue = cleanValue;
839
901
  } else {
840
902
  defaultValue = String(arg.value);
841
903
  }
904
+ } else if (typeof arg === "string") {
905
+ defaultValue = arg;
842
906
  }
843
907
  }
844
908
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mutano",
3
3
  "type": "module",
4
- "version": "3.0.8",
4
+ "version": "3.0.10",
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",