updateDoc method

Future<FeedbackResponse<DocumentReference<Object?>>> updateDoc({
  1. required Writeable writeable,
  2. required String id,
  3. WriteBatch? writeBatch,
  4. TimestampType timestampType = TimestampType.updated,
  5. String? collectionPathOverride,
  6. Transaction? transaction,
})

Updates data based on given writeable and id.

Passing in a writeBatch will close the WriteBatch and perform the last commit. If you want to add more to your WriteBatch then use the batchUpdateDoc method instead.

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<DocumentReference>> updateDoc({
  required Writeable writeable,
  required String id,
  WriteBatch? writeBatch,
  TimestampType timestampType = TimestampType.updated,
  String? collectionPathOverride,
  Transaction? transaction,
}) 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.',
  );
  try {
    _log.info('🔥 Checking if writeable is valid..');
    final isValidResponse = writeable.isValidResponse();
    if (isValidResponse.isSuccess) {
      _log.success('🔥 Writeable is valid!');
      _log.info('🔥 '
          'Updating ${collectionPathOverride ?? _collectionPath()} document with '
          'writeable: $writeable, '
          'id: $id, '
          'writeBatch: $writeBatch, '
          'timestampType: ${timestampType.name}..');
      final DocumentReference documentReference;
      if (writeBatch != null) {
        _log.info('🔥 WriteBatch was not null! Updating with batch..');
        final lastBatchResponse = await batchUpdateDoc(
          writeable: writeable,
          id: id,
          writeBatch: writeBatch,
          timestampType: timestampType,
          collectionPathOverride: collectionPathOverride,
        );
        _log.info('🔥 Checking if batchUpdate was successful..');
        if (lastBatchResponse.isSuccess) {
          final writeBatchWithReference = lastBatchResponse.result!;
          _log.info('🔥 Last batch was added with success! Committing..');
          await writeBatchWithReference.writeBatch.commit();
          _log.success('🔥 Committing writeBatch done!');
          documentReference = writeBatchWithReference.documentReference;
        } else {
          _log.error('🔥 Last batch failed!');
          return _responseConfig.updateFailedResponse(isPlural: true);
        }
      } else {
        _log.info('🔥 WriteBatch was null! Updating without batch..');
        documentReference = findDocRef(
            id: id, collectionPathOverride: collectionPathOverride);
        _log.value(documentReference.id, '🔥 Document ID');
        _log.info('🔥 Creating JSON..');
        final writeableAsJson = timestampType.add(
          writeable.toJson(),
          createdFieldName: _createdFieldName,
          updatedFieldName: _updatedFieldName,
        );
        _log.value(writeableAsJson, 'JSON');
        if (transaction == null) {
          _log.info('🔥 Updating data with documentReference.update..');
          await documentReference.update(writeableAsJson);
        } else {
          _log.info('🔥 Updating data with transaction.update..');
          transaction.update(
              findDocRef(id: documentReference.id), writeableAsJson);
        }
      }
      _log.success('🔥 Updating data done!');
      return _responseConfig.updateSuccessResponse(
        isPlural: writeBatch != null,
        result: documentReference,
      );
    }
    _log.warning('🔥 Writeable was invalid!');
    return FeedbackResponse.error(
      title: isValidResponse.title,
      message: isValidResponse.message,
    );
  } catch (error, stackTrace) {
    _log.error(
      '🔥 '
      'Unable to update ${collectionPathOverride ?? _collectionPath()} document with '
      'id: $id, '
      'writeBatch: $writeBatch, '
      'timeStampType: ${timestampType.name}..',
      error: error,
      stackTrace: stackTrace,
    );
    return _responseConfig.updateFailedResponse(isPlural: writeBatch != null);
  }
}