[DAPS-1513] Rustlike JS
Ticket
Description
This PR implements a Rust-compatible design pattern system for the DataFed repository management, preparing the codebase for eventual Rust migration while maintaining JavaScript functionality.
Key changes:
- Created Repository Type System - Implemented enum-like types (globus, metadata_only) to support different repository behaviors
- Replaced Inheritance with Composition - Transformed OOP patterns to use trait-like operations and factory patterns
- Added Result Type Error Handling - Implemented Rust-style Result types for explicit error propagation
- Created Modular Repository Implementation - Separated concerns into:
-
types.js- Type definitions and data structures -factory.js- Type-based repository creation -operations.js- Trait-like repository operations -validation.js- Pure validation functions -globus.js/metadata.js- Type-specific implementations - Added Rust Book References - Comprehensive documentation linking to relevant Rust patterns
How Has This Been Tested?
- Docker Compose with ArangoDB 3.12.4
- Node.js Foxx microservices
- Local development environment
Unit Tests - Created repository_types.test.js covering:
- Type creation and validation
- Factory pattern functionality
- Operation implementations
- Error handling scenarios
Integration Tests - Created test-foxx-api.sh:
- API endpoint connectivity
- Repository CRUD operations
- Type-specific behaviors
- Error response validation
Artifacts
API Response Examples:
// Version endpoint working at /api/1
{
"release_year": 2025,
"api_major": 1,
"api_minor": 1,
"component_major": 1
}
// Repository with type field
{
"_id": "repo/metadata_only_repo",
"type": "metadata_only",
"title": "Metadata Only Repository",
"capacity": 1000000
}
Test Execution Output:
=== Foxx API Test Suite ===
Testing: Version endpoint... PASSED
Testing: Foxx service health... PASSED
Testing: List repositories endpoint... PASSED
Tests passed: 3
Summary by Sourcery
Introduce a Rust-compatible repository type system in JavaScript by modularizing repository logic into enum-like types, trait-like operations, and composition-based factories, update the Foxx API to use the new patterns while preserving legacy compatibility, and add supporting tests, examples, and documentation to facilitate a future Rust migration.
New Features:
- Define a RepositoryType enum and a Rust-style Result type for explicit error handling
- Implement factory-based repository creation and trait-like operations in types.js, factory.js, operations.js, validation.js, globus.js, and metadata.js
- Add a new Foxx API router (repo_router_new.js) that uses the repository type system for CRUD and allocation endpoints
- Provide an ArangoDB migration script to backfill the
typefield on existing repositories - Include usage examples (example.js) and a detailed README documenting the Rust-compatible design patterns
Enhancements:
- Refactor the legacy Repo class to delegate to the new repository operations internally for backward compatibility
Documentation:
- Add a comprehensive README in the repository module illustrating the design patterns and migration benefits
- Include example code demonstrating repository creation, trait-like operations, and error handling
Tests:
- Add unit tests in repository_types.test.js covering types, factory, validation, and operations
- Add integration tests for the Foxx API endpoints to verify repository CRUD and allocation behaviors
Chores:
- Modularize repository code into separate files for better maintainability and future Rust migration
Reviewer's Guide
Implements a Rust-compatible repository type system with enum-like types, explicit Result-based error handling, and trait-like operations; refactors the Foxx API and legacy Repo class to leverage these modular patterns; and adds comprehensive tests, examples, documentation, and a migration script.
Class diagram for the new repository type system
classDiagram
class RepositoryOps {
+validate(repository)
+createAllocation(repository, allocationParams)
+deleteAllocation(repository, subjectId)
+supportsDataOperations(repository)
+getCapacityInfo(repository)
+save(repository)
+update(repository, updates)
+find(repoId)
+list(filter)
+checkPermission(repository, userId, permission)
}
class Result {
+ok(value)
+err(error)
}
class RepositoryType {
<<enum>>
GLOBUS
METADATA_ONLY
}
class ExecutionMethod {
<<enum>>
TASK
DIRECT
}
class Repository {
type
data
}
class GlobusConfig {
endpoint
path
pub_key
address
exp_path
domain
}
class RepositoryData {
_key
_id
type
title
desc
capacity
admins
}
RepositoryData <|-- GlobusConfig : composition
RepositoryType <.. Repository : type
RepositoryData <.. Repository : data
Result <.. RepositoryOps
ExecutionMethod <.. Result
File-Level Changes
| Change | Details | Files |
|---|---|---|
| Introduce repository type definitions and Result pattern for explicit error handling |
|
core/database/foxx/api/repository/types.js |
| Implement factory pattern and trait-like dynamic dispatch for repository operations |
|
core/database/foxx/api/repository/factory.jscore/database/foxx/api/repository/operations.js |
| Extract modular validation and type-specific implementations |
|
core/database/foxx/api/repository/validation.jscore/database/foxx/api/repository/metadata.jscore/database/foxx/api/repository/globus.js |
| Refactor Foxx API to leverage new system and maintain backward compatibility |
|
core/database/foxx/api/repo.jscore/database/foxx/api/repo_router_new.js |
| Add testing, documentation, examples, and migration support |
|
core/database/foxx/tests/repository_types.test.jscore/database/foxx/api/repository/example.jscore/database/foxx/api/repository/README.mdcore/database/foxx/migrations/add_repository_type.js |
Possibly linked issues
- #1513: The PR implements a Rust-compatible repository design using enums, traits, composition, and Result types, directly addressing the issue's architectural goals for Rust migration.
- #1513: The PR directly implements the repository type system, including enum, validation, and modular design as specified.
- #1514: The PR implements the core repository type system, new API endpoints, and allocation logic, directly fulfilling the issue's requirements for type support and response format.
Tips and commands
Interacting with Sourcery
-
Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
-
Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with
@sourcery-ai issueto create an issue from it. -
Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. -
Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. -
Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. -
Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore. -
Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
- Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Stacked diff
All review items already addressed in code. Documentation simplified per feedback.
LGTM