Defining multiple subcollections of the same type within a Fireorm model causes an error. While there are workarounds, such as extending the class, this should ideally be supported without errors. The issue seems to stem from the way MetadataStorage.setCollection() evaluates children before parents and updates segments.
Steps to Reproduce
Create a model with multiple subcollections of the same type.
Attempt to use the model in Fireorm.
Observe the error caused by having multiple subcollections of the same type.
Expected Behavior
Fireorm should support multiple subcollections of the same type within a model without causing errors.
Actual Behavior
An error is thrown when defining multiple subcollections of the same type within a model.
Acceptance Criteria
Modify MetadataStorage.setCollection() to correctly handle multiple subcollections of the same type.
Ensure that parent and child collections are evaluated and updated correctly.
Add unit tests to validate the support for multiple subcollections of the same type.
Additional Context
April 26, 2021: Initial issue raised about the error caused by multiple subcollections of the same type.
April 6, 2022: Additional comments about the need for a solution and suggestions for workarounds.
April 7, 2022: Reference to a related issue.
Proposed API Changes
Update MetadataStorage.setCollection:
Modify the MetadataStorage.setCollection() method to correctly handle multiple subcollections of the same type.
class MetadataStorage {
setCollection(collection: any) {
// Logic to handle multiple subcollections of the same type
// Ensure parent and child collections are evaluated and updated correctly
}
}
Support Multiple SubCollections:
Ensure the model can support multiple subcollections of the same type without errors.
@Collection()
class Band {
id: string;
name: string;
@SubCollection(Album, 'albums')
albums: ISubCollection<Album>;
@SubCollection(Album, 'singles')
singles: ISubCollection<Album>;
}
Unit Tests:
Add unit tests to validate the correct handling of multiple subcollections of the same type.
test('should support multiple subcollections of the same type', () => {
const bandRepository = getRepository(Band);
const band = new Band();
band.id = 'band1';
band.name = 'Test Band';
const album = new Album();
album.id = 'album1';
album.name = 'Test Album';
const single = new Album();
single.id = 'single1';
single.name = 'Test Single';
band.albums.create(album);
band.singles.create(single);
expect(band.albums.findById('album1').name).toBe('Test Album');
expect(band.singles.findById('single1').name).toBe('Test Single');
});
Example Implementation
class Album {
id: string;
name: string;
}
@Collection()
class Band {
id: string;
name: string;
@SubCollection(Album, 'albums')
albums: ISubCollection<Album>;
@SubCollection(Album, 'singles')
singles: ISubCollection<Album>;
}
const bandRepository = getRepository(Band);
const band = new Band();
band.id = 'band1';
band.name = 'Test Band';
const album = new Album();
album.id = 'album1';
album.name = 'Test Album';
const single = new Album();
single.id = 'single1';
single.name = 'Test Single';
bandRepository.create(band);
band.albums.create(album);
band.singles.create(single);
const fetchedAlbum = band.albums.findById('album1');
const fetchedSingle = band.singles.findById('single1');
console.log(fetchedAlbum.name); // Output: 'Test Album'
console.log(fetchedSingle.name); // Output: 'Test Single'
Description
Defining multiple subcollections of the same type within a Fireorm model causes an error. While there are workarounds, such as extending the class, this should ideally be supported without errors. The issue seems to stem from the way
MetadataStorage.setCollection()
evaluates children before parents and updates segments.Steps to Reproduce
Expected Behavior
Fireorm should support multiple subcollections of the same type within a model without causing errors.
Actual Behavior
An error is thrown when defining multiple subcollections of the same type within a model.
Acceptance Criteria
MetadataStorage.setCollection()
to correctly handle multiple subcollections of the same type.Additional Context
Proposed API Changes
Update MetadataStorage.setCollection:
MetadataStorage.setCollection()
method to correctly handle multiple subcollections of the same type.Support Multiple SubCollections:
Unit Tests:
Example Implementation
Original Issue