prisma-case-format icon indicating copy to clipboard operation
prisma-case-format copied to clipboard

MongoDB ID field issue

Open daeteck opened this issue 11 months ago • 4 comments

Hi,

I found this same issue in the library https://github.com/loop-payments/prisma-lint and that's why I tried this one for case linting of tables and field for a model of MongoDB in Prisma.

The issue is the following:

In MongoDB the id field is not Snake or Pascal case. The id is named by convention _id.

Example in Prisma Documentation:

https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/mongodb/creating-the-prisma-schema-typescript-mongodb

model Post {
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  slug     String    @unique
  title    String
  body     String
  author   User      @relation(fields: [authorId], references: [id])
  authorId String    @db.ObjectId
  comments Comment[]
}

So the issue is that the prisma-case-format is removing the @map("_id") from the Prisma schema, when the idea is to keep that special mapping from id to _id.

This is a general rule and not something that should be added as an override table by table.

Thanks!

daeteck avatar Mar 05 '24 13:03 daeteck

So essentially, if provider = "mongodb" then id shall map to _id

iiian avatar Mar 05 '24 19:03 iiian

So essentially, if provider = "mongodb" then id shall map to _id

Yes, that's correct.

Give the option to omit some specific field without link that field to a table should be the way to go. Something like:

default: ...
override:
  mYTaBlE: 'disable' # skip convention management for this table
  ...
  YourTable:
    default: '...'
    field:
      unmanaged_property: 'disable' # skip convention management for this field
override-field:
  id: 'disable'

Or if that is too hard perhaps another solution is to add the option of adding a comment to disable from the process the next line after a special comment, like the linting tools have. Something like:

model Post {
  // prisma-case-format-disable-next-line
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  slug     String    @unique
  title    String
  body     String
  author   User      @relation(fields: [authorId], references: [id])
  authorId String    @db.ObjectId
  comments Comment[]
}

daeteck avatar Mar 05 '24 19:03 daeteck

I mean this sounds like it's a fundamentalism of using mongodb, so I'd think we shouldn't make this configurable, it should be embedded. Probably the way I'd build it under the hood is to implement common field overrides, and then when it's detected that "mongodb" is the provider, create an ad hoc common field override for @id.

iiian avatar Mar 05 '24 20:03 iiian

Yes, I totally agree with you. That's an even better solution!

daeteck avatar Mar 05 '24 20:03 daeteck

@daeteck have you solved it somehow?

gal-terra avatar Oct 09 '24 08:10 gal-terra