File: models.py

package info (click to toggle)
python-odmantic 1.0.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,756 kB
  • sloc: python: 8,547; sh: 37; makefile: 34; xml: 13; javascript: 3
file content (68 lines) | stat: -rw-r--r-- 2,093 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Models based on https://github.com/tortoise/orm-benchmarks"""

from datetime import datetime, timezone
from decimal import Decimal
from random import choice
from typing import Iterator, Optional

from typing_extensions import Literal, get_args

from odmantic import Field, Model

Level = Literal[10, 20, 30, 40, 50]
VALID_LEVELS = list(get_args(Level))


def utc_now():
    return datetime.now(timezone.utc)


class SmallJournal(Model):
    timestamp: datetime = Field(default_factory=utc_now)
    level: Level = Field(index=True)
    text: str = Field(index=True)

    @classmethod
    def get_random_instances(cls, context: str, count: int) -> Iterator["SmallJournal"]:
        for i in range(count):
            yield cls(level=choice(VALID_LEVELS), text=f"From {context}, item {i}")


class JournalWithRelations(Model):
    timestamp: datetime = Field(default_factory=utc_now)
    level: Level = Field(index=True)
    text: str = Field(index=True)

    # parent


class BigJournal(Model):
    timestamp: datetime = Field(default_factory=utc_now)
    level: Level = Field(index=True)
    text: str = Field(index=True)

    col_float1: float = Field(default=2.2)
    col_smallint1: int = Field(default=2)
    col_int1: int = Field(default=2000000)
    col_bigint1: int = Field(default=99999999)
    col_char1: str = Field(default=255, max_length=255)
    col_text1: str = Field(
        default="Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa,Moo,Foo,Baa,Waa",
    )
    col_decimal1: Decimal = Field(default=Decimal("2.2"))
    col_json1: dict = Field(
        default={"a": 1, "b": "b", "c": [2], "d": {"e": 3}, "f": True},
    )

    col_float2: Optional[float] = Field(default=None)
    col_smallint2: Optional[int] = Field(default=None)
    col_int2: Optional[int] = Field(default=None)
    col_bigint2: Optional[int] = Field(default=None)
    col_char2: Optional[str] = Field(default=None, max_length=255)
    col_text2: Optional[str] = Field(
        default=None,
    )
    col_decimal2: Optional[Decimal] = Field(default=None)
    col_json2: Optional[dict] = Field(
        default=None,
    )