Skip to content

Commit

Permalink
Merge pull request #21 from SSmietana/generate-decimal-as-decimal
Browse files Browse the repository at this point in the history
Generate Decimals using Decimal.js
  • Loading branch information
luisrudge authored Jan 25, 2024
2 parents d7bc72f + 5f3bae3 commit 956668d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
42 changes: 42 additions & 0 deletions src/__tests__/__snapshots__/createMethods.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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(' '),
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/sample.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
10 changes: 7 additions & 3 deletions src/helpers/createMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 || ''}
Expand Down

0 comments on commit 956668d

Please sign in to comment.