saveOnTransaction method
void
saveOnTransaction(
- ModelTransactionRef ref,
- ModelAdapterDocumentQuery query,
- DynamicMap value
override
Describes the data storage process when performing a transaction.
The ModelTransactionRef created by runTransaction is passed to ref
and the query of the target document is passed to query
.
The data to be stored is passed to value
.
Keys with value
value of null
should be deleted from the database.
トランザクションを行う際のデータ保存処理を記述します。
ref
にrunTransactionで作成したModelTransactionRefが渡され、query
は対象のドキュメントのクエリが渡されます。
value
に保存するデータが渡されます。
value
の値にNullが入っているキーはデータベース上から削除するようにしてください。
Implementation
@override
void saveOnTransaction(
ModelTransactionRef ref,
ModelAdapterDocumentQuery query,
DynamicMap value,
) {
_assert();
if (ref is! FirestoreModelTransactionRef) {
throw Exception("[ref] is not [FirestoreModelTransactionRef].");
}
final converted = _convertTo(
value,
_FirestoreCache.getCache(options).get(_path(query.query.path)) ?? {},
);
ref._transaction.set(
database.doc(_path(query.query.path)),
converted,
SetOptions(merge: true),
);
ref._preLocalTransaction.add(() async {
if (validator != null) {
final oldValue =
_FirestoreCache.getCache(options).get(_path(query.query.path));
await validator!.onSaveDocument(
query,
oldValue: oldValue,
newValue: value,
);
}
});
ref._postLocalTransaction.add(() async {
if (validator != null) {
final oldValue =
_FirestoreCache.getCache(options).get(_path(query.query.path));
await validator!.onSaveDocument(
query,
oldValue: oldValue,
newValue: value,
);
}
await localDatabase.saveDocument(query, value, prefix: prefix);
_FirestoreCache.getCache(options).set(_path(query.query.path), value);
});
}