batchUpdateDoc method

Future<FeedbackResponse<WriteBatchWithReference?>> batchUpdateDoc({
  1. required Writeable writeable,
  2. required String id,
  3. WriteBatch? writeBatch,
  4. TimestampType timestampType = TimestampType.updated,
  5. String? collectionPathOverride,
})

Batch updates data based on given writeable and id.

Passing in a writeBatch will use that batch to add to it. If no batch is provided this method will create and return one.

The timestampType determines the type of automatically added _createdFieldName and/or _updatedFieldName field(s) of Timestamp. Pass in a TimestampType.none to avoid any of this automatic behaviour.

Implementation

Future<FeedbackResponse<WriteBatchWithReference?>> batchUpdateDoc({
  required Writeable writeable,
  required String id,
  WriteBatch? writeBatch,
  TimestampType timestampType = TimestampType.updated,
  String? collectionPathOverride,
}) async {
  assert(
    _isCollectionGroup == (collectionPathOverride != null),
    'Firestore does not support finding a document by id when communicating with a collection group, '
    'therefore, you must specify the collectionPathOverride containing all parent collection and document ids '
    'in order to make this method work.',
  );
  final isValidResponse = writeable.isValidResponse();
  try {
    if (isValidResponse.isSuccess) {
      _log.success(
        message: 'Writeable is valid!',
        sensitiveData: null,
      );
      _log.info(
        message: 'Updating document with batch..',
        sensitiveData: _shouldNotSensitiveInfo
            ? null
            : SensitiveData(
                path: collectionPathOverride ?? _collectionPath(),
                id: id,
                isBatch: writeBatch != null,
                updateTimeStampType: timestampType,
              ),
      );
      final nullSafeWriteBatch = writeBatch ?? this.writeBatch;
      final documentReference = findDocRef(id: id);
      _log.info(
        message: 'Creating JSON..',
        sensitiveData: null,
      );
      final writeableAsJson = timestampType.add(
        writeable.toJson(),
        createdFieldName: _createdFieldName,
        updatedFieldName: _updatedFieldName,
      );
      _log.info(
        message: 'Updating data with writeBatch.update..',
        sensitiveData: _shouldNotSensitiveInfo
            ? null
            : SensitiveData(
                path: collectionPathOverride ?? _collectionPath(),
                id: documentReference.id,
                data: writeableAsJson,
              ),
      );
      nullSafeWriteBatch.update(
        documentReference,
        writeableAsJson,
      );
      _log.success(
        message:
            'Adding update to batch done! Returning WriteBatchWithReference..',
        sensitiveData: null,
      );
      return FeedbackResponse.successNone(
        result: WriteBatchWithReference(
          writeBatch: nullSafeWriteBatch,
          documentReference: documentReference,
        ),
      );
    }
    _log.warning(
      message: 'Writeable was invalid!',
      sensitiveData: null,
    );
    return FeedbackResponse.error(
        title: isValidResponse.title, message: isValidResponse.message);
  } catch (error, stackTrace) {
    _log.error(
      message: 'Unable to update document with batch',
      sensitiveData: _shouldNotSensitiveError
          ? null
          : SensitiveData(
              path: collectionPathOverride ?? _collectionPath(),
              id: id,
            ),
      error: error,
      stackTrace: stackTrace,
    );
    return _responseConfig.updateFailedResponse(isPlural: writeBatch != null);
  }
}