(refactor): database models and their associated calls#104
Merged
1 commit merged intoposeidon/devfrom Aug 4, 2025
Merged
Conversation
This pull request was closed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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