Skip to main content
The Definable backend follows a well-organized directory structure that promotes separation of concerns and maintainability. This guide will help you navigate the codebase and understand the purpose of each directory.

Root Directory Structure

Source Code Structure (src/)

Key Directories Explained

Services (src/services/)

The services directory contains the core business logic of the application, organized by domain. Each service typically includes:
  • service.py: The main service implementation with HTTP-exposed methods
  • schema.py: Pydantic models for request/response validation
  • Additional domain-specific modules as needed
Services follow this pattern:

Base Service (__base/)

The __base/ directory contains core service infrastructure:
  • acquire.py: Dependency injection container
  • manager.py: Service discovery and registration

Models (src/models/)

The models directory contains SQLAlchemy ORM models representing database entities. Each file typically follows the *_model.py naming convention and defines one or more related models. Models follow this pattern:

Database (src/database/)

Contains database connection management, session handling, and base model classes:
  • models.py: Base model class and CRUD operations
  • postgres.py: PostgreSQL connection setup
  • __init__.py: Exports session factories and utilities

Middlewares (src/middlewares/)

Contains FastAPI middleware components that process requests and responses:
  • exceptions.py: Global exception handling
  • ratelimit.py: Rate limiting middleware

Dependencies (src/dependencies/)

Contains FastAPI dependencies for authentication, authorization, and other cross-cutting concerns:
  • security.py: JWT bearer token authentication and RBAC

Libraries (src/libs/)

Contains integrations with external services and reusable libraries:
  • s3/: Amazon S3 compatible storage
  • vectorstore/: Vector embeddings and similarity search
  • tools_factory/: Dynamic tool generation

Testing Structure (tests/)

Documentation Structure (docs/)

Configuration Files

alembic.copy.ini

Template for Alembic database migration configuration. This should be copied to alembic.ini and updated with your database connection details.

.env.local

Template for environment variables. This should be copied to .env and populated with your configuration values.

pyproject.toml

Contains project metadata, dependencies, and tool configurations using Poetry format.

ruff.toml

Configuration for the Ruff linter, specifying code style and quality rules.

mypy.ini

Configuration for Mypy type checking, ensuring strict type safety.

Working with the Codebase

Adding New Features

When adding new features, you typically need to:
  1. Create a new model in src/models/ if you need database entities
  2. Create a new service in src/services/ with appropriate business logic
  3. Update any dependencies or integrations as needed
  4. Add tests in the tests/ directory
  5. Update documentation in the docs/ directory

Code Organization Principles

Definable follows these principles for code organization:
  1. Domain-driven organization: Code is organized by domain (auth, org, kb, etc.)
  2. Single responsibility: Each module has a clear, focused purpose
  3. Explicit dependencies: Dependencies are injected through the Acquire class
  4. Clear interfaces: Services expose clear HTTP endpoints through the http_exposed property

Naming Conventions

  • Service methods: {http_method}_{resource}
    • Example: post_signup, get_user, put_update_profile
  • Model files: {entity}_model.py
    • Example: user_model.py, organization_model.py
  • Schema files: schema.py (within service directories)
  • Test files: test_{component}.py

Next Steps