Refactor MongoDB Repository Interface by Removing Bulk Operations
- Removed the BulkCreate, BulkUpdate, and BulkDelete methods from the Repository interface to streamline the data access layer. - Updated the FindMany method to maintain functionality while adhering to Clean Architecture principles. - This change simplifies the repository interface, focusing on essential operations and improving maintainability.
This commit is contained in:
@@ -65,15 +65,6 @@ type Repository[T any] interface {
|
||||
|
||||
// FindMany retrieves multiple documents without pagination
|
||||
FindMany(ctx context.Context, filter bson.M, limit int) ([]T, error)
|
||||
|
||||
// BulkCreate inserts multiple documents
|
||||
BulkCreate(ctx context.Context, models []T) error
|
||||
|
||||
// BulkUpdate updates multiple documents
|
||||
BulkUpdate(ctx context.Context, filter bson.M, update bson.M) error
|
||||
|
||||
// BulkDelete removes multiple documents
|
||||
BulkDelete(ctx context.Context, filter bson.M) error
|
||||
}
|
||||
|
||||
// repository implements the Repository interface
|
||||
@@ -403,81 +394,6 @@ func (r *repository[T]) FindMany(ctx context.Context, filter bson.M, limit int)
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// BulkCreate inserts multiple documents
|
||||
func (r *repository[T]) BulkCreate(ctx context.Context, models []T) error {
|
||||
if len(models) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set timestamps for all models
|
||||
now := time.Now().Unix()
|
||||
for i := range models {
|
||||
if timestampable, ok := any(&models[i]).(Timestampable); ok {
|
||||
timestampable.SetCreatedAt(now)
|
||||
timestampable.SetUpdatedAt(now)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to interface slice for bulk insert
|
||||
documents := make([]interface{}, len(models))
|
||||
for i, model := range models {
|
||||
documents[i] = model
|
||||
}
|
||||
|
||||
result, err := r.collection.InsertMany(ctx, documents)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to bulk create documents", map[string]interface{}{
|
||||
"count": len(models),
|
||||
"error": err.Error(),
|
||||
})
|
||||
return fmt.Errorf("failed to bulk create documents: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("Documents bulk created successfully", map[string]interface{}{
|
||||
"count": len(result.InsertedIDs),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BulkUpdate updates multiple documents
|
||||
func (r *repository[T]) BulkUpdate(ctx context.Context, filter bson.M, update bson.M) error {
|
||||
result, err := r.collection.UpdateMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to bulk update documents", map[string]interface{}{
|
||||
"filter": filter,
|
||||
"update": update,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return fmt.Errorf("failed to bulk update documents: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("Documents bulk updated successfully", map[string]interface{}{
|
||||
"matched": result.MatchedCount,
|
||||
"modified": result.ModifiedCount,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BulkDelete removes multiple documents
|
||||
func (r *repository[T]) BulkDelete(ctx context.Context, filter bson.M) error {
|
||||
result, err := r.collection.DeleteMany(ctx, filter)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to bulk delete documents", map[string]interface{}{
|
||||
"filter": filter,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return fmt.Errorf("failed to bulk delete documents: %w", err)
|
||||
}
|
||||
|
||||
r.logger.Info("Documents bulk deleted successfully", map[string]interface{}{
|
||||
"deleted": result.DeletedCount,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
// validatePagination validates pagination parameters
|
||||
|
||||
Reference in New Issue
Block a user