language-tools
language-tools copied to clipboard
MongoDB: Quick fix for missing `@map("_id")` annotation

@matthewmueller do you think this should be prioritized for mongo GA?
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.
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.