diff --git a/README.md b/README.md index bee40be..c031ddb 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,9 @@ model User { | `output` | Output directory for the generated zod schemas | `string` | `./generated` | | `isGenerateSelect` | Enables the generation of Select related schemas and the select property | `boolean` | `false` | | `isGenerateInclude` | Enables the generation of Include related schemas and the include property | `boolean` | `false` | +| `useDecimalLib` | Treat Decimal values as instances of Decimal library instead of numbers | `boolean` | `false` | +| Use additional options in the `schema.prisma` ```prisma @@ -136,5 +138,6 @@ generator zod { output = "./generated-zod-schemas" isGenerateSelect = true isGenerateInclude = true + useDecimalLib = true } ``` diff --git a/package-lock.json b/package-lock.json index 0c217e7..010271f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "devDependencies": { "@types/node": "^18.11.18", "@types/prettier": "^2.7.2", + "decimal.js": "^10.4.3", "prisma": "^4.8.0", "ts-node": "^10.9.1", "typescript": "^4.9.4" @@ -820,6 +821,12 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/decimal.js": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.3.tgz", + "integrity": "sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==", + "dev": true + }, "node_modules/del": { "version": "6.1.1", "resolved": "https://registry.npmjs.org/del/-/del-6.1.1.tgz", @@ -3418,6 +3425,12 @@ } } }, + "decimal.js": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.3.tgz", + "integrity": "sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==", + "dev": true + }, "del": { "version": "6.1.1", "resolved": "https://registry.npmjs.org/del/-/del-6.1.1.tgz", diff --git a/package.json b/package.json index b5f776c..d533ec2 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "devDependencies": { "@types/node": "^18.11.18", "@types/prettier": "^2.7.2", + "decimal.js": "^10.4.3", "prisma": "^4.8.0", "ts-node": "^10.9.1", "typescript": "^4.9.4" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7bcc181..d3403c0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -12,14 +12,16 @@ generator zod { output = "./generated" isGenerateSelect = true isGenerateInclude = true + useDecimalLib = true } model User { - id Int @id @default(autoincrement()) - email String @unique - name String? - posts Post[] - books Book[] + id Int @id @default(autoincrement()) + email String @unique + name String? + posts Post[] + books Book[] + rating Decimal } model Post { diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index 876361b..ee8cf3d 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -10,6 +10,7 @@ import { changeOptionalToRequiredFields } from './whereUniqueInput-helpers'; interface AddMissingInputObjectTypeOptions { isGenerateSelect: boolean; isGenerateInclude: boolean; + useDecimalLib: boolean; } export function addMissingInputObjectTypes( @@ -53,6 +54,7 @@ export function addMissingInputObjectTypes( ); Transformer.setIsGenerateInclude(true); } + Transformer.setUseDecimalLib(options.useDecimalLib); changeOptionalToRequiredFields(inputObjectTypes); } @@ -63,5 +65,6 @@ export function resolveAddMissingInputObjectTypeOptions( return { isGenerateSelect: generatorConfigOptions.isGenerateSelect === 'true', isGenerateInclude: generatorConfigOptions.isGenerateInclude === 'true', + useDecimalLib: generatorConfigOptions.useDecimalLib === 'true', }; } diff --git a/src/transformer.ts b/src/transformer.ts index 4d8af7c..74fd7d4 100644 --- a/src/transformer.ts +++ b/src/transformer.ts @@ -32,6 +32,7 @@ export default class Transformer { private static isCustomPrismaClientOutputPath: boolean = false; private static isGenerateSelect: boolean = false; private static isGenerateInclude: boolean = false; + private static useDecimalLib: boolean = false; constructor(params: TransformerParams) { this.name = params.name ?? ''; @@ -54,6 +55,10 @@ export default class Transformer { this.isGenerateInclude = isGenerateInclude; } + static setUseDecimalLib(useDecimalLib: boolean) { + this.useDecimalLib = useDecimalLib; + } + static getOutputPath() { return this.outputPath; } @@ -133,6 +138,11 @@ export default class Transformer { let alternatives = lines.reduce((result, inputType) => { if (inputType.type === 'String') { result.push(this.wrapWithZodValidators('z.string()', field, inputType)); + } else if (Transformer.useDecimalLib && inputType.type === 'Decimal') { + result.push( + this.wrapWithZodValidators('z.instanceof(Decimal)', field, inputType), + ); + this.addSchemaImport('Decimal'); } else if ( inputType.type === 'Int' || inputType.type === 'Float' || @@ -372,6 +382,8 @@ export default class Transformer { )} } from '../${queryName}${modelName}.schema'`; } else if (Transformer.enumNames.includes(name)) { return `import { ${name}Schema } from '../enums/${name}.schema'`; + } else if (name === 'Decimal') { + return "import { Decimal } from 'decimal.js'"; } else { return `import { ${name}ObjectSchema } from './${name}.schema'`; }