Initial release: async repository layer over SQLModel

This commit is contained in:
2026-08-13 22:25:59 +03:00
commit 3d85c2b5dd
26 changed files with 2252 additions and 0 deletions

78
tests/models.py Normal file
View File

@@ -0,0 +1,78 @@
from pydantic import BaseModel
from pydantic_filters import BaseFilter
from sqlmodel import Field, Relationship
from metaorm import BaseRepository, BaseTable
class User(BaseModel):
id: int | None = None
name: str
email: str
class UserTable(BaseTable[User], table=True):
__tablename__ = "users"
id: int | None = Field(default=None, primary_key=True)
name: str
email: str = Field(unique=True)
@classmethod
def from_item(cls, item: User) -> "UserTable":
return cls(id=item.id, name=item.name, email=item.email)
def to_item(self) -> User:
return User(id=self.id, name=self.name, email=self.email)
class UserRepository(BaseRepository):
def get_db_table(self) -> type[UserTable]:
return UserTable
def get_dto_type(self) -> type[User]:
return User
class ProductTable(BaseTable, table=True):
__tablename__ = "products"
id: int | None = Field(default=None, primary_key=True)
name: str
price: float
class ProductFilter(BaseFilter):
name: str | None = None
price: int | None = None
class ProductRepository(BaseRepository):
def get_db_table(self) -> type[ProductTable]:
return ProductTable
def get_filter_type(self) -> type[ProductFilter]:
return ProductFilter
class AuthorTable(BaseTable, table=True):
__tablename__ = "authors"
id: int | None = Field(default=None, primary_key=True)
name: str
books: list["BookTable"] = Relationship(back_populates="author")
class BookTable(BaseTable, table=True):
__tablename__ = "books"
id: int | None = Field(default=None, primary_key=True)
title: str
author_id: int = Field(foreign_key="authors.id")
author: AuthorTable = Relationship(back_populates="books")
class AuthorRepository(BaseRepository):
def get_db_table(self) -> type[AuthorTable]:
return AuthorTable
class BookRepository(BaseRepository):
def get_db_table(self) -> type[BookTable]:
return BookTable