notiz-dev / prisma-dbml-generator

Prisma DBML Generator
MIT License
685 stars 37 forks source link

should use mapped column and table names #18

Open keenahn opened 3 years ago

keenahn commented 3 years ago

since this is supposed to be the actual database diagram, shouldn't the generated dbml use the mapped table and column names from @map and @@map ?

marcjulian commented 3 years ago

@keenahn good idea. I think I prefer to haven an option to use the names either from the models/fields or the names for @map and @@map. What do you think?

Katli95 commented 2 years ago

Yeah having the option at least would be cool! I think I'd prefer to export the actual Database Schema for the diagram, even though the Prisma client surfaces another API. Another thing to look at would be to include the db type attributes such as @db.VarChar(5000).

But overall, awesome little utility, I'm starting to integrate it into my workflow!

marcjulian commented 2 years ago

For the following schema with @map, @@map and @db., only @@map is surfaced in the DMMF schema from Prisma. Thus, for now only the dbName can be mapped to the value specified in @@map.

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) @db.DateTime(0)
  updatedAt DateTime @updatedAt @map("updated_at")
  email     String   @unique
  name      String?  @db.VarChar(255)

  @@map("user")
}
DMMF ```json { "enums": [], "models": [ { "name": "User", "dbName": "user", // 👈 @@map "fields": [ { "name": "id", "kind": "scalar", "isList": false, "isRequired": true, "isUnique": false, "isId": true, "isReadOnly": false, "type": "Int", "hasDefaultValue": true, "default": { "name": "autoincrement", "args": [] }, "isGenerated": false, "isUpdatedAt": false }, { "name": "createdAt", "kind": "scalar", "isList": false, "isRequired": true, "isUnique": false, "isId": false, "isReadOnly": false, "type": "DateTime", "hasDefaultValue": true, "default": { "name": "now", "args": [] }, "isGenerated": false, "isUpdatedAt": false }, { "name": "updatedAt", "kind": "scalar", "isList": false, "isRequired": true, "isUnique": false, "isId": false, "isReadOnly": false, "type": "DateTime", "hasDefaultValue": false, "isGenerated": false, "isUpdatedAt": true }, { "name": "email", "kind": "scalar", "isList": false, "isRequired": true, "isUnique": true, "isId": false, "isReadOnly": false, "type": "String", "hasDefaultValue": false, "isGenerated": false, "isUpdatedAt": false }, { "name": "name", "kind": "scalar", "isList": false, "isRequired": false, "isUnique": false, "isId": false, "isReadOnly": false, "type": "String", "hasDefaultValue": false, "isGenerated": false, "isUpdatedAt": false } ], "isGenerated": false, "primaryKey": null, "uniqueFields": [], "uniqueIndexes": [] } ], "types": [] } ```
marcjulian commented 2 years ago

I added mapToDbSchema option for you to test out. You can test it with the latest dev release 0.9.0-dev.1

npm i -D prisma-dbml-generator@0.9.0-dev.1
generator dbml {
  provider    = "node ./dist/generator.js"
  mapToDbSchema = "true" // 👈  enable mapToDbSchema with the new option
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) 
  updatedAt DateTime @updatedAt
  email     String   @unique
  name      String? 

  @@map("user") // 👈  this will be used in the dbml schema
}
dominikjasek commented 2 years ago

Could anybody confirm that release 0.9.0 fixed this? It is still not working for me personally.

marcjulian commented 2 years ago

@dominikjasek Did you add mapToDbSchema to the dbml generator as this is not enabled by default? Otherwise do you have a prisma.schema to reproduce?

dominikjasek commented 2 years ago

It works on table names but not on relations, when using @@map('users') on User Model etc. Expected: Ref: answers.userId > users.id Got: Ref: Answer.userId > User.id

marcjulian commented 2 years ago

Ah good point. I might have missed that! I'll look into it soon

marcjulian commented 2 years ago

@dominikjasek Please update to 0.9.1 and test it again.

dominikjasek commented 2 years ago

Thanks! I confirm it is working.

hotrungnhan commented 2 years ago

hallo guy i have field name wasn't mapped right to the dbml. prisma-dbml-generator: 0.9.1 prisma: 4.0.1

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

generator dbml {
  provider              = "node node_modules/prisma-dbml-generator/dist/generator.js"
  mapToDbSchema         = true
  includeRelationFields = false
  projectName           = "Backend DB"
}

generator docs {
  provider = "node node_modules/prisma-docs-generator"
}

generator nestgraphql {
  provider = "node node_modules/prisma-nestjs-graphql"
  output   = "./@generated/graphql"
}

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

model User {
  id          Int    @id @default(autoincrement())
  email       String @unique
  displayName String @map("display_name")
  password    String

  @@map("user")
}

model Movies {
  id          Int          @id @default(autoincrement())
  title       String?
  code        String?
  description Json?
  thumbnail   String
  seo         Seo?         @relation(fields: [seoId], references: [id])
  seoId       Int?         @map("seo_id")
  movieVideo  MovieVideo[]
  video       Video[]

  @@map("movies")
}

model MovieVideo {
  movieId Int    @map("movie_id")
  videoId Int    @map("video_id")
  movie   Movies @relation(fields: [movieId], references: [id])
  video   Video  @relation(fields: [videoId], references: [id])

  @@id([movieId, videoId])
  @@map("movie_video")
}

model Seo {
  id          Int      @id @default(autoincrement())
  title       String?
  description String?
  keywords    String[]
  slug        String   @default(dbgenerated("'<(10,4),11>'::circle"))
  thumbnail   String
  movies      Movies[]

  @@map("seo")
}

model VideoProvider {
  id       Int     @id @default(autoincrement())
  provider String
  video    Video[]

  @@map("video_provider")
}

model Video {
  id         Int           @id @default(autoincrement())
  url        String
  providerId Int           @map("provider_id")
  provider   VideoProvider @relation(fields: [providerId], references: [id])
  movieVideo MovieVideo[]
  movies     Movies?       @relation(fields: [moviesId], references: [id])
  moviesId   Int?          @map("movies_id")

  @@map("video")
}

model VideoCategory {
  id   Int     @id @default(autoincrement())
  name String?

  @@map("video_category")
}

model Actor {
  id     Int      @id @default(autoincrement())
  name   String?
  images String[]

  @@map("actor")
}
erlanggadewa commented 10 months ago

For the following schema with @map, @@map and @db., only @@map is surfaced in the DMMF schema from Prisma. Thus, for now only the dbName can be mapped to the value specified in @@map.

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now()) @db.DateTime(0)
  updatedAt DateTime @updatedAt @map("updated_at")
  email     String   @unique
  name      String?  @db.VarChar(255)

  @@map("user")
}

DMMF

{
  "enums": [],
  "models": [
    {
      "name": "User",
      "dbName": "user", // 👈 @@map
      "fields": [
        {
          "name": "id",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": true,
          "isReadOnly": false,
          "type": "Int",
          "hasDefaultValue": true,
          "default": { "name": "autoincrement", "args": [] },
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "createdAt",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "DateTime",
          "hasDefaultValue": true,
          "default": { "name": "now", "args": [] },
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "updatedAt",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "DateTime",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": true
        },
        {
          "name": "email",
          "kind": "scalar",
          "isList": false,
          "isRequired": true,
          "isUnique": true,
          "isId": false,
          "isReadOnly": false,
          "type": "String",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": false
        },
        {
          "name": "name",
          "kind": "scalar",
          "isList": false,
          "isRequired": false,
          "isUnique": false,
          "isId": false,
          "isReadOnly": false,
          "type": "String",
          "hasDefaultValue": false,
          "isGenerated": false,
          "isUpdatedAt": false
        }
      ],
      "isGenerated": false,
      "primaryKey": null,
      "uniqueFields": [],
      "uniqueIndexes": []
    }
  ],
  "types": []
}

can you create a new option to make name field to snake case ? Because a lot of user using snake case in @map notation
exampe -> updatedAt become updated_at ?