Source Code:
src/gaia/core/database_mixin.pyComponent: DatabaseMixin
Module:
gaia.core.database_mixin (planned location)
Import: from gaia import DatabaseMixin (after v1.0.0 restructure)Overview
DatabaseMixin provides database connectivity for GAIA agents using SQLAlchemy Core (not ORM). It enables agents to store and query data across multiple database backends (SQLite, PostgreSQL, MySQL) with a simple, consistent API. Why not use ORM?- Simpler for AI agents (tools return dicts, not objects)
- More explicit SQL (easier for LLMs to reason about)
- Better performance for simple queries
- Lower learning curve
Requirements
Functional Requirements
-
Multi-Database Support
- SQLite for development and small deployments
- PostgreSQL for production
- MySQL for existing infrastructure
-
Simple Query Interface
execute_query()- SELECT queries returning list of dictsexecute_insert()- INSERT with optional RETURNING clauseexecute_update()- UPDATE with parameterized WHEREexecute_delete()- DELETE with parameterized WHERE
-
Transaction Management
- Context manager for automatic commit/rollback
- Explicit transaction control
- Nested transaction support
-
Connection Pooling
- Configurable pool size
- Automatic connection recycling
- Thread-safe operations
-
Schema Management
- Initialize from SQL file
- Table existence checking
- Schema reflection
-
Security
- Parameterized queries (SQL injection prevention)
- No raw SQL string interpolation
- Named parameters only
Non-Functional Requirements
-
Performance
- Connection pooling for concurrent requests
- Lazy initialization option
- Query result streaming for large datasets
-
Error Handling
- Clear error messages
- Connection retry logic
- Transaction rollback on errors
-
Testability
- In-memory SQLite for tests
- Temporary database creation
- Easy mocking
API Specification
File Location
Public Interface
Implementation Details
Connection Pooling
SQLite-specific handling:Schema Initialization
Query Execution
INSERT with RETURNING (Database-specific)
UPDATE with Parameter Safety
Testing Requirements
Unit Tests
File:tests/sdk/test_database_mixin.py
Dependencies
Required Packages
Import Dependencies
Error Handling
Common Errors and Responses
Usage Examples
Example 1: Simple Agent with Database
Example 2: Transaction Usage
Documentation Updates Required
SDK.md
Add to Section 1 (Core Agent System):Update Existing Examples
Replace all temporary database code in SDK.md with DatabaseMixin usage.Acceptance Criteria
- DatabaseMixin class implemented in
src/gaia/core/database_mixin.py - All methods implemented with docstrings
- Supports SQLite, PostgreSQL, MySQL
- Connection pooling works
- Transaction management works (commit/rollback)
- Schema initialization from file works
- All unit tests pass (10+ tests)
- Exported from
gaia/__init__.py - Can import:
from gaia import DatabaseMixin - Documented in SDK.md
- Example agent using DatabaseMixin works
- EMR agent can use it
Implementation Checklist
Step 1: Create File
- Create
src/gaia/core/database_mixin.py - Add copyright header
- Add module docstring
Step 2: Implement Core Methods
-
initialize_database() -
execute_query() -
execute_insert() -
execute_update() -
execute_delete() -
transaction()context manager -
get_connection()context manager -
table_exists() -
close_database()
Step 3: Add Error Handling
- RuntimeError if not initialized
- DatabaseError for SQL errors
- FileNotFoundError for schema
- Proper error messages
Step 4: Add Logging
- Log initialization
- Log query execution (debug level)
- Log errors
- Hide credentials in logs
Step 5: Write Tests
- Create
tests/sdk/test_database_mixin.py - Test all methods
- Test transactions
- Test error cases
- Test multiple databases
Step 6: Export & Document
- Add to
src/gaia/__init__.py - Add to
__all__list - Update SDK.md
- Add example to SDK.md
Step 7: Validate
- Can import:
from gaia import DatabaseMixin - Example agent works
- All tests pass
- EMR agent can use it
DatabaseMixin Technical Specification