saveDocument method

Future<void> saveDocument(
  1. ModelAdapterDocumentQuery query,
  2. DynamicMap value, {
  3. String? prefix,
})

Update and add data to value by passing query and the data in the document corresponding to query.

When changes are made, notifications are sent to documents and collections already registered for monitoring according to the data in query.

You can also register a callback for NoSqlDatabase.onSaved to add processing such as writing as a file after saving.

Keys with value value of Null will be deleted. If all keys are deleted, the document itself will be deleted.

prefix can be specified to prefix the path.

queryを渡してqueryに対応するドキュメントのデータをvalueに更新・追加します。

変更点があった場合、queryのデータに応じてすでに監視対象に登録されているドキュメントやコレクションに通知を送信します。

また、NoSqlDatabase.onSavedのコールバックを登録しておくことで保存後にファイルとして書き出すなどの処理を追加することができます。

valueの値にNullが入っているキーは削除されます。すべてのキーが削除された場合、ドキュメント自体が削除されます。

prefixを指定するとパスにプレフィックスを付与可能です。

Implementation

Future<void> saveDocument(
  ModelAdapterDocumentQuery query,
  DynamicMap value, {
  String? prefix,
}) async {
  _addDocumentListener(query, prefix: prefix);
  await _initialize();
  final trimPath = _path(query.query.path, prefix);
  final paths = trimPath.split("/");
  if (paths.isEmpty) {
    return;
  }
  value = Map.from(value)..removeWhere((key, value) => value == null);
  if (value.isEmpty) {
    return deleteDocument(query, prefix: prefix);
  }
  final isAdd = data._writeToPath(paths, 0, value);
  if (isAdd == null) {
    return;
  }
  notifyDocuments(
    trimPath,
    paths.last,
    value,
    isAdd
        ? ModelUpdateNotificationStatus.added
        : ModelUpdateNotificationStatus.modified,
    query,
  );
  await onSaved?.call(this);
}