saveDoc<T extends YustDoc> method
Future<void>
saveDoc<T extends YustDoc>(
- YustDocSetup<
T> docSetup, - T doc, {
- bool? merge = true,
- bool? trackModification,
- bool skipOnSave = false,
- bool? removeNullValues,
- List<
String> ? updateMask, - bool skipLog = false,
- bool doNotCreate = false,
override
Saves a document.
If merge
is false a document with the same name
will be overwritten instead of trying to merge the data.
Use doNotCreate
to ensure that no new record is created
Implementation
@override
Future<void> saveDoc<T extends YustDoc>(
YustDocSetup<T> docSetup,
T doc, {
bool? merge = true,
bool? trackModification,
bool skipOnSave = false,
bool? removeNullValues,
List<String>? updateMask,
bool skipLog = false,
bool doNotCreate = false,
}) async {
await doc.onSave();
await prepareSaveDoc(docSetup, doc,
trackModification: trackModification, skipOnSave: skipOnSave);
final jsonDocs = _getJSONCollection(docSetup.collectionName);
final index = jsonDocs.indexWhere((d) => d['id'] == doc.id);
final docJsonClone = jsonDecode(jsonEncode(doc.toJson()));
if (index == -1 && !doNotCreate) {
jsonDocs.add(docJsonClone);
await onChange?.call(
_getParentPath(docSetup, doc: doc),
null,
docJsonClone,
);
dbLogCallback?.call(DatabaseLogAction.save, _getDocumentPath(docSetup), 1,
id: doc.id, updateMask: updateMask ?? []);
} else {
final oldDoc = jsonDecode(jsonEncode(jsonDocs[index]));
if (updateMask == null) {
jsonDocs[index] = docJsonClone;
await onChange?.call(
_getParentPath(docSetup, doc: doc),
oldDoc,
docJsonClone,
);
} else {
var jsonDoc = jsonDocs[index];
final newJsonDoc = docJsonClone;
for (final path in updateMask) {
final newValue = _readValueInJsonDoc(newJsonDoc, path);
_changeValueInJsonDoc(jsonDoc, newValue, path);
}
jsonDocs[index] = jsonDoc;
await onChange?.call(
_getParentPath(docSetup, doc: doc),
oldDoc,
jsonDoc,
);
}
dbLogCallback?.call(DatabaseLogAction.save, _getDocumentPath(docSetup), 1,
id: doc.id, updateMask: updateMask ?? []);
}
}