syncCollection method
Future<Map<String, DynamicMap> ?>
syncCollection(
- ModelAdapterCollectionQuery query,
- Map<
String, DynamicMap> ? value, { - String? prefix,
Stores value
in the path corresponding to query
and then returns value
.
Used to synchronize NoSqlDatabase with other databases. (Strictly speaking, the given value is stocked, not synchronized.)
prefix
can be specified to prefix the path.
query
に対応するパスにvalue
を格納してからvalue
を返します。
他のデータベースとNoSqlDatabaseを同期するために利用します。 (厳密には同期ではなく与えられた値をストックしていきます。)
prefix
を指定するとパスにプレフィックスを付与可能です。
Implementation
Future<Map<String, DynamicMap>?> syncCollection(
ModelAdapterCollectionQuery query,
Map<String, DynamicMap>? value, {
String? prefix,
}) async {
_addCollectionListener(query, prefix: prefix);
await _initialize();
final trimPath = _path(query.query.path, prefix);
final paths = trimPath.split("/");
if (paths.isEmpty || value == null) {
return value;
}
final isCollectionGroup = query.query.isCollectionGroup;
for (final tmp in value.entries) {
final key = tmp.key;
final val = Map<String, dynamic>.from(tmp.value);
data._writeToPath([...paths, key], 0, val);
}
final newValue = data._readFromPath(paths, 0);
if (newValue is! DynamicMap) {
return null;
}
final limitValue = query.query.filters
.firstWhereOrNull((e) => e.type == ModelQueryFilterType.limit)
?.value as int?;
final entries = query.query.sort(
newValue
.toList(
(key, value) {
// ignore: unnecessary_type_check
if (value is! Map) {
return null;
}
return MapEntry(
key,
Map<String, dynamic>.from(value),
);
},
)
.where(
(element) =>
element != null && query.query.hasMatchAsMap(element.value),
)
.removeEmpty(),
);
final limited = entries.sublist(
0,
limitValue != null ? min(limitValue, entries.length) : null,
);
if (isCollectionGroup) {
_collectionGroupEntries[query.origin] = List.from(limited);
} else {
_collectionEntries[query.origin] = List.from(limited);
}
await onSaved?.call(this);
return limited.toMap((item) => MapEntry(item.key, item.value));
}