createDocs method
Future<TurboResponse<List<T> > >
createDocs({
- Transaction? transaction,
- required List<
CreateDocDef< docs,T> > - bool doNotifyListeners = true,
Creates multiple documents both locally and in Firestore.
Performs optimistic creates by updating the local state first, then syncing with Firestore. Uses a batch operation for multiple documents unless a transaction is provided.
Parameters:
transaction
- Optional transaction for atomic operationsids
- The IDs of the documents to createdoc
- The definition of how to create the documentsdoNotifyListeners
- Whether to notify listeners of the changes
Returns a TurboResponse
indicating success or failure
Implementation
@protected
Future<TurboResponse<List<T>>> createDocs({
Transaction? transaction,
required List<CreateDocDef<T>> docs,
bool doNotifyListeners = true,
}) async {
try {
final pDocs = createLocalDocs(
docs: docs,
doNotifyListeners: doNotifyListeners,
);
log.debug('Creating ${pDocs.length} docs');
if (transaction != null) {
for (final pDoc in pDocs) {
(await api.createDoc(
id: pDoc.id,
transaction: transaction,
writeable: pDoc,
))
.throwWhenFail();
}
return TurboResponse.success(result: pDocs);
} else {
final batch = api.writeBatch;
for (final pDoc in pDocs) {
await api.createDocInBatch(
id: pDoc.id,
writeBatch: batch,
writeable: pDoc,
);
}
final future = batch.commit();
await future;
return TurboResponse.success(result: pDocs);
}
} catch (error, stackTrace) {
if (transaction != null) rethrow;
log.error(
'${error.runtimeType} caught while creating docs',
error: error,
stackTrace: stackTrace,
);
return TurboResponse.fail(error: error);
}
}