Skip to content

odmantic.index

Source code in odmantic/index.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class Index:
    def __init__(
        self,
        *fields: Union[FieldProxyAny, SortExpression],
        unique: bool = False,
        name: Optional[str] = None,
    ) -> None:
        """Declare an ODM index in the Model.Config.indexes generator.

        Example usage:
        ```python
        from odmantic import Model, Index
        from odmantic.query import desc

        class Player(Model):
            name: str
            score: int

            model_config = {
                "indexes": lambda: [Index(Player.name, desc(Player.score))],
            }
        ```

        Args:
            *fields (Any | SortExpression | str): fields to build the index with
            unique: build a unique index
            name: specify an optional custom index name
        """
        self.fields = cast(Tuple[Union[SortExpression, FieldProxy], ...], fields)
        self.unique = unique
        self.name = name

    def to_odm_index(self) -> "ODMBaseIndex":
        if len(self.fields) == 1:
            field = self.fields[0]
            if isinstance(field, SortExpression):
                key_name = list(field.keys())[0]
            else:
                key_name = object.__getattribute__(field, "_get_key_name")()
            return ODMSingleFieldIndex(
                key_name, unique=self.unique, index_name=self.name
            )
        else:
            fields = tuple(
                (f if isinstance(f, SortExpression) else asc(f) for f in self.fields)
            )
            return ODMCompoundIndex(fields, unique=self.unique, index_name=self.name)

__init__(*fields, unique=False, name=None)

Declare an ODM index in the Model.Config.indexes generator.

Example usage:

from odmantic import Model, Index
from odmantic.query import desc

class Player(Model):
    name: str
    score: int

    model_config = {
        "indexes": lambda: [Index(Player.name, desc(Player.score))],
    }

Parameters:

Name Type Description Default
*fields Any | SortExpression | str

fields to build the index with

()
unique bool

build a unique index

False
name Optional[str]

specify an optional custom index name

None
Source code in odmantic/index.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def __init__(
    self,
    *fields: Union[FieldProxyAny, SortExpression],
    unique: bool = False,
    name: Optional[str] = None,
) -> None:
    """Declare an ODM index in the Model.Config.indexes generator.

    Example usage:
    ```python
    from odmantic import Model, Index
    from odmantic.query import desc

    class Player(Model):
        name: str
        score: int

        model_config = {
            "indexes": lambda: [Index(Player.name, desc(Player.score))],
        }
    ```

    Args:
        *fields (Any | SortExpression | str): fields to build the index with
        unique: build a unique index
        name: specify an optional custom index name
    """
    self.fields = cast(Tuple[Union[SortExpression, FieldProxy], ...], fields)
    self.unique = unique
    self.name = name