loadCollection method

  1. @override
Future<Map<String, DynamicMap>> loadCollection(
  1. ModelAdapterCollectionQuery query
)
override

Pass query to the platform set by the adapter to retrieve the collection.

All return values ​​will be Map<String, DynamicMap>. String contains the ID (not the path) of each document.

アダプターで設定されたプラットフォームにqueryを渡してコレクションを取得します。

戻り値はすべてMap<String, DynamicMap>になります。Stringには各ドキュメントのID(パスではない)が入ります。

Implementation

@override
Future<Map<String, DynamicMap>> loadCollection(
  ModelAdapterCollectionQuery query,
) async {
  _assert();
  if (validator != null) {
    await validator!.onPreloadCollection(query);
  }
  if (onInitialize != null) {
    await onInitialize?.call(options);
  } else {
    await FirebaseCore.initialize(options: options);
  }
  final snapshot = await Future.wait<QuerySnapshot<DynamicMap>>(
    _collectionReference(query).map((reference) => reference.get()),
  );
  final res = snapshot.expand((e) => e.docChanges).toMap(
        (e) => MapEntry(e.doc.id, _convertFrom(e.doc.data()?.cast() ?? {})),
      );
  final localRes =
      await localDatabase.getInitialCollection(query, prefix: prefix);
  if (localRes.isNotEmpty) {
    for (final entry in localRes!.entries) {
      if (res.containsKey(entry.key)) {
        continue;
      }
      if (!query.query.hasMatchAsMap(entry.value)) {
        continue;
      }
      res[entry.key] = entry.value;
    }
  }
  if (validator != null) {
    await validator!.onPostloadCollection(query, res);
  }
  await localDatabase.syncCollection(query, res, prefix: prefix);
  for (final doc in res.entries) {
    _FirestoreCache.getCache(options).set(
      "${_path(query.query.path)}/${doc.key}",
      doc.value,
    );
  }
  return res;
}