batchCreateDoc method
- required Writeable writeable,
- String? id,
- WriteBatch? writeBatch,
- TimestampType createTimeStampType = TimestampType.createdAndUpdated,
- TimestampType updateTimeStampType = TimestampType.updated,
- bool merge = false,
- List<
FieldPath> ? mergeFields, - String? collectionPathOverride,
Batch creates/writes data based on given writeable
.
Passing in an id
will give your document that 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 createTimeStampType
determines the type of automatically added _createdFieldName
and/or
_updatedFieldName
field(s) of Timestamp
when merge
is false. Pass in a TimestampType.none
to avoid any of this automatic behaviour.
The updateTimeStampType
determines the type of automatically added _createdFieldName
and/or
_updatedFieldName
field(s) of Timestamp
when merge
is true or mergeFields
!= null.
Pass in a TimestampType.none to avoid any of this automatic behaviour.
When merge
is true this method will attempt an upsert if the document exists. If the
document does not exist it will default to a regular create.
If addIdAsField
is true it will automatically add the ID (given as id
or generated) as a
field to your document. The field name will be what's specified in _idFieldName
.
The mergeFields
determine which fields to upsert, leave blank to upsert the entire object.
Implementation
Future<FeedbackResponse<WriteBatchWithReference?>> batchCreateDoc({
required Writeable writeable,
String? id,
WriteBatch? writeBatch,
TimestampType createTimeStampType = TimestampType.createdAndUpdated,
TimestampType updateTimeStampType = TimestampType.updated,
bool merge = false,
List<FieldPath>? mergeFields,
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.',
);
try {
final isValidResponse = writeable.isValidResponse();
if (isValidResponse.isSuccess) {
_log.success(message: 'Writeable is valid!', sensitiveData: null);
_log.info(
message: 'Creating document with batch..',
sensitiveData: _shouldNotSensitiveInfo
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: id,
isBatch: writeBatch != null,
createTimeStampType: createTimeStampType,
updateTimeStampType: updateTimeStampType,
isMerge: merge,
mergeFields: mergeFields,
),
);
final nullSafeWriteBatch = writeBatch ?? this.writeBatch;
final documentReference = id != null
? findDocRef(id: id, collectionPathOverride: collectionPathOverride)
: _firebaseFirestore
.collection(collectionPathOverride ?? _collectionPath())
.doc();
_log.info(message: 'Creating JSON..', sensitiveData: null);
final writeableAsJson = (merge || mergeFields != null) &&
(await documentReference.get(_getOptions)).exists
? updateTimeStampType.add(
writeable.toJson(),
updatedFieldName: _updatedFieldName,
createdFieldName: _createdFieldName,
)
: createTimeStampType.add(
writeable.toJson(),
createdFieldName: _createdFieldName,
updatedFieldName: _updatedFieldName,
);
_log.info(
message: 'Setting data with writeBatch.set..',
sensitiveData: _shouldNotSensitiveInfo
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: documentReference.id,
data: writeableAsJson,
),
);
nullSafeWriteBatch.set(
documentReference,
writeableAsJson,
SetOptions(
merge: mergeFields == null ? merge : null,
mergeFields: mergeFields,
),
);
_log.success(
message:
'Adding create 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 create document with batch',
sensitiveData: _shouldNotSensitiveError
? null
: SensitiveData(
path: collectionPathOverride ?? _collectionPath(),
id: id,
isBatch: writeBatch != null,
createTimeStampType: createTimeStampType,
updateTimeStampType: updateTimeStampType,
isMerge: merge,
mergeFields: mergeFields,
),
error: error,
stackTrace: stackTrace,
);
return _responseConfig.createFailedResponse(isPlural: true);
}
}