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

Mapping: stringcase conversion on introspection #2873

Open
vmolero opened this issue Jun 28, 2020 · 2 comments
Open

Mapping: stringcase conversion on introspection #2873

vmolero opened this issue Jun 28, 2020 · 2 comments

Comments

@vmolero
Copy link

vmolero commented Jun 28, 2020

Problem

Manual mapping using @Map() and @@Map()

I've found a little bit off-putting the fact of having to map manually all columns and tables for a database of 110 tables, where the database is snakecase'd and the code is camelcase'd.

I think it would be beneficial if an option to automatically map table names and columns were in place

Suggested solution

I see a possible solution by just using a library like "knex-stringcase" (https://github.com/Kequc/knex-stringcase#readme). It would be used when generating the prisma schema via introspection command.

A new option in prisma.schema could be added such as (same way as knex-stringcase does)

generator client {
  provider = "prisma-client-js"
  stringcase = ['snakecase', 'camelcase']
}

So that, a prisma model name could be generated on introspection with the corresponding @Map() clauses

Example, after running $ npx prisma introspect I get the following model definition for my explicit n:m relationship

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

i get

model APARTMENT_IMAGE {
  APARTMENT_ID         Int
  IMAGE_ID             Int
  SORT_NO              Int                  @default(0)
  APARTMENT_PROPERTIES APARTMENT_PROPERTIES @relation(fields: [APARTMENT_ID], references: [APARTMENT_ID])
  IMAGE                IMAGE                @relation(fields: [IMAGE_ID], references: [ID])

  @@id([APARTMENT_ID, IMAGE_ID])
  @@index([IMAGE_ID], name: "FK_APARTMENT_IMAGE_IMAGE")
  @@unique([APARTMENT_ID, IMAGE_ID], name: "APARTMENT_ID_IMAGE_ID")
}

However, if I ran

generator client {
  provider = "prisma-client-js"
  stringcase = ['snakecase', 'camelcase']
}

The result, automatically, would be

model apartmentImage {
  apartmentId         Int		    					    @map(APARTMENT_ID)
  imageId             Int		    					    @map(IMAGE_ID)
  sortNo              Int                  @default(0)				    @map(SORT_NO)
  apartmentProperties @relation(fields: [APARTMENT_ID], references: [APARTMENT_ID]) @map(APARTMENT_PROPERTIES)
  image               @relation(fields: [IMAGE_ID], references: [ID])		    @map(IMAGE)

  @@id([APARTMENT_ID, IMAGE_ID])
  @@index([IMAGE_ID], name: "FK_APARTMENT_IMAGE_IMAGE")
  @@unique([APARTMENT_ID, IMAGE_ID], name: "APARTMENT_ID_IMAGE_ID")
 
  @@map(APARTMENT_IMAGE)
}

In a next iteration, the ability to distinguish between model name and model attributes would be ideal since you might want to have your model name capitalized but not the attribute.

I hope my feature suggestion is clear enough,

Thanks!

@vmolero vmolero changed the title Mapping: strincase conversion on instrospection Mapping: stringcase conversion on instrospection Jun 30, 2020
@pantharshit00 pantharshit00 added the kind/feature A request for a new feature. label Jul 1, 2020
@pantharshit00 pantharshit00 changed the title Mapping: stringcase conversion on instrospection Mapping: stringcase conversion on introspection Jul 1, 2020
@albertoperdomo albertoperdomo added the team/schema Issue for team Schema. label Nov 25, 2020
@nikolasburk
Copy link
Member

This feature request seems to be included in this one: Introspection Configuration File (#1184 ) Should we close it and point people to #1184 in the future?

@nikolasburk
Copy link
Member

Also, for everyone who is interested in this, check out this tool as a workaround that can be used to automatically transform snake_case to camelCase: https://github.com/IBM/prisma-schema-transformer

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

No branches or pull requests

5 participants