Skip to content

Commit

Permalink
fix #787
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed Jun 5, 2020
1 parent 4ec10da commit f78e4ad
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 13 deletions.
8 changes: 8 additions & 0 deletions libs/datamodel/core/src/json/dmmf/mod.rs
Expand Up @@ -57,6 +57,14 @@ pub struct Model {
pub documentation: Option<String>,
pub id_fields: Vec<String>,
pub unique_fields: Vec<Vec<String>>,
pub unique_indexes: Vec<UniqueIndex>,
}

#[serde(rename_all = "camelCase")]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct UniqueIndex {
pub name: Option<String>,
pub fields: Vec<String>,
}

#[serde(rename_all = "camelCase")]
Expand Down
14 changes: 14 additions & 0 deletions libs/datamodel/core/src/json/dmmf/to_dmmf.rs
Expand Up @@ -72,6 +72,20 @@ fn model_to_dmmf(model: &dml::Model) -> Model {
}
})
.collect(),
unique_indexes: model
.indices
.iter()
.filter_map(|i| {
if i.tpe == IndexType::Unique {
Some(UniqueIndex {
name: i.name.clone(),
fields: i.fields.clone(),
})
} else {
None
}
})
.collect(),
}
}

Expand Down
Expand Up @@ -48,7 +48,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
}
]
}
24 changes: 17 additions & 7 deletions libs/datamodel/core/tests/render_to_dmmf/files/general.json
Expand Up @@ -114,7 +114,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
},
{
"name": "Profile",
Expand Down Expand Up @@ -184,7 +185,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
},
{
"name": "Post",
Expand Down Expand Up @@ -326,7 +328,8 @@
"title",
"createdAt"
],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
},
{
"name": "Category",
Expand Down Expand Up @@ -392,7 +395,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
},
{
"name": "PostToCategory",
Expand Down Expand Up @@ -498,7 +502,11 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [["postTitle", "categoryId"]],
"uniqueIndexes": [{
"name": "MyUniqueIndex",
"fields": ["postTitle", "categoryId"]
}]
},
{
"name": "A",
Expand Down Expand Up @@ -555,7 +563,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
},
{
"name": "B",
Expand Down Expand Up @@ -595,7 +604,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
}
]
}
Expand Up @@ -50,6 +50,7 @@ model PostToCategory {
post Post @relation(fields: [postTitle, postCreatedAt], references: [title, createdAt])
category Category @relation(fields: [categoryId], references: [id])
@@unique([postTitle, categoryId], name: "MyUniqueIndex")
@@map("post_to_category")
}

Expand Down
3 changes: 2 additions & 1 deletion libs/datamodel/core/tests/render_to_dmmf/files/source.json
Expand Up @@ -48,7 +48,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
}
]
}
Expand Up @@ -37,7 +37,8 @@
"isGenerated": false,
"documentation": "My author model.",
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
}
]
}
Expand Up @@ -35,7 +35,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
}
]
}
Expand Up @@ -39,7 +39,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
},
{
"name": "Post",
Expand Down Expand Up @@ -96,7 +97,8 @@
],
"isGenerated": false,
"idFields": [],
"uniqueFields": []
"uniqueFields": [],
"uniqueIndexes": []
}
]
}

0 comments on commit f78e4ad

Please sign in to comment.