Skip to content

databases

Pydantic models/schemas for the LinksResource used in /databases

DatabaseCreate

Bases: EntryResourceCreate, LinksResourceAttributes

Model for creating new LinksResources representing /databases resources in the MongoDB.

Required fields:

  • name
  • base_url

Original required fields for a LinksResourceAttributes model:

  • name
  • description
  • link_type
Source code in optimade_gateway/models/databases.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class DatabaseCreate(EntryResourceCreate, LinksResourceAttributes):
    """Model for creating new LinksResources representing `/databases` resources in the
    MongoDB.

    Required fields:

    - `name`
    - `base_url`

    Original required fields for a
    [`LinksResourceAttributes`](https://www.optimade.org/optimade-python-tools/api_reference/models/links/#optimade.models.links.LinksResourceAttributes)
    model:

    - `name`
    - `description`
    - `link_type`

    """

    description: Annotated[
        str | None,
        StrictField(
            description=LinksResourceAttributes.model_fields["description"].description
        ),
    ] = None

    base_url: Annotated[
        JsonLinkType,
        StrictField(
            description=LinksResourceAttributes.model_fields["base_url"].description
        ),
    ]

    homepage: Annotated[
        JsonLinkType | None,
        StrictField(
            description=LinksResourceAttributes.model_fields["homepage"].description,
        ),
    ] = None

    link_type: Annotated[
        LinkType | None,
        StrictField(
            title=LinksResourceAttributes.model_fields["link_type"].title,
            description=LinksResourceAttributes.model_fields["link_type"].description,
        ),
    ] = None

    @field_validator("link_type", mode="after")
    @classmethod
    def ensure_database_link_type(cls, value: LinkType) -> LinkType:
        """Ensure databases are not index meta-database-only types

        I.e., ensure they're not of type `"root"` or `"providers"`.

        !!! note
            Both `"external"` and `"child"` can still represent index meta-dbs,
            but `"root"` and `"providers"` can not represent "regular" dbs.

        """
        if value in (LinkType.ROOT, LinkType.PROVIDERS):
            raise ValueError(
                "Databases with 'root' or 'providers' link_type is not allowed for "
                f"gateway-usable database resources. Given link_type: {value}"
            )
        return value

base_url: Annotated[JsonLinkType, StrictField(description=LinksResourceAttributes.model_fields['base_url'].description)] instance-attribute

description: Annotated[str | None, StrictField(description=LinksResourceAttributes.model_fields['description'].description)] = None class-attribute instance-attribute

homepage: Annotated[JsonLinkType | None, StrictField(description=LinksResourceAttributes.model_fields['homepage'].description)] = None class-attribute instance-attribute

Ensure databases are not index meta-database-only types

I.e., ensure they're not of type "root" or "providers".

Note

Both "external" and "child" can still represent index meta-dbs, but "root" and "providers" can not represent "regular" dbs.

Source code in optimade_gateway/models/databases.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@field_validator("link_type", mode="after")
@classmethod
def ensure_database_link_type(cls, value: LinkType) -> LinkType:
    """Ensure databases are not index meta-database-only types

    I.e., ensure they're not of type `"root"` or `"providers"`.

    !!! note
        Both `"external"` and `"child"` can still represent index meta-dbs,
        but `"root"` and `"providers"` can not represent "regular" dbs.

    """
    if value in (LinkType.ROOT, LinkType.PROVIDERS):
        raise ValueError(
            "Databases with 'root' or 'providers' link_type is not allowed for "
            f"gateway-usable database resources. Given link_type: {value}"
        )
    return value