Understanding the database model architecture in Zyeta
Zyeta uses SQLAlchemy ORM with asynchronous operations to interact with a PostgreSQL database. The models are organized in a structured hierarchy that promotes code reuse through mixins and inheritance.
Every model inherits standardized CRUD operations:
Create
Copy
async def create(self): async with async_session() as session: session.add(self) await session.commit() return self
Read
Copy
@classmethodasync def read(cls, _id: uuid_pkg.UUID): async with async_session() as session: query = select(cls).where(cls.id == _id) result = await session.execute(query) instance = result.scalars().first() return instance
Update
Copy
async def update(self, **kwargs): async with async_session() as session: for attr, value in kwargs.items(): setattr(self, attr, value) session.add(self) await session.commit() return self
Delete
Copy
async def delete(self): async with async_session() as session: await session.delete(self) await session.commit()