writeBlobAttributesTransaction method

Transaction writeBlobAttributesTransaction({
  1. required String blobObjectId,
  2. required Map<String, String?> attributes,
  3. Map<String, String>? existingAttributes,
  4. Transaction? transaction,
})

Build a complete write-blob-attributes transaction.

Reads existing attributes from the blob and applies the given attributes map. If a value is null, the key is removed. If non-null, the key is inserted or updated.

If the blob has no metadata yet, creates a new empty metadata object and attaches it first.

Mirrors the TS SDK's #writeBlobAttributesForRef().

Implementation

Transaction writeBlobAttributesTransaction({
  required String blobObjectId,
  required Map<String, String?> attributes,
  Map<String, String>? existingAttributes,
  Transaction? transaction,
}) {
  final tx = transaction ?? Transaction();
  final blobObj = tx.object(blobObjectId);

  if (existingAttributes == null) {
    // No metadata exists yet — create and attach.
    final meta = createMetadata(tx: tx);
    addMetadata(tx: tx, blobObject: blobObj, metadata: meta);
  }

  for (final entry in attributes.entries) {
    if (entry.value == null) {
      // Remove this key (only if it existed).
      if (existingAttributes != null &&
          existingAttributes.containsKey(entry.key)) {
        removeMetadataPair(tx: tx, blobObject: blobObj, key: entry.key);
      }
    } else {
      insertOrUpdateMetadataPair(
        tx: tx,
        blobObject: blobObj,
        key: entry.key,
        value: entry.value!,
      );
    }
  }

  return tx;
}