From 629fed0efc0304af261ca47d5d511601c581298f Mon Sep 17 00:00:00 2001 From: Bryn Edwards Date: Tue, 2 Jan 2024 17:15:07 +0000 Subject: [PATCH 1/3] Allow overriding faker method using prisma comment --- README.md | 3 +++ .../__snapshots__/createMethods.test.ts.snap | 14 ++++++++++++++ src/__tests__/sample.prisma | 2 ++ src/helpers/createMethods.ts | 4 ++++ 4 files changed, 23 insertions(+) diff --git a/README.md b/README.md index 3ad7991..65ed28a 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ model User { id String @id @default(cuid()) email String @unique name String + /// Use comments to specify the faker method. For example: + ///FAKE:faker.location.streetAddress({ useFullAddress: true }) + address String ///FAKE:{notificationsEnabled: faker.datatype.boolean(), preferredColor: faker.color.rgb()} settings Json status UserStatus diff --git a/src/__tests__/__snapshots__/createMethods.test.ts.snap b/src/__tests__/__snapshots__/createMethods.test.ts.snap index 2f2ec0a..7ef1414 100644 --- a/src/__tests__/__snapshots__/createMethods.test.ts.snap +++ b/src/__tests__/__snapshots__/createMethods.test.ts.snap @@ -10,6 +10,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -42,6 +43,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -110,6 +112,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -142,6 +145,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -210,6 +214,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -242,6 +247,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -310,6 +316,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -342,6 +349,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -410,6 +418,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -442,6 +451,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -510,6 +520,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -542,6 +553,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -610,6 +622,7 @@ export function fakeUser() { return { email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), @@ -642,6 +655,7 @@ export function fakeUserComplete() { id: faker.string.uuid(), email: faker.internet.email(), name: faker.person.fullName(), + companyName: faker.company.name(), age: faker.number.int({min: 0, max: 99}), firstName: faker.person.firstName(), lastName: faker.person.lastName(), diff --git a/src/__tests__/sample.prisma b/src/__tests__/sample.prisma index 8b9b8ac..014fb72 100644 --- a/src/__tests__/sample.prisma +++ b/src/__tests__/sample.prisma @@ -7,6 +7,8 @@ model User { id String @id @default(cuid()) email String name String + ///FAKE:faker.company.name() + companyName String age Int firstName String lastName String diff --git a/src/helpers/createMethods.ts b/src/helpers/createMethods.ts index 5856bef..901223f 100644 --- a/src/helpers/createMethods.ts +++ b/src/helpers/createMethods.ts @@ -12,6 +12,10 @@ function getFieldDefinition( const fakeLine = docLines.find((line) => line.startsWith('FAKE:')); const fakeValue = fakeLine?.replace('FAKE:', ''); + if (fakeValue) { + return `${field.name}: ${fakeValue}`; + } + if (field.isId) { return `${field.name}: ${ field.type === 'String' From aa8e59750975bb20ad1e555551cb02f6b74a7ffa Mon Sep 17 00:00:00 2001 From: Bryn Edwards Date: Wed, 3 Jan 2024 09:40:00 +0000 Subject: [PATCH 2/3] Move check for empty `///FAKE:` comment --- src/helpers/createMethods.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/helpers/createMethods.ts b/src/helpers/createMethods.ts index 901223f..f964640 100644 --- a/src/helpers/createMethods.ts +++ b/src/helpers/createMethods.ts @@ -12,6 +12,10 @@ function getFieldDefinition( const fakeLine = docLines.find((line) => line.startsWith('FAKE:')); const fakeValue = fakeLine?.replace('FAKE:', ''); + if(fakeLine && !fakeValue) { + logger.warn(`${field.name} appears to have a '///FAKE:' comment but is missing a method or JSON after it.`) + } + if (fakeValue) { return `${field.name}: ${fakeValue}`; } @@ -138,18 +142,6 @@ function getFieldDefinition( return `${field.name}: faker.date.anytime()`; } if (field.type === 'Json') { - const docLines = field.documentation?.split('\n') || []; - const fake = docLines.find((line) => line.startsWith('FAKE:')); - if (fake) { - const fakeValue = fake.replace('FAKE:', ''); - if (!fakeValue) { - logger.warn( - `Incorrect format for fake JSON. Field ${field.name} won't be generated. Example: ///[FAKE:{"test": "faker.lorem.word()"}]`, - ); - return null; - } - return `${field.name}: ${fakeValue}`; - } return `${field.name}: JSON.stringify(${generateRandomJson()})`; } logger.warn( From 3dee0e38a72fabcd26a4870702003585cb78f560 Mon Sep 17 00:00:00 2001 From: Bryn Edwards Date: Wed, 3 Jan 2024 09:46:40 +0000 Subject: [PATCH 3/3] Include `model.name` in warning message --- src/helpers/createMethods.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/createMethods.ts b/src/helpers/createMethods.ts index f964640..ad41f2d 100644 --- a/src/helpers/createMethods.ts +++ b/src/helpers/createMethods.ts @@ -13,7 +13,7 @@ function getFieldDefinition( const fakeValue = fakeLine?.replace('FAKE:', ''); if(fakeLine && !fakeValue) { - logger.warn(`${field.name} appears to have a '///FAKE:' comment but is missing a method or JSON after it.`) + logger.warn(`${model.name}.${field.name} appears to have a '///FAKE:' comment but is missing a method or JSON after it.`) } if (fakeValue) {