diff --git a/bun.lockb b/bun.lockb index afff2ee..bcbb646 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 77f0e7c..ef28862 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@prisma/client": "^5.0.0", "@prisma/generator-helper": "^5.0.0", "@prisma/internals": "^5.0.0", + "decimal.js": "^10.4.3", "tiny-invariant": "^1.3.1" }, "devDependencies": { diff --git a/src/__tests__/__snapshots__/createMethods.test.ts.snap b/src/__tests__/__snapshots__/createMethods.test.ts.snap index 3af825d..6881dad 100644 --- a/src/__tests__/__snapshots__/createMethods.test.ts.snap +++ b/src/__tests__/__snapshots__/createMethods.test.ts.snap @@ -3,6 +3,7 @@ exports[`createMethods with \`null\` as \`emptyValueAs\` 1`] = ` "import { Enum } from '@prisma/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; @@ -23,6 +24,8 @@ export function fakeUser() { nullableBigInt: null, float: faker.number.float(), nullableFloat: null, + decimal: new Decimal(faker.number.float()), + nullableDecimal: null, dateTime: faker.date.anytime(), nullableDateTime: null, stringArray: faker.lorem.words(5).split(' '), @@ -60,6 +63,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: null, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: null, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: null, @@ -103,6 +109,7 @@ export function fakeUser2RelationComplete() { exports[`createMethods with custom clientImportPath 1`] = ` "import { Enum } from './src/generated/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; @@ -123,6 +130,8 @@ export function fakeUser() { nullableBigInt: null, float: faker.number.float(), nullableFloat: null, + decimal: new Decimal(faker.number.float()), + nullableDecimal: null, dateTime: faker.date.anytime(), nullableDateTime: null, stringArray: faker.lorem.words(5).split(' '), @@ -160,6 +169,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: null, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: null, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: null, @@ -203,6 +215,7 @@ export function fakeUser2RelationComplete() { exports[`createMethods with default clientImportPath 1`] = ` "import { Enum } from '@prisma/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; @@ -223,6 +236,8 @@ export function fakeUser() { nullableBigInt: null, float: faker.number.float(), nullableFloat: null, + decimal: new Decimal(faker.number.float()), + nullableDecimal: null, dateTime: faker.date.anytime(), nullableDateTime: null, stringArray: faker.lorem.words(5).split(' '), @@ -260,6 +275,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: null, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: null, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: null, @@ -303,6 +321,7 @@ export function fakeUser2RelationComplete() { exports[`createMethods with extraExport 1`] = ` "import { Enum } from '@prisma/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; export * from '../utils/fakeImports' @@ -323,6 +342,8 @@ export function fakeUser() { nullableBigInt: undefined, float: faker.number.float(), nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + nullableDecimal: undefined, dateTime: faker.date.anytime(), nullableDateTime: undefined, stringArray: faker.lorem.words(5).split(' '), @@ -360,6 +381,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: undefined, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: undefined, @@ -403,6 +427,7 @@ export function fakeUser2RelationComplete() { exports[`createMethods with extraImport 1`] = ` "import { Enum } from '@prisma/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; import {myCustomFunction} from '../utils/fakeImports' @@ -423,6 +448,8 @@ export function fakeUser() { nullableBigInt: undefined, float: faker.number.float(), nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + nullableDecimal: undefined, dateTime: faker.date.anytime(), nullableDateTime: undefined, stringArray: faker.lorem.words(5).split(' '), @@ -460,6 +487,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: undefined, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: undefined, @@ -503,6 +533,7 @@ export function fakeUser2RelationComplete() { exports[`createMethods with extraImport and extraExport 1`] = ` "import { Enum } from '@prisma/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; import {myCustomFunction} from '../utils/fakeImports' export * from '../utils/fakeImports' @@ -523,6 +554,8 @@ export function fakeUser() { nullableBigInt: undefined, float: faker.number.float(), nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + nullableDecimal: undefined, dateTime: faker.date.anytime(), nullableDateTime: undefined, stringArray: faker.lorem.words(5).split(' '), @@ -560,6 +593,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: undefined, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: undefined, @@ -603,6 +639,7 @@ export function fakeUser2RelationComplete() { exports[`createMethods without extraImport 1`] = ` "import { Enum } from '@prisma/client'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; @@ -623,6 +660,8 @@ export function fakeUser() { nullableBigInt: undefined, float: faker.number.float(), nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + nullableDecimal: undefined, dateTime: faker.date.anytime(), nullableDateTime: undefined, stringArray: faker.lorem.words(5).split(' '), @@ -660,6 +699,9 @@ export function fakeUserComplete() { float: faker.number.float(), floatWithDefault: 1.1, nullableFloat: undefined, + decimal: new Decimal(faker.number.float()), + decimalWithDefault: new Decimal(1.23), + nullableDecimal: undefined, dateTime: faker.date.anytime(), dateTimeWithDefault: new Date(), nullableDateTime: undefined, diff --git a/src/__tests__/sample.prisma b/src/__tests__/sample.prisma index cffb1d3..942e65d 100644 --- a/src/__tests__/sample.prisma +++ b/src/__tests__/sample.prisma @@ -25,6 +25,9 @@ model User { float Float floatWithDefault Float @default(1.1) nullableFloat Float? + decimal Decimal + decimalWithDefault Decimal @default(1.23) + nullableDecimal Decimal? dateTime DateTime dateTimeWithDefault DateTime @default(now()) nullableDateTime DateTime? diff --git a/src/helpers/createMethods.ts b/src/helpers/createMethods.ts index 4b0158e..a9bac38 100644 --- a/src/helpers/createMethods.ts +++ b/src/helpers/createMethods.ts @@ -43,9 +43,12 @@ function getFieldDefinition( return `${field.name}: ${field.type}.${field.default}`; } - if (['Int', 'BigInt', 'Float', 'Decimal', 'Boolean'].includes(field.type)) { + if (['Int', 'BigInt', 'Float', 'Boolean'].includes(field.type)) { return `${field.name}: ${field.default}`; } + if (['Decimal'].includes(field.type)) { + return `${field.name}: new Decimal(${field.default})`; + } if (['String'].includes(field.type)) { return `${field.name}: '${field.default}'`; } @@ -124,9 +127,9 @@ function getFieldDefinition( } if (field.type === 'Decimal') { if (field.isList) { - return `${field.name}: [faker.datatype.hexaDecimal(),faker.datatype.hexaDecimal(),faker.datatype.hexaDecimal(),faker.datatype.hexaDecimal(),faker.datatype.hexaDecimal()]`; + return `${field.name}: [new Decimal(faker.number.float()),new Decimal(faker.number.float()),new Decimal(faker.number.float()),new Decimal(faker.number.float()),new Decimal(faker.number.float())]`; } - return `${field.name}: faker.datatype.hexaDecimal()`; + return `${field.name}: new Decimal(faker.number.float())`; } if (field.type === 'DateTime') { if (field.isList) { @@ -173,6 +176,7 @@ export async function createMethods( const enumNames = enums.map((it) => it.name).join(', '); return await `import { ${enumNames} } from '${clientImportPath}'; import { faker } from '@faker-js/faker'; +import Decimal from 'decimal.js; ${extraImport || ''} ${extraExport || ''}