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

Not working on unsupported types (sqlite) #17

Open
jsbrain opened this issue Nov 17, 2023 · 3 comments
Open

Not working on unsupported types (sqlite) #17

jsbrain opened this issue Nov 17, 2023 · 3 comments

Comments

@jsbrain
Copy link

jsbrain commented Nov 17, 2023

In case of sqlite the generator won't create faker objects despite the annotations because it's listed as string.
But there are extensions for sqlite that would allow this also there could be manual serialization.

Would be great if that's supported.

model users {
  id    String @id @default(cuid())
  email String @unique
  name  String

  ///FAKE:fakeSettings()
  settings String? @default("{}") /// <- This doesn't work

  ///FAKE:{notificationsEnabled: faker.datatype.boolean(), preferredColor: faker.color.rgb()}
  ui_settings Unsupported("json") /// <- This doesn't work either

  profile profiles?
}
@luisrudge
Copy link
Owner

Interesting. Thanks for the issue. Not sure what Prisma will report in the DMMF.Field object for fields like this. Can you write a failing test for it?

  • Create a new sqlite.prisma file in src/__tests__
  • Create a new method to retrieve the sqlite DMMF
  • Create a new test in createMethods.test.ts that uses the sqlite DMMF to do any assertions. Feel free to add only your specific use case instead of adding all other types

If you find a fix for it, even better 😅 Otherwise I'll investigate with the help of the failing test.

@jsbrain
Copy link
Author

jsbrain commented Nov 20, 2023

I'm very limited on time right now but I think changing this:

if (['String'].includes(field.type)) {
  return `${field.name}: '${field.default}'`;
}

to this:

if (['String'].includes(field.type)) {
  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}: '${field.default}'`;
}

should do it.

@jsbrain
Copy link
Author

jsbrain commented Nov 20, 2023

Basically keeps the current implementation unless there is a FAKE: comment. Should be fine I guess?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants