feat: Add ORM for user model (#1924)

This commit is contained in:
Matthew Zhou
2024-10-23 10:28:00 -07:00
committed by GitHub
parent a70dea15a4
commit ff4be4576b
16 changed files with 422 additions and 282 deletions

View File

@@ -1,35 +1,28 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped, mapped_column, relationship
from letta.orm.sqlalchemy_base import SqlalchemyBase
from letta.schemas.organization import Organization as PydanticOrganization
if TYPE_CHECKING:
from sqlalchemy.orm import Session
from letta.orm.user import User
class Organization(SqlalchemyBase):
"""The highest level of the object tree. All Entities belong to one and only one Organization."""
__tablename__ = "organizations"
__tablename__ = "organization"
__pydantic_model__ = PydanticOrganization
name: Mapped[str] = mapped_column(doc="The display name of the organization.")
users: Mapped[List["User"]] = relationship("User", back_populates="organization", cascade="all, delete-orphan")
# TODO: Map these relationships later when we actually make these models
# below is just a suggestion
# users: Mapped[List["User"]] = relationship("User", back_populates="organization", cascade="all, delete-orphan")
# agents: Mapped[List["Agent"]] = relationship("Agent", back_populates="organization", cascade="all, delete-orphan")
# sources: Mapped[List["Source"]] = relationship("Source", back_populates="organization", cascade="all, delete-orphan")
# tools: Mapped[List["Tool"]] = relationship("Tool", back_populates="organization", cascade="all, delete-orphan")
# documents: Mapped[List["Document"]] = relationship("Document", back_populates="organization", cascade="all, delete-orphan")
@classmethod
def default(cls, db_session: "Session") -> "Organization":
"""Get the default org, or create it if it doesn't exist."""
try:
return db_session.query(cls).filter(cls.name == "Default Organization").one()
except NoResultFound:
return cls(name="Default Organization").create(db_session)