mutano 3.0.7 → 3.0.9

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 (3) hide show
  1. package/README.md +4 -4
  2. package/dist/main.js +42 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -90,16 +90,16 @@ export interface InsertableUser {
90
90
 
91
91
  **Kysely Types:**
92
92
  ```typescript
93
- export interface UserTable {
93
+ export interface User {
94
94
  id: Generated<number>;
95
95
  name: string;
96
96
  email: string;
97
97
  role: 'admin' | 'user';
98
98
  }
99
99
 
100
- export type User = Selectable<UserTable>;
101
- export type NewUser = Insertable<UserTable>;
102
- export type UserUpdate = Updateable<UserTable>;
100
+ export type SelectableUser = Selectable<User>;
101
+ export type InsertableUser = Insertable<User>;
102
+ export type UpdateableUser = Updateable<User>;
103
103
  ```
104
104
 
105
105
  ## Configuration
package/dist/main.js CHANGED
@@ -277,24 +277,36 @@ 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 (shouldBeNullable && shouldBeOptional) {
286
+ const nullableMethod = nullishOption ? "nullish" : "nullable";
287
+ return `${enumString}.${nullableMethod}()`;
288
+ } else if (shouldBeNullable) {
289
+ const nullableMethod = nullishOption ? "nullish" : "nullable";
290
+ return `${enumString}.${nullableMethod}()`;
291
+ } else if (shouldBeOptional) {
292
+ return `${enumString}.optional()`;
293
+ } else {
294
+ return enumString;
295
+ }
286
296
  } 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;
297
+ const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
298
+ if (shouldBeNullable) {
299
+ return `${enumString} | null`;
291
300
  } else {
292
- const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
293
- return shouldBeNullable ? `${enumString} | null` : enumString;
301
+ return enumString;
294
302
  }
295
303
  } else if (isKyselyDestination) {
296
304
  const enumString = enumValues.map((v) => `'${v}'`).join(" | ");
297
- return shouldBeNullable ? `${enumString} | null` : enumString;
305
+ if (shouldBeNullable) {
306
+ return `${enumString} | null`;
307
+ } else {
308
+ return enumString;
309
+ }
298
310
  }
299
311
  }
300
312
  }
@@ -309,7 +321,8 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
309
321
  const isGenerated = Extra.toLowerCase().includes("auto_increment") || Extra.toLowerCase().includes("default_generated");
310
322
  const isZodDestination = destination.type === "zod";
311
323
  const isKyselyDestination = destination.type === "kysely";
312
- const shouldBeNullable = isNull || ["insertable", "updateable"].includes(op) && (hasDefaultValue || isGenerated) || op === "updateable" && !isNull && !hasDefaultValue;
324
+ const shouldBeNullable = isNull;
325
+ const shouldBeOptional = op === "insertable" && (hasDefaultValue || isGenerated) || op === "updateable";
313
326
  let baseType;
314
327
  if (typeMappings.dateTypes.includes(type)) {
315
328
  if (isZodDestination) {
@@ -362,16 +375,27 @@ function generateStandardType(op, desc, config, destination, typeMappings) {
362
375
  } else {
363
376
  baseType = isZodDestination ? "z.string()" : "string";
364
377
  }
365
- if (shouldBeNullable) {
366
- if (isZodDestination) {
378
+ if (isZodDestination) {
379
+ if (shouldBeNullable && shouldBeOptional) {
367
380
  const nullishOption = destination.nullish;
368
381
  const nullableMethod = nullishOption ? "nullish" : "nullable";
369
382
  return `${baseType}.${nullableMethod}()`;
383
+ } else if (shouldBeNullable) {
384
+ const nullishOption = destination.nullish;
385
+ const nullableMethod = nullishOption ? "nullish" : "nullable";
386
+ return `${baseType}.${nullableMethod}()`;
387
+ } else if (shouldBeOptional) {
388
+ return `${baseType}.optional()`;
370
389
  } else {
390
+ return baseType;
391
+ }
392
+ } else {
393
+ if (shouldBeNullable) {
371
394
  return `${baseType} | null`;
395
+ } else {
396
+ return baseType;
372
397
  }
373
398
  }
374
- return baseType;
375
399
  }
376
400
 
377
401
  function generateViewContent({
@@ -544,7 +568,7 @@ function generateKyselyContent({
544
568
  `;
545
569
  content += `// This interface defines the structure of the '${table}' table
546
570
  `;
547
- content += `export interface ${pascalTable}Table {
571
+ content += `export interface ${pascalTable} {
548
572
  `;
549
573
  for (const desc of describes) {
550
574
  const fieldName = isCamelCase ? camelCase(desc.Field) : desc.Field;
@@ -560,11 +584,11 @@ function generateKyselyContent({
560
584
  content += "}\n\n";
561
585
  content += `// Use these types for inserting, selecting and updating the table
562
586
  `;
563
- content += `export type Selectable${pascalTable} = Selectable<${pascalTable}Table>;
587
+ content += `export type Selectable${pascalTable} = Selectable<${pascalTable}>;
564
588
  `;
565
- content += `export type Insertable${pascalTable} = Insertable<${pascalTable}Table>;
589
+ content += `export type Insertable${pascalTable} = Insertable<${pascalTable}>;
566
590
  `;
567
- content += `export type Updateable${pascalTable} = Updateable<${pascalTable}Table>;
591
+ content += `export type Updateable${pascalTable} = Updateable<${pascalTable}>;
568
592
  `;
569
593
  return content;
570
594
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mutano",
3
3
  "type": "module",
4
- "version": "3.0.7",
4
+ "version": "3.0.9",
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",