Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Custom Repositories in Transactions #30

Open
elersong opened this issue Jul 13, 2024 · 0 comments
Open

Support Custom Repositories in Transactions #30

elersong opened this issue Jul 13, 2024 · 0 comments
Labels
enhancement New feature or request from_willyovale An issue described in original project, but never implemented planning Not yet ready for development

Comments

@elersong
Copy link
Owner

Description

Currently, the transaction object in Fireorm only exposes a getRepository method, which returns a TransactionRepository. This limits the ability to use custom repositories within transactions, as there is no getCustomRepository method available.

Steps to Reproduce

  1. Attempt to use a custom repository within a transaction in Fireorm.
  2. Notice that the transaction object does not provide a getCustomRepository method.

Expected Behavior

Ability to use custom repositories within transactions by providing a getCustomRepository method on the transaction object.

Actual Behavior

The transaction object only exposes a getRepository method, which returns a TransactionRepository.

Acceptance Criteria

  • Implement a getCustomRepository method on the transaction object.
  • Ensure the custom repositories within transactions receive the necessary special treatment.
  • Add unit tests to verify the functionality of custom repositories in transactions.

Additional Context

  • December 31, 2020: Initial issue raised about the lack of support for custom repositories in transactions.
  • January 11, 2021: Acknowledgment of the issue and discussion about the special treatment required for transaction repositories.
  • March 7, 2021: Expression of interest in supporting this feature, with an invitation for contributions.

Proposed API Changes

  1. Implement getCustomRepository Method:

    • Add a getCustomRepository method to the transaction object to support custom repositories in transactions.
    class Transaction {
      // Existing methods...
    
      getCustomRepository<T>(entity: EntityConstructor<T>): CustomRepository<T> {
        const repository = getCustomRepository(entity);
        // Apply necessary special treatment for transaction
        return new CustomTransactionRepository(repository, this);
      }
    }
  2. Special Treatment for Transaction Repositories:

    • Ensure that custom repositories within transactions receive the necessary special treatment.
    class CustomTransactionRepository<T> extends CustomRepository<T> {
      constructor(repository: CustomRepository<T>, private transaction: Transaction) {
        super(repository);
        // Apply transaction-specific logic
      }
    
      // Override necessary methods to handle transactions
    }
  3. Unit Tests:

    • Create unit tests to validate the functionality of custom repositories within transactions.
    test('should support custom repositories within transactions', async () => {
      const customRepo = transaction.getCustomRepository(CustomEntity);
      const customEntity = new CustomEntity();
      customEntity.name = 'Test Entity';
    
      await transaction.run(async () => {
        await customRepo.create(customEntity);
      });
    
      const fetchedEntity = await customRepo.findById(customEntity.id);
      expect(fetchedEntity.name).toBe('Test Entity');
    });

Example Implementation

@Collection()
class CustomEntity {
  id: string;
  name: string;
}

// Custom repository
class CustomRepository extends BaseRepository<CustomEntity> {
  // Custom methods...
}

// Transaction using custom repository
const customRepo = getCustomRepository(CustomEntity);

await transaction.run(async () => {
  const customRepoInTx = transaction.getCustomRepository(CustomEntity);
  const customEntity = new CustomEntity();
  customEntity.name = 'Test Entity';

  await customRepoInTx.create(customEntity);
});

// Verify the entity was created
const fetchedEntity = await customRepo.findById(customEntity.id);
console.log(fetchedEntity.name); // Output: 'Test Entity'

Original Issue

@elersong elersong added enhancement New feature or request from_willyovale An issue described in original project, but never implemented planning Not yet ready for development labels Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request from_willyovale An issue described in original project, but never implemented planning Not yet ready for development
Projects
None yet
Development

No branches or pull requests

1 participant