Skip to content

Commit

Permalink
Allow overriding faker method using prisma comment
Browse files Browse the repository at this point in the history
  • Loading branch information
brynedwards committed Jan 2, 2024
1 parent 9de7b5b commit 629fed0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 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 @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
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
4 changes: 4 additions & 0 deletions src/helpers/createMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 629fed0

Please sign in to comment.