prisma_rust_schema/
dmmf.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Debug, Clone, Deserialize, Serialize)]
5pub struct Document {
6    pub datamodel: Datamodel,
7    pub schema: Schema,
8    pub mappings: Mappings,
9}
10
11#[derive(Debug, Clone, Deserialize, Serialize)]
12pub struct Mappings {
13    #[serde(rename = "modelOperations")]
14    pub model_operations: Vec<ModelMapping>,
15    #[serde(rename = "otherOperations")]
16    pub other_operations: OtherOperationMappings,
17}
18
19#[derive(Debug, Clone, Deserialize, Serialize)]
20pub struct OtherOperationMappings {
21    pub read: Vec<String>,
22    pub write: Vec<String>,
23}
24
25#[derive(Debug, Clone, Deserialize, Serialize)]
26pub struct DatamodelEnum {
27    pub name: String,
28    pub values: Vec<EnumValue>,
29    #[serde(rename = "dbName")]
30    pub db_name: Option<String>,
31    pub documentation: Option<String>,
32}
33
34#[derive(Debug, Clone, Deserialize, Serialize)]
35pub struct SchemaEnum {
36    pub name: String,
37    pub values: Vec<String>,
38}
39
40#[derive(Debug, Clone, Deserialize, Serialize)]
41pub struct EnumValue {
42    pub name: String,
43    #[serde(rename = "dbName")]
44    pub db_name: Option<String>,
45    pub documentation: Option<String>,
46}
47
48#[derive(Debug, Clone, Deserialize, Serialize)]
49pub struct Datamodel {
50    pub models: Vec<Model>,
51    pub enums: Vec<DatamodelEnum>,
52    #[serde(rename = "types")]
53    pub type_models: Vec<Model>,
54    pub indexes: Vec<Index>,
55}
56
57#[derive(Debug, Clone, Deserialize, Serialize)]
58pub struct UniqueIndex {
59    pub name: String,
60    pub fields: Vec<String>,
61}
62
63#[derive(Debug, Clone, Deserialize, Serialize)]
64pub struct PrimaryKey {
65    pub name: Option<String>,
66    pub fields: Vec<String>,
67}
68
69#[derive(Debug, Clone, Deserialize, Serialize)]
70#[serde(rename_all = "camelCase")]
71pub struct Model {
72    pub name: String,
73    pub db_name: Option<String>,
74    pub schema: Option<String>,
75    pub fields: Vec<Field>,
76    pub unique_fields: Vec<Vec<String>>,
77    pub unique_indexes: Vec<UniqueIndex>,
78    pub documentation: Option<String>,
79    pub primary_key: Option<PrimaryKey>,
80    pub is_generated: Option<bool>,
81}
82
83#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
84#[serde(rename_all = "lowercase")]
85pub enum FieldKind {
86    Scalar,
87    Object,
88    Enum,
89    Unsupported,
90}
91
92#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
93#[serde(rename_all = "lowercase")]
94pub enum FieldNamespace {
95    Model,
96    Prisma,
97}
98
99#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
100#[serde(rename_all = "camelCase")]
101pub enum FieldLocation {
102    Scalar,
103    InputObjectTypes,
104    OutputObjectTypes,
105    EnumTypes,
106    FieldRefTypes,
107}
108
109impl AsRef<str> for FieldLocation {
110    fn as_ref(&self) -> &str {
111        match self {
112            FieldLocation::Scalar => "scalar",
113            FieldLocation::InputObjectTypes => "inputObjectTypes",
114            FieldLocation::OutputObjectTypes => "outputObjectTypes",
115            FieldLocation::EnumTypes => "enumTypes",
116            FieldLocation::FieldRefTypes => "fieldRefTypes",
117        }
118    }
119}
120
121#[derive(Debug, Clone, Deserialize, Serialize)]
122#[serde(rename_all = "camelCase")]
123pub struct Field {
124    pub kind: FieldKind,
125    pub name: String,
126    pub is_required: bool,
127    pub is_list: bool,
128    pub is_unique: bool,
129    pub is_id: bool,
130    pub is_read_only: bool,
131    pub is_generated: Option<bool>,
132    pub is_updated_at: Option<bool>,
133    #[serde(rename = "type")]
134    pub field_type: String,
135    /// [string | string[]]
136    pub native_type: Option<Vec<Value>>,
137    /// Name of the field in the database
138    pub db_name: Option<String>,
139    pub has_default_value: bool,
140    #[serde(rename = "default")]
141    pub default_value: Option<FieldDefaultValue>,
142    pub relation_from_fields: Option<Vec<String>>,
143    pub relation_to_fields: Option<Vec<String>>,
144    pub relation_on_delete: Option<String>,
145    pub relation_on_update: Option<String>,
146    pub relation_name: Option<String>,
147    pub documentation: Option<String>,
148}
149
150#[derive(Debug, Clone, Deserialize, Serialize)]
151#[serde(untagged)]
152pub enum FieldDefaultValue {
153    Object(FieldDefault),
154    Scalar(FieldDefaultScalar),
155    ScalarList(Vec<FieldDefaultScalar>),
156}
157
158#[derive(Debug, Clone, Deserialize, Serialize)]
159pub struct FieldDefault {
160    pub name: String,
161    /// Can be a string or number
162    pub args: Vec<Value>,
163}
164
165/// String, bool, or number
166pub type FieldDefaultScalar = Value;
167
168#[derive(Debug, Clone, Deserialize, Serialize)]
169#[serde(rename_all = "camelCase")]
170pub struct Index {
171    pub model: String,
172    #[serde(rename = "type")]
173    pub index_type: IndexType,
174    pub is_defined_on_field: bool,
175    pub name: Option<String>,
176    pub db_name: Option<String>,
177    pub algorithm: Option<String>,
178    pub clustered: Option<bool>,
179    pub fields: Vec<IndexField>,
180}
181
182#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
183#[serde(rename_all = "lowercase")]
184pub enum IndexType {
185    Id,
186    Normal,
187    Unique,
188    Fulltext,
189}
190
191#[derive(Debug, Clone, Deserialize, Serialize)]
192#[serde(rename_all = "camelCase")]
193pub struct IndexField {
194    pub name: String,
195    pub sort_order: Option<SortOrder>,
196    pub length: Option<u32>,
197    pub operator_class: Option<String>,
198}
199
200#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
201#[serde(rename_all = "lowercase")]
202pub enum SortOrder {
203    Asc,
204    Desc,
205}
206
207#[derive(Debug, Clone, Deserialize, Serialize)]
208#[serde(rename_all = "camelCase")]
209pub struct Schema {
210    pub root_query_type: Option<String>,
211    pub root_mutation_type: Option<String>,
212    pub input_object_types: InputObjectTypes,
213    pub output_object_types: OutputObjectTypes,
214    pub enum_types: EnumTypes,
215    pub field_ref_types: FieldRefTypes,
216}
217
218#[derive(Debug, Clone, Deserialize, Serialize)]
219pub struct InputObjectTypes {
220    pub model: Option<Vec<InputType>>,
221    pub prisma: Vec<InputType>,
222}
223
224#[derive(Debug, Clone, Deserialize, Serialize)]
225pub struct OutputObjectTypes {
226    pub model: Vec<OutputType>,
227    pub prisma: Vec<OutputType>,
228}
229
230#[derive(Debug, Clone, Deserialize, Serialize)]
231pub struct EnumTypes {
232    pub model: Option<Vec<SchemaEnum>>,
233    pub prisma: Vec<SchemaEnum>,
234}
235
236#[derive(Debug, Clone, Deserialize, Serialize)]
237pub struct FieldRefTypes {
238    pub prisma: Option<Vec<FieldRefType>>,
239}
240
241#[derive(Debug, Clone, Deserialize, Serialize)]
242pub struct Query {
243    pub name: String,
244    pub args: Vec<SchemaArg>,
245    pub output: QueryOutput,
246}
247
248#[derive(Debug, Clone, Deserialize, Serialize)]
249#[serde(rename_all = "camelCase")]
250pub struct QueryOutput {
251    pub name: String,
252    pub is_required: bool,
253    pub is_list: bool,
254}
255
256#[derive(Debug, Clone, Deserialize, Serialize)]
257#[serde(rename_all = "camelCase")]
258pub struct TypeRef<T: AsRef<str>> {
259    pub is_list: bool,
260    #[serde(rename = "type")]
261    pub type_name: String,
262    pub location: T,
263    pub namespace: Option<FieldNamespace>,
264}
265
266pub type InputTypeRef = TypeRef<FieldLocation>;
267
268#[derive(Debug, Clone, Deserialize, Serialize)]
269#[serde(rename_all = "camelCase")]
270pub struct SchemaArg {
271    pub name: String,
272    pub comment: Option<String>,
273    pub is_nullable: bool,
274    pub is_required: bool,
275    pub input_types: Vec<InputTypeRef>,
276    pub deprecation: Option<Deprecation>,
277}
278
279#[derive(Debug, Clone, Deserialize, Serialize)]
280pub struct OutputType {
281    pub name: String,
282    pub fields: Vec<SchemaField>,
283}
284
285#[derive(Debug, Clone, Deserialize, Serialize)]
286#[serde(rename_all = "camelCase")]
287pub struct SchemaField {
288    pub name: String,
289    pub is_nullable: Option<bool>,
290    pub output_type: OutputTypeRef,
291    pub args: Vec<SchemaArg>,
292    pub deprecation: Option<Deprecation>,
293    pub documentation: Option<String>,
294}
295
296#[derive(Debug, Clone, Deserialize, Serialize)]
297#[serde(rename_all = "camelCase")]
298pub struct OutputTypeRef {
299    pub is_list: bool,
300    #[serde(rename = "type")]
301    pub type_name: String,
302    pub location: FieldLocation,
303}
304
305#[derive(Debug, Clone, Deserialize, Serialize)]
306#[serde(rename_all = "camelCase")]
307pub struct Deprecation {
308    pub since_version: String,
309    pub reason: String,
310    pub planned_removal_version: Option<String>,
311}
312
313#[derive(Debug, Clone, Deserialize, Serialize)]
314pub struct InputType {
315    pub name: String,
316    pub constraints: InputTypeConstraints,
317    pub meta: Option<InputTypeMeta>,
318    pub fields: Vec<SchemaArg>,
319}
320
321#[derive(Debug, Clone, Deserialize, Serialize)]
322#[serde(rename_all = "camelCase")]
323pub struct InputTypeConstraints {
324    pub max_num_fields: Option<u32>,
325    pub min_num_fields: Option<u32>,
326    pub fields: Option<Vec<String>>,
327}
328
329#[derive(Debug, Clone, Deserialize, Serialize)]
330pub struct InputTypeMeta {
331    pub source: Option<String>,
332    pub grouping: Option<String>,
333}
334
335#[derive(Debug, Clone, Deserialize, Serialize)]
336#[serde(rename_all = "camelCase")]
337pub struct FieldRefType {
338    pub name: String,
339    pub allow_types: Vec<TypeRef<FieldLocation>>,
340    pub fields: Vec<SchemaArg>,
341}
342
343#[derive(Debug, Clone, Deserialize, Serialize)]
344#[serde(rename_all = "camelCase")]
345pub struct ModelMapping {
346    pub model: String,
347    /// This is not optional in @prisma/dmmf, but can be None in the Generator DMMF
348    pub plural: Option<String>,
349    pub find_unique: Option<String>,
350    pub find_unique_or_throw: Option<String>,
351    pub find_first: Option<String>,
352    pub find_first_or_throw: Option<String>,
353    pub find_many: Option<String>,
354    pub create: Option<String>,
355    pub create_many: Option<String>,
356    pub create_many_and_return: Option<String>,
357    pub update: Option<String>,
358    pub update_many: Option<String>,
359    pub update_many_and_return: Option<String>,
360    pub upsert: Option<String>,
361    pub delete: Option<String>,
362    pub delete_many: Option<String>,
363    pub aggregate: Option<String>,
364    pub group_by: Option<String>,
365    pub count: Option<String>,
366    pub find_raw: Option<String>,
367    pub aggregate_raw: Option<String>,
368}
369
370#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
371#[serde(rename_all = "camelCase")]
372pub enum ModelAction {
373    FindUnique,
374    FindUniqueOrThrow,
375    FindFirst,
376    FindFirstOrThrow,
377    FindMany,
378    Create,
379    CreateMany,
380    CreateManyAndReturn,
381    Update,
382    UpdateMany,
383    UpdateManyAndReturn,
384    Upsert,
385    Delete,
386    DeleteMany,
387    GroupBy,
388    /// Unused
389    Count,
390    Aggregate,
391    FindRaw,
392    AggregateRaw,
393}