Skip to content

(refactor): database models and their associated calls#104

Merged
1 commit merged intoposeidon/devfrom
feature/103-paz-db-models-and-db-backend-refactor
Aug 4, 2025
Merged

(refactor): database models and their associated calls#104
1 commit merged intoposeidon/devfrom
feature/103-paz-db-models-and-db-backend-refactor

Conversation

@ghost
Copy link

@ghost ghost commented Jul 18, 2025

Refactor: Database models and their associated calls
This PR introduces a comprehensive refactoring of the database layer, implementing a clean separation of concerns with proper model definitions, repository patterns, and service layers.
Overview
This refactor restructures the entire database architecture to provide better maintainability, type safety, and scalability for the Mindtrace platform. The changes implement a modern ODM-based approach using Beanie for MongoDB integration.
Database Models
Core Models Implemented:
User - User management with organization roles and project assignments
Organization - Multi-tenant organization structure with subscription plans
Project - Project management within organizations
Camera - Camera device management with configuration and status tracking
Image - Image metadata and storage management
Model - ML model definitions with validation status
ModelDeployment - Model deployment configurations and health monitoring
Key Features:
Forward Reference Resolution: Proper handling of circular dependencies between models
Automatic Timestamping: Created/updated timestamps with UTC timezone
Link Relationships: Beanie Link fields for proper document relationships
Validation Hooks: Pre-event hooks for data validation and timestamp management
Enum Support: Structured enums for status fields, subscription plans, and roles
Repository Pattern
Repository Classes:
UserRepository - User CRUD operations with organization scoping
OrganizationRepository - Organization management and user limits
ProjectRepository - Project operations with proper ownership
CameraRepository - Camera management with multi-tenant support
ImageRepository - Image metadata operations
ModelRepository - ML model lifecycle management
ModelDeploymentRepository - Deployment configuration management
Benefits:
Separation of Concerns: Database logic isolated from business logic
Testability: Repository pattern enables easier unit testing
Consistency: Standardized CRUD operations across all models
Multi-tenancy: Built-in organization and project scoping
Services Layer
State Management Services:
AuthState - Authentication and authorization logic
CameraState - Camera discovery, configuration, and streaming
ImageState - Image upload and management
UserManagementState - User administration
OrganizationManagementState - Organization administration
ModelDeploymentState - ML model deployment management
Database Initialization
Model Registration: All models properly registered with Beanie
Forward Reference Handling: Automatic model rebuilding to resolve circular dependencies
Connection Management: Singleton MongoDB client with proper initialization
Error Handling: Graceful handling of model relationship resolution
Technical Improvements
Type Safety: Full TypeScript-style type hints with Pydantic models
Validation: Comprehensive data validation at the model level
Performance: Optimized queries with proper indexing support
Scalability: Multi-tenant architecture with proper data isolation
Maintainability: Clear separation between data, business, and presentation layers
Security Enhancements
Multi-tenancy: Organization-scoped data access
Role-based Access: Admin, user, and super admin role separation
Data Isolation: Proper scoping prevents cross-organization data leaks
Validation: Input validation at multiple layers
Testing Impact
This refactor provides a solid foundation for comprehensive testing:
Repository classes can be easily mocked
Model validation can be unit tested
Service layer logic is isolated and testable
Database operations are abstracted and consistent
Migration Notes
All existing functionality is preserved
Forward references are automatically resolved during initialization
No breaking changes to existing API endpoints
This refactor sets the foundation for a scalable, maintainable, and secure database architecture that will support the platform's continued growth and feature development

@ghost ghost requested review from Yasserelhaddar and canelbirlik July 18, 2025 11:44
@ghost ghost self-assigned this Jul 23, 2025
@canelbirlik canelbirlik added the paz Raised from paz team in the mindtrace package label Jul 30, 2025
@ghost ghost mentioned this pull request Jul 30, 2025
@ghost ghost merged commit 12daf8f into poseidon/dev Aug 4, 2025
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

paz Raised from paz team in the mindtrace package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants