Skip to main content
Definable 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.

Model Architecture

Base Classes and Mixins

All database models are built on top of a common foundation of base classes and mixins:
The architecture follows these core principles:
  • Base: The foundational class for all SQLAlchemy models
  • UUIDMixin: Provides a UUID primary key for all models
  • TimestampMixin: Adds creation timestamps to track when records are created
  • CRUD: Combines the Base and mixins while adding async CRUD operations

CRUD Operations

Every model inherits standardized CRUD operations:

Core Domain Models

Definableโ€™s domain is represented by the following core models:

User and Authentication

  • UserModel: Stores user account information
    • Email, password (hashed), name, active status
    • Relationships to organizations and invitations

Organizations and Teams

  • OrganizationModel: Represents a customer organization
    • Name, description, settings
    • Relationships to members, knowledge bases, etc.
  • OrganizationMemberModel: Maps users to organizations with roles
    • Organization ID, user ID, role ID
    • Activity status

Knowledge Bases

  • KnowledgeBaseModel: Stores metadata about knowledge bases
    • Name, description, settings, embedding model
    • Relationships to documents and organization
  • KBDocumentModel: Represents documents in a knowledge base
    • Content, metadata, processing status
    • Vector embeddings for semantic search

Conversations and Messages

  • ConversationModel: Tracks conversation history
    • Title, context, metadata
    • Relationships to messages and user
  • MessageModel: Individual messages in a conversation
    • Content, role (user/assistant), metadata
    • Processing timestamps and status

Agents and Tools

  • AgentModel: Configuration for AI agents
    • Name, description, capabilities, prompt template
    • Relationships to tools and conversations
  • ToolModel: Custom tools for agent use
    • Name, description, implementation details
    • Input/output schemas and API integration details

Model Relationships

The database schema uses relationships to model domain connections:

PostgreSQL Features

Definable leverages PostgreSQL-specific features:
  • UUID Generation: Native gen_random_uuid() function
  • pgvector Extension: Enables vector embeddings storage and similarity search
  • Full Text Search: Used for content indexing and searching
  • JSON/JSONB: Flexible storage of metadata and configurations

Example Model

Hereโ€™s an example of a model implementation:

Database Connections

Database connections are handled through asynchronous session management:

Best Practices for Model Development

When extending or creating models in Definable:
  1. Inherit from CRUD: Always extend the CRUD base class
  2. Use Type Annotations: Leverage SQLAlchemyโ€™s Mapped[Type] for clarity
  3. Define Relationships: Explicitly define relationships between models
  4. Add Comments: Document the purpose of each model and complex attributes
  5. Keep Models Focused: Each model should represent a single domain entity
  6. Index for Performance: Add indexes to frequently queried columns