language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

MongoDB: Quick fix for missing `@map("_id")` annotation

Open janpio opened this issue 4 years ago • 3 comments

image

janpio avatar Apr 12 '21 21:04 janpio

@matthewmueller do you think this should be prioritized for mongo GA?

tomhoule avatar Sep 23 '21 09:09 tomhoule

I could see this being a nice-to-have for sure. It seems that people like that our formatter auto-writes back-relations. I could see us extending that concept here as well. Some ideas:

Scenario 1: You're using the mongodb provider, you add @id to a String field, hit save.

Before (cursor is |):

model User {
  id String @id|
  firstName String
}

I could see this auto-writing the following:

model User {
  id String @id @default(dbgenerated()) @map("_id") @db.ObjectId|
  firstName String
}

Of course, if you edit any of this, it doesn't try to re-correct. It only adds under the conditions above, it never changes, deletes, or duplicates. I assume there's some element of playing around with it until it feels right.

Scenario 2: You're using the mongodb provider, you add @id to a Int field, hit save.

Before (cursor is |):

model User {
  id Int @id|
  firstName String
}

I could see this auto-writing the following:

model User {
  id Int @id @map("_id")|
  firstName String
}

Again, we'd need to be conservative about when we auto-write.

Added as a nice-to-have to the epic.

matthewmueller avatar Nov 16 '21 17:11 matthewmueller

Note: Hitting save does not do anything by default, you need to manually configure VSCode to run "format on save". A quick fix is a feature that is native to VSCode that does not need that configuration instead.

janpio avatar Nov 16 '21 21:11 janpio