upsertBulk method

Future<Iterable<DtoChatItem>> upsertBulk(
  1. Iterable<DtoChatItem> items, {
  2. bool toView = false,
})

Creates or updates the provided items in the database.

If toView is true, then also adds the views to the ChatItemViews.

Implementation

Future<Iterable<DtoChatItem>> upsertBulk(
  Iterable<DtoChatItem> items, {
  bool toView = false,
}) async {
  for (var e in items) {
    _cache[e.value.id] = e;
  }

  final result = await safe((db) async {
    Log.debug(
      'upsertBulk(${items.length} items) toView($toView)',
      '$runtimeType',
    );

    await db.batch((batch) {
      for (var item in items) {
        final ChatItemRow row = item.toDb();
        batch.insert(db.chatItems, row, onConflict: DoUpdate((_) => row));
      }

      if (toView) {
        for (var item in items) {
          final ChatItemViewRow row = item.toView();
          batch.insert(db.chatItemViews, row, onConflict: DoNothing());
        }
      }
    });

    return items.toList();
  }, tag: 'chat_item.upsertBulk(${items.length} items, toView: $toView)');

  return result ?? items;
}