many<T> method

Future<void> many<T>(
  1. List<T> objects, {
  2. String? subcollection,
})

Creates multiple documents in Firestore from a list of objects. Uses a batch operation for efficiency.

Implementation

Future<void> many<T>(List<T> objects, { String? subcollection }) {
  if (objects.length > 500) {
    throw ArgumentError('Batch limit exceeded. Maximum 500 objects allowed.');
  }
  if (objects.isEmpty) {
   return Future.value();
  }

  final serializer = FS.serializers[objects[0].runtimeType];

  if (serializer == null) {
    throw UnsupportedError('No serializer found for type: ${objects[0].runtimeType}. Consider re-generating Firestorm data classes.');
  }

  WriteBatch batch = FS.instance.batch();
  for (var object in objects) {

    final map = serializer(object);
    if (map["id"].isEmpty) {
      throw NullIDException(map);
    }

    DocumentReference documentReference = FS.instance.collection(T.toString()).doc(map["id"]);
    if (subcollection != null) {
      documentReference = FS.instance.collection(T.toString()).doc(subcollection).collection(subcollection).doc(map["id"]);
    }

    batch.set(documentReference, serializer(object));
  }
  return batch.commit();
}