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

[WIP] feat: prisma enum from JS enum #48

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Daidalos117
Copy link

@Daidalos117 Daidalos117 commented Nov 7, 2022

This is proposed functionality for importing TS/JS enums into the schemix/prisma. We can do this manually now, but it would be nice if the library could support this way out of the box.
IMHO it should be preferred way to import JS enums, because it has numerous advantages.

  • using enum anywhere / using already typed enums
  • using default value for column from enum

Maybe better safety could be implemented because of this, not really sure for now. What do you think?

@ridafkih
Copy link
Owner

ridafkih commented Nov 7, 2022

I like this! Will have a closer look once I land, currently on flight. c:

@ridafkih
Copy link
Owner

ridafkih commented Nov 7, 2022

@Daidalos117 What do you think of the changes I've made? First, I ran the linter quickly and created some tests.

The real changes were that now you can pass in raw enum from TypeScript, and then if you are to assign each of those values a string it would reflect as a map on the enum in Prisma. Otherwise it would just default to the standard input.

  it("Should allow importing JS/TS enums through `fromJSEnum`", async () => {
    enum AvengersEnum {
      THOR,
      IRON_MAN,
      CAPTAIN_AMERICA,
      BLACK_WIDOW,
      HULK,
    }

    const avengersEnum = new PrismaEnum("Avengers").fromJSEnum(AvengersEnum);
    const asString = await avengersEnum.toString();
    expect(asString).toMatchSnapshot();
  });

This test produces the following output.

enum Avengers {
  THOR
  IRON_MAN
  CAPTAIN_AMERICA
  BLACK_WIDOW
  HULK
}

while the following produces what follows.

  it("Should allow mapping values through `fromJSEnum`", async () => {
    enum AvengersNameAcronymEnum {
      THOR = "TO",
      IRON_MAN = "TS",
      CAPTAIN_AMERICA = "SR",
      BLACK_WIDOW = "NR",
      HULK = "BB",
    }

    const avengersNameAcronymEnum = new PrismaEnum("Avengers").fromJSEnum(
      AvengersNameAcronymEnum
    );

    const asString = await avengersNameAcronymEnum.toString();
    expect(asString).toMatchSnapshot();
  });
enum Avengers {
  THOR             @map("TO")
  IRON_MAN         @map("TS")
  CAPTAIN_AMERICA  @map("SR")
  BLACK_WIDOW      @map("NR")
  HULK             @map("BB")
}

Let me know if you're okay with these changes, and if you see foresee any issues.

@Daidalos117
Copy link
Author

@ridafkih looks very good to me! Good idea with the mapping, I didn't think of that :)

@ridafkih
Copy link
Owner

ridafkih commented Nov 7, 2022

@Daidalos117 Awesome, this shouldn't affect any other implementations so it should be good to go! Think we're ready to get this in?

@ridafkih
Copy link
Owner

ridafkih commented Nov 7, 2022

Actually, going to write some quick tests for setting default values.

@ridafkih
Copy link
Owner

ridafkih commented Nov 8, 2022

Current status, looking into making enums on Schemix type-safe.

@dstoyanoff
Copy link
Contributor

@ridafkih, this sounds like something I would love to have. Which part is not currently type-safe?

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

Successfully merging this pull request may close these issues.

3 participants