saveData method
Save data to local storage with version increment and hash generation
Implementation
Future<void> saveData({
required String collectionName,
required String recordId,
required String data,
}) async {
_ensureInitialized();
// Encrypt data if encryption is enabled
final dataToStore =
encryptionService != null ? encryptionService!.encryptData(data) : data;
await _isar.writeTxn(() async {
final existing = await _isar.dataRecords
.filter()
.collectionNameEqualTo(collectionName)
.recordIdEqualTo(recordId)
.findFirst();
final record = existing ?? DataRecord();
record.collectionName = collectionName;
record.recordId = recordId;
record.data = dataToStore;
record.updatedAt = DateTime.now();
if (existing == null) {
record.createdAt = DateTime.now();
record.version = 1; // Initial version
} else {
// Increment version on update
record.version += 1;
}
// Generate sync hash for change detection (on original data, not encrypted)
record.syncHash = _generateHash(data);
record.isSynced = false;
await _isar.dataRecords.put(record);
});
}