upsert method

  1. @override
Future<void> upsert(
  1. DatabaseData data
)
override

Insert or update data in storage.

Insert data if it does not exist in the storage, otherwise update the existing data.

Implementation

@override
Future<void> upsert(DatabaseData data) async {
  final db = await _getDb();
  await db.transaction((txn) async {
    final maps = await _queryByKeyAndParams(txn, data.key, data.queryParams);
    if (maps.isEmpty) {
      await txn.insert(_cacheTable, data.toJson());
    } else {
      final id = maps.first['_id'];
      await _updateData(txn, data.copyWith(id: id));
    }
  });
}