updateDoc method
Future<FeedbackResponse<DocumentReference<Object?> > >
updateDoc({
- required Writeable writeable,
- required String id,
- WriteBatch? writeBatch,
- TimestampType timestampType = TimestampType.updated,
- String? collectionPathOverride,
- 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(
message: 'Checking if writeable is valid..',
sensitiveData: null,
);
final isValidResponse = writeable.isValidResponse();
if (isValidResponse.isSuccess) {
_log.success(message: 'Writeable is valid!', sensitiveData: null);
_log.info(
message: 'Updating document..',
sensitiveData: _shouldNotSensitiveInfo
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: id,
isBatch: writeBatch != null,
isTransaction: transaction != null,
updateTimeStampType: timestampType,
),
);
final DocumentReference documentReference;
if (writeBatch != null) {
_log.info(
message: 'WriteBatch was not null! Updating with batch..',
sensitiveData: null,
);
final lastBatchResponse = await batchUpdateDoc(
writeable: writeable,
id: id,
writeBatch: writeBatch,
timestampType: timestampType,
collectionPathOverride: collectionPathOverride,
);
_log.info(
message: 'Checking if batchUpdate was successful..',
sensitiveData: null,
);
if (lastBatchResponse.isSuccess) {
final writeBatchWithReference = lastBatchResponse.result!;
_log.info(
message: 'Last batch was added with success! Committing..',
sensitiveData: null,
);
await writeBatchWithReference.writeBatch.commit();
_log.success(
message: 'Committing writeBatch done!',
sensitiveData: null,
);
documentReference = writeBatchWithReference.documentReference;
} else {
_log.error(
message: 'Last batch failed!',
sensitiveData: null,
);
return _responseConfig.updateFailedResponse(isPlural: true);
}
} else {
_log.info(
message: 'WriteBatch was null! Updating without batch..',
sensitiveData: null,
);
documentReference = findDocRef(
id: id, collectionPathOverride: collectionPathOverride);
_log.info(
message: 'Creating JSON..',
sensitiveData: null,
);
final writeableAsJson = timestampType.add(
writeable.toJson(),
createdFieldName: _createdFieldName,
updatedFieldName: _updatedFieldName,
);
if (transaction == null) {
_log.info(
message: 'Updating data with documentReference.update..',
sensitiveData: _shouldNotSensitiveInfo
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: documentReference.id,
data: writeableAsJson,
),
);
await documentReference.update(writeableAsJson);
} else {
_log.info(
message: 'Updating data with transaction.update..',
sensitiveData: _shouldNotSensitiveInfo
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: documentReference.id,
data: writeableAsJson,
),
);
transaction.update(
findDocRef(id: documentReference.id), writeableAsJson);
}
}
_log.success(
message: 'Updating data done!',
sensitiveData: null,
);
return _responseConfig.updateSuccessResponse(
isPlural: writeBatch != null,
result: 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',
sensitiveData: _shouldNotSensitiveError
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: id,
isBatch: writeBatch != null,
updateTimeStampType: timestampType,
),
error: error,
stackTrace: stackTrace,
);
return _responseConfig.updateFailedResponse(isPlural: writeBatch != null);
}
}