Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding faker method using prisma comment #20

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Owner

@luisrudge luisrudge Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should remove the code related to JSON now that we support a simpler approach that is valid for all fields.. what are your thoughts on this?
https://github.com/luisrudge/prisma-generator-fake-data/pull/20/files#diff-9f2dd6068c2dc5460ec9d76b011ed01f9148e427b807ddb5b96d03f259253f97R140-R154

seems like this code will never run with this change, so we might as well remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should remove the code related to JSON now that we support a simpler approach that is valid for all fields.. what are your thoughts on this? https://github.com/luisrudge/prisma-generator-fake-data/pull/20/files#diff-9f2dd6068c2dc5460ec9d76b011ed01f9148e427b807ddb5b96d03f259253f97R140-R154

seems like this code will never run with this change, so we might as well remove it.

I left it because I saw the logger.warn() call but now I see that it only triggers if the comment is "empty" i.e. just ///FAKE:. I removed the code and added a similar check to the top and changed the wording. Also, maybe I'm doing something wrong, but I can't get logger.warn() to print anything when running prisma generate, I only see log messages when running the tests.

return `${field.name}: ${fakeValue}`;
}

if (field.isId) {
return `${field.name}: ${
field.type === 'String'
Expand Down
Loading