1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Rendering of relation fields.

use crate::introspection::introspection_pair::RelationFieldPair;
use datamodel_renderer::datamodel as renderer;

/// Render a relation field to be added in a model.
pub(super) fn render(field: RelationFieldPair<'_>) -> renderer::Field<'_> {
    let mut rendered = renderer::Field::new(field.field_name(), field.prisma_type());

    if field.is_optional() {
        rendered.optional();
    } else if field.is_array() {
        rendered.array();
    }

    if field.ignore() {
        rendered.ignore();
    }

    if field.renders_attribute() {
        let mut relation = renderer::Relation::new();

        if let Some(name) = field.relation_name() {
            relation.name(name);
        }

        if let Some(fields) = field.fields() {
            relation.fields(fields);
        }

        if let Some(references) = field.references() {
            relation.references(references);
        }

        if let Some(action) = field.on_delete() {
            relation.on_delete(action);
        }

        if let Some(action) = field.on_update() {
            relation.on_update(action);
        }

        if let Some(map) = field.constraint_name() {
            relation.map(map);
        }

        rendered.relation(relation);
    }

    rendered
}