Skip to content

Commit

Permalink
Swap to using SQLModel instead of pony.orm
Browse files Browse the repository at this point in the history
  • Loading branch information
Buried-In-Code committed Jun 4, 2024
1 parent d2d1580 commit 166de33
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 622 deletions.
Binary file added freyr.sqlite
Binary file not shown.
2 changes: 1 addition & 1 deletion freyr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from freyr.console import CONSOLE

__version__ = "0.5.3"
__version__ = "0.6.0"


def get_cache() -> Path:
Expand Down
3 changes: 3 additions & 0 deletions freyr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from freyr import __version__, elapsed_timer, get_project, setup_logging
from freyr.constants import constants
from freyr.database import create_db_and_tables
from freyr.routers.api import router as api_router
from freyr.routers.html import router as html_router

Expand All @@ -30,6 +31,8 @@ def create_app() -> FastAPI:
async def startup_event() -> None:
setup_logging()

create_db_and_tables()

LOGGER.info(
"Listening on %s:%s", constants.settings.website.host, constants.settings.website.port
)
Expand Down
18 changes: 18 additions & 0 deletions freyr/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sqlmodel import Session, SQLModel, create_engine

from freyr.constants import constants

sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

connect_args = {"check_same_thread": False}
engine = create_engine(constants.settings.database.db_url, echo=False, connect_args=connect_args)


def create_db_and_tables() -> None:
SQLModel.metadata.create_all(engine)


def get_session() -> Session:
with Session(engine) as session:
yield session
26 changes: 0 additions & 26 deletions freyr/database/__init__.py

This file was deleted.

68 changes: 0 additions & 68 deletions freyr/database/tables.py

This file was deleted.

86 changes: 86 additions & 0 deletions freyr/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from datetime import datetime
from decimal import Decimal
from typing import Annotated, Self

from sqlmodel import Field, Relationship, SQLModel


class DeviceBase(SQLModel):
name: str = Field(index=True)


class Device(DeviceBase, table=True):
__tablename__ = "devices"

id: int | None = Field(default=None, primary_key=True)
readings: list["Reading"] = Relationship(back_populates="device")


class DeviceCreate(DeviceBase):
pass


class DevicePublic(DeviceBase):
id: int
readings: list["ReadingPublic"] = Field(default_factory=list)


class ReadingBase(SQLModel):
temperature: Decimal | None = None
humidity: Decimal | None = None


class Reading(ReadingBase, table=True):
__tablename__ = "readings"

id: int | None = Field(default=None, primary_key=True)
timestamp: datetime
device_id: int = Field(foreign_key="devices.id")
device: Device = Relationship(back_populates="readings")

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, Reading):
raise NotImplementedError
return self.timestamp < other.timestamp

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, Reading):
raise NotImplementedError
return self.timestamp == other.timestamp

def __hash__(self: Self) -> int:
return hash((type(self), self.timestamp))


class ReadingCreate(ReadingBase):
device_id: int | None = None
timestamp: datetime | None = None


class ReadingPublic(ReadingBase):
id: int
timestamp: datetime


class Summary(SQLModel):
class Reading(SQLModel):
timestamp: datetime
temperature: Annotated[Decimal, Field(decimal_places=2)] | None = None
humidity: Annotated[Decimal, Field(decimal_places=2)] | None = None

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, Summary.Reading):
raise NotImplementedError
return self.timestamp < other.timestamp

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, Summary.Reading):
raise NotImplementedError
return self.timestamp == other.timestamp

def __hash__(self: Self) -> int:
return hash((type(self), self.timestamp))

highs: list[Reading] = Field(default_factory=list)
averages: list[Reading] = Field(default_factory=list)
lows: list[Reading] = Field(default_factory=list)
31 changes: 0 additions & 31 deletions freyr/models/__init__.py

This file was deleted.

56 changes: 0 additions & 56 deletions freyr/models/device.py

This file was deleted.

38 changes: 0 additions & 38 deletions freyr/models/reading.py

This file was deleted.

Loading

0 comments on commit 166de33

Please sign in to comment.