paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
681 stars 55 forks source link

Using pal schema with uppercase snake case tables and columns #231

Closed BudWhiteStudying closed 2 years ago

BudWhiteStudying commented 2 years ago

I'm trying out Prisma+pal with a database where all tables and columns are written in uppercase snake case e.g. the AVAILABLE_PRODUCT table has a PRODUCT_ID column and so on.

model AVAILABLE_PRODUCT {
  PRODUCT_ID              Int            @id @default(autoincrement())
}

Running pal schema camel-case produces a schema.prisma file where all tables and columns are still uppercased, but with no underscore e.g.

model AVAILABLEPRODUCT {
  PRODUCTID              Int            @id @default(autoincrement())
}

I guess pal assumes that tables and columns are written in lowercase snake case, and just removes underscores and uppercases initials.

Any suggestions on how to get out of this situation? I Thought of replacing lowercase characters with uppercase before running pal schema camel-case (e.g. through a Sublime RegEx) but that would mess up types e.g. Int, which are already in the correct notation.

Thanks in advance.

BudWhiteStudying commented 2 years ago

I dug up a bit more. https://github.com/paljs/prisma-tools/blob/17d8c67f0da89c6c3303f269e4a010fc8135f072/packages/schema/src/camelCase.ts#L74 Using camelCase from lodash here instead of doing the work yourself would probably be enough

Welcome to Node.js v16.9.0.
Type ".help" for more information.
> const lodash = require('lodash')
undefined
> lodash.camelCase("MY_TABLE")
'myTable'
AhmedElywa commented 2 years ago

hey, @BudWhiteStudying, I think all that I need to do is add one word https://github.com/paljs/prisma-tools/blob/17d8c67f0da89c6c3303f269e4a010fc8135f072/packages/schema/src/camelCase.ts#L80

image

AhmedElywa commented 2 years ago

also when I am working I love to do all my work without external packages if this is small work.

BudWhiteStudying commented 2 years ago

Ah sure, I think that would work too. I typically try to avoid reinventing the wheel by relying on external packages when they're well established, but I completely understand your point.

AhmedElywa commented 2 years ago

Please try v4.0.2

BudWhiteStudying commented 2 years ago

Doesn't seem to work, although I don't understand why. I did everything from scratch with a clean schema.prisma file.

npx prisma db pull                    
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
[...]
Introspecting based on datasource defined in prisma/schema.prisma …

✔ Introspected 8 models and wrote them into prisma/schema.prisma in 2.68s

This again generates a model like

model AVAILABLE_PRODUCT {
  PRODUCT_ID              Int            @id @default(autoincrement())
}

then I ran

npx @paljs/cli@4.0.2 schema camel-case
Prisma Schema loaded from prisma/schema.prisma
✔ Your schema converted successfully

and the result is still

model AVAILABLEPRODUCT {
  PRODUCTID              Int            @id @default(autoincrement())
}
AhmedElywa commented 2 years ago

it must give you

model AvailableProduct {
  PRODUCTId              Int            @id @default(autoincrement())
}

and this is wrong and I released another version that has a fix v4.0.3

model AvailableProduct {
  productId Int @id @default(autoincrement()) @map("PRODUCT_ID")

  @@map("AVAILABLE_PRODUCT")
}
BudWhiteStudying commented 2 years ago

Now it works as intended, except for names without underscores i.e. the ID column isn't converted to id, the USER table isn't converted to User.

AhmedElywa commented 2 years ago

because we search for words have _