server
server copied to clipboard
Refactor StorageBucketService to handle document-bucket relationships safely
trafficstars
Problem
The current implementation in StorageBucketService allows saving documents before saving storage buckets, which can lead to implicit behavior based on TypeORM entity relationships.
Impact
- If a storage bucket is not saved before its documents, the relationship between them may not be persisted correctly.
- This can result in orphaned documents or missing relationships in the database.
- The behavior depends on cascade settings and relationship types, making it less predictable.
Context
- PR Discussion: https://github.com/alkem-io/server/pull/4763#discussion_r1878297104
- Related file: src/domain/storage/storage-bucket/storage.bucket.service.ts
Proposed Solution
Ensure that storage buckets are always saved before their associated documents to maintain data integrity and explicit behavior.
Tasks
- Review and document all places where documents are saved in relation to storage buckets
- Implement proper validation to ensure storage buckets exist before saving documents
- Consider adding transaction support for atomic operations
- Update tests to verify correct relationship persistence