Skip to content

Commit

Permalink
feat: Allow overriding faker method using prisma comment
Browse files Browse the repository at this point in the history
Allow overriding faker method using prisma comment
  • Loading branch information
luisrudge authored Jan 25, 2024
2 parents 82e0d4e + 3dee0e3 commit df30449
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/__snapshots__/createMethods.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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(),
Expand Down Expand Up @@ -46,6 +47,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(),
Expand Down Expand Up @@ -119,6 +121,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(),
Expand Down Expand Up @@ -154,6 +157,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(),
Expand Down Expand Up @@ -227,6 +231,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(),
Expand Down Expand Up @@ -262,6 +267,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(),
Expand Down Expand Up @@ -335,6 +341,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(),
Expand Down Expand Up @@ -370,6 +377,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(),
Expand Down Expand Up @@ -443,6 +451,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(),
Expand Down Expand Up @@ -478,6 +487,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(),
Expand Down Expand Up @@ -551,6 +561,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(),
Expand Down Expand Up @@ -586,6 +597,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(),
Expand Down Expand Up @@ -659,6 +671,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(),
Expand Down Expand Up @@ -694,6 +707,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(),
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/sample.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 8 additions & 12 deletions src/helpers/createMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ function getFieldDefinition(
const fakeLine = docLines.find((line) => line.startsWith('FAKE:'));
const fakeValue = fakeLine?.replace('FAKE:', '');

if(fakeLine && !fakeValue) {
logger.warn(`${model.name}.${field.name} appears to have a '///FAKE:' comment but is missing a method or JSON after it.`)
}

if (fakeValue) {
return `${field.name}: ${fakeValue}`;
}

if (field.isId) {
return `${field.name}: ${
field.type === 'String' ? 'faker.string.uuid()' : 'faker.number.int()'
Expand Down Expand Up @@ -144,18 +152,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(
Expand Down

0 comments on commit df30449

Please sign in to comment.