fix(persistence): change mode insert or replace to upsert

This commit is contained in:
Dmitry Zhifarsky
2021-08-08 21:42:27 +03:00
parent 70fdd42e58
commit 8adc38f8e4
9 changed files with 9 additions and 18 deletions
@@ -42,10 +42,9 @@ class ChannelDao extends DatabaseAccessor<MoorChatDatabase>
/// Updates all the channels using the new [channelList] data /// Updates all the channels using the new [channelList] data
Future<void> updateChannels(List<ChannelModel> channelList) => batch( Future<void> updateChannels(List<ChannelModel> channelList) => batch(
(it) => it.insertAll( (it) => it.insertAllOnConflictUpdate(
channels, channels,
channelList.map((c) => c.toEntity()).toList(), channelList.map((c) => c.toEntity()).toList(),
mode: InsertMode.insertOrReplace,
), ),
); );
} }
@@ -45,13 +45,12 @@ class ChannelQueryDao extends DatabaseAccessor<MoorChatDatabase>
} }
await batch((it) { await batch((it) {
it.insertAll( it.insertAllOnConflictUpdate(
channelQueries, channelQueries,
cids cids
.map((cid) => .map((cid) =>
ChannelQueryEntity(queryHash: hash, channelCid: cid)) ChannelQueryEntity(queryHash: hash, channelCid: cid))
.toList(), .toList(),
mode: InsertMode.insertOrReplace,
); );
}); });
}); });
@@ -26,7 +26,7 @@ class ConnectionEventDao extends DatabaseAccessor<MoorChatDatabase>
/// Update stored connection event with latest data /// Update stored connection event with latest data
Future<int> updateConnectionEvent(Event event) => transaction(() async { Future<int> updateConnectionEvent(Event event) => transaction(() async {
final connectionInfo = await select(connectionEvents).getSingleOrNull(); final connectionInfo = await select(connectionEvents).getSingleOrNull();
return into(connectionEvents).insert( return into(connectionEvents).insertOnConflictUpdate(
ConnectionEventEntity( ConnectionEventEntity(
id: 1, id: 1,
type: event.type, type: event.type,
@@ -38,7 +38,6 @@ class ConnectionEventDao extends DatabaseAccessor<MoorChatDatabase>
unreadChannels: unreadChannels:
event.unreadChannels ?? connectionInfo?.unreadChannels, event.unreadChannels ?? connectionInfo?.unreadChannels,
), ),
mode: InsertMode.insertOrReplace,
); );
}); });
@@ -32,10 +32,9 @@ class MemberDao extends DatabaseAccessor<MoorChatDatabase>
/// Updates all the members using the new [memberList] data /// Updates all the members using the new [memberList] data
Future<void> updateMembers(String cid, List<Member> memberList) async => Future<void> updateMembers(String cid, List<Member> memberList) async =>
batch( batch(
(it) => it.insertAll( (it) => it.insertAllOnConflictUpdate(
members, members,
memberList.map((m) => m.toEntity(cid: cid)).toList(), memberList.map((m) => m.toEntity(cid: cid)).toList(),
mode: InsertMode.insertOrReplace,
), ),
); );
@@ -171,10 +171,9 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
/// the new [messageList] data /// the new [messageList] data
Future<void> updateMessages(String cid, List<Message> messageList) => batch( Future<void> updateMessages(String cid, List<Message> messageList) => batch(
(batch) { (batch) {
batch.insertAll( batch.insertAllOnConflictUpdate(
messages, messages,
messageList.map((it) => it.toEntity(cid: cid)).toList(), messageList.map((it) => it.toEntity(cid: cid)).toList(),
mode: InsertMode.insertOrReplace,
); );
}, },
); );
@@ -170,10 +170,9 @@ class PinnedMessageDao extends DatabaseAccessor<MoorChatDatabase>
/// the new [messageList] data /// the new [messageList] data
Future<void> updateMessages(String cid, List<Message> messageList) => batch( Future<void> updateMessages(String cid, List<Message> messageList) => batch(
(batch) { (batch) {
batch.insertAll( batch.insertAllOnConflictUpdate(
pinnedMessages, pinnedMessages,
messageList.map((it) => it.toPinnedEntity(cid: cid)).toList(), messageList.map((it) => it.toPinnedEntity(cid: cid)).toList(),
mode: InsertMode.insertOrReplace,
); );
}, },
); );
@@ -42,10 +42,9 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
/// Updates the reactions data with the new [reactionList] data /// Updates the reactions data with the new [reactionList] data
Future<void> updateReactions(List<Reaction> reactionList) => batch((it) { Future<void> updateReactions(List<Reaction> reactionList) => batch((it) {
it.insertAll( it.insertAllOnConflictUpdate(
reactions, reactions,
reactionList.map((r) => r.toEntity()).toList(), reactionList.map((r) => r.toEntity()).toList(),
mode: InsertMode.insertOrReplace,
); );
}); });
@@ -30,10 +30,9 @@ class ReadDao extends DatabaseAccessor<MoorChatDatabase> with _$ReadDaoMixin {
/// Updates the read data of a particular channel with /// Updates the read data of a particular channel with
/// the new [readList] data /// the new [readList] data
Future<void> updateReads(String cid, List<Read> readList) => batch( Future<void> updateReads(String cid, List<Read> readList) => batch(
(it) => it.insertAll( (it) => it.insertAllOnConflictUpdate(
reads, reads,
readList.map((r) => r.toEntity(cid: cid)).toList(), readList.map((r) => r.toEntity(cid: cid)).toList(),
mode: InsertMode.insertOrReplace,
), ),
); );
} }
@@ -14,10 +14,9 @@ class UserDao extends DatabaseAccessor<MoorChatDatabase> with _$UserDaoMixin {
/// Updates the users data with the new [userList] data /// Updates the users data with the new [userList] data
Future<void> updateUsers(List<User> userList) => batch( Future<void> updateUsers(List<User> userList) => batch(
(it) => it.insertAll( (it) => it.insertAllOnConflictUpdate(
users, users,
userList.map((u) => u.toEntity()).toList(), userList.map((u) => u.toEntity()).toList(),
mode: InsertMode.insertOrReplace,
), ),
); );