keonik / prisma-erd-generator

Generate an ER Diagram based on your Prisma schema every time you run npx prisma generate
https://www.npmjs.com/package/prisma-erd-generator
MIT License
894 stars 52 forks source link

Fail to generate ERD when Prisma schema contains dots in mapping #212

Closed egadin closed 1 year ago

egadin commented 1 year ago

Hi just found that the ERD doesn't support having schemas with dots in the mapping TwoOneOne @map("2.1.1") for version 1.6.2. I've tried for both mapping a ENUM as in the example and on the models and both cases fail so it seems to be for all instances where you would want to map something.

Suspect that the issue is similar to what was mentioned in issue 174. Is the generation limited to only having - and _ as special characters?

Here is a minimal reproducible schema

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

generator erd {
  provider = "prisma-erd-generator"
  output   = "../../docs/building_blocks/images/ERD.svg"
  erdDebug = true
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

enum Version {
  TwoOneOne @map("2.1.1")
  TwoTwo    @map("2.2")
}
keonik commented 1 year ago

I wrote a test to figure out what can be done. I also tried it out on mermaids live editor

Here is an example of what we can get working. Simple strings as per mermaids parser

Screenshot 2023-08-22 at 8 21 04 AM

Here is what you are trying to do and why it is failing. Periods or special characters in the name of an attribute in a class are breaking in the parser

Screenshot 2023-08-22 at 8 20 50 AM

My hopeful fix for this was the be able to just wrap all the attributes of a class in quotes and folks could put whatever characters they'd like into the models.

Screenshot 2023-08-22 at 8 20 40 AM

Unfortunately, it is not happy with using quotes over attributes. In your example above, enums or models would still cause this issue with special characters such as period. There was a need to overwrite model names because folks in Mongo commonly use underscores to prefix their models. That also made the mermaid cli unhappy. So they added regex patterns to this library to overwrite the prefix with z_Model instead of _Model.

I don't think there is a smart way to handle overwrites for special characters that will make everyone happy. Because of that, I'm not sure how to proceed. If you've got ideas let me know! Happy to collab or review a PR!