Skip to content

Commit

Permalink
Added
Browse files Browse the repository at this point in the history
- quickfix for mongodb native type with `@default(auto)`

closes prisma/language-tools#1548
  • Loading branch information
Druue committed Jan 4, 2024
1 parent babfd59 commit 8ef74fb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions prisma-fmt/src/code_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec

if matches!(datasource, Some(ds) if ds.active_provider == "mongodb") {
mongodb::add_at_map_for_id(&mut actions, &params, validated_schema.db.source(), model);

mongodb::add_native_for_auto_id(&mut actions, &params, validated_schema.db.source(), model);
}
}

Expand Down
49 changes: 49 additions & 0 deletions prisma-fmt/src/code_actions/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,52 @@ pub(super) fn add_at_map_for_id(

actions.push(CodeActionOrCommand::CodeAction(action))
}

pub(super) fn add_native_for_auto_id(
actions: &mut Vec<CodeActionOrCommand>,
params: &lsp_types::CodeActionParams,
schema: &str,
model: ModelWalker<'_>,
) {
let pk = match model.primary_key() {
Some(pk) => pk,
None => return,
};

if pk.fields().len() < 1 {
return;
}

let field = match pk.fields().next() {
Some(field) => field,
None => return,
};

let span_diagnostics =
match super::diagnostics_for_span(schema, &params.context.diagnostics, model.ast_model().span()) {
Some(sd) => sd,
None => return,
};

let diagnostics = match super::filter_diagnostics(
span_diagnostics,
r#"MongoDB `@default(auto())` fields must have `ObjectId` native type."#,
) {
Some(value) => value,
None => return,
};

let formatted_attribute = super::format_field_attribute(r#"@db.ObjectId"#);

let edit = super::create_text_edit(schema, formatted_attribute, true, field.ast_field().span(), params);

let action = CodeAction {
title: r#"Add @db.ObjectId"#.to_owned(),
kind: Some(CodeActionKind::QUICKFIX),
edit: Some(edit),
diagnostics: Some(diagnostics),
..Default::default()
};

actions.push(CodeActionOrCommand::CodeAction(action))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"title": "Add @db.ObjectId",
"kind": "quickfix",
"diagnostics": [
{
"range": {
"start": {
"line": 10,
"character": 4
},
"end": {
"line": 11,
"character": 0
}
},
"severity": 1,
"message": "Error validating field `id` in model `Kattbjorn`: MongoDB `@default(auto())` fields must have `ObjectId` native type."
}
],
"edit": {
"changes": {
"file:///path/to/schema.prisma": [
{
"range": {
"start": {
"line": 10,
"character": 46
},
"end": {
"line": 11,
"character": 0
}
},
"newText": " @db.ObjectId\n"
}
]
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model Kattbjorn {
id String @id @default(auto()) @map("_id")
}
1 change: 1 addition & 0 deletions prisma-fmt/tests/code_actions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ scenarios! {
multi_schema_add_to_nonexisting_schemas
mongodb_at_map
mongodb_at_map_with_validation_errors
mongodb_auto_native
}

0 comments on commit 8ef74fb

Please sign in to comment.