getDeletePlan method

Future<CascadeDeletePreview?> getDeletePlan(
  1. String id, {
  2. String? userId,
})

Returns a visualization of the delete plan for the given entity. This is useful for previewing what will happen before deleting.

Implementation

Future<CascadeDeletePreview?> getDeletePlan(String id, {String? userId}) async {
  _ensureInitialized();

  // Check for entity existence (reuse read logic but lighter? No, need entity object for plan)
  final entity = await read(id, userId: userId);
  if (entity == null) return null;

  final analyticsBuilder = CascadeAnalyticsBuilder();
  analyticsBuilder.startOperation(dryRun: true);
  final plan = await _buildCascadeDeletePlan(entity, userId ?? entity.userId, analyticsBuilder);

  final stepsPreview = plan.steps.map((step) {
    String action = 'Delete';
    Map<String, dynamic>? details;

    if (step.type == _CascadeStepType.update) {
      action = 'SetNull';
      details = step.updateData;
    }

    return CascadeDeleteStepPreview(
      entityType: step.entity.runtimeType.toString(),
      entityId: step.entity.id,
      action: action,
      details: details,
    );
  }).toList();

  final warnings = <String>[];
  if (!plan.canDelete) {
    plan.restrictedRelations.forEach((relation, entities) {
      warnings.add('Cannot delete because of restrict constraint on relation "$relation" (${entities.length} items)');
    });
  }

  return CascadeDeletePreview(
    mainEntityType: T.toString(),
    mainEntityId: id,
    steps: stepsPreview,
    canDelete: plan.canDelete,
    warningMessages: warnings,
  );
}