upsertBulk method

Future<Iterable<DtoChat>> upsertBulk(
  1. Iterable<DtoChat> items
)

Creates or updates the provided items in the database.

Implementation

Future<Iterable<DtoChat>> upsertBulk(Iterable<DtoChat> items) async {
  for (var e in items) {
    _cache[e.id] = e;
  }

  for (var e in items) {
    _controllers[e.id]?.add(e);
  }

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

    await db.batch((batch) {
      for (var item in items) {
        final ChatRow row = item.toDb();
        batch.insert(db.chats, row, mode: InsertMode.insertOrReplace);
      }
    });

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

  for (var e in items) {
    _cache.remove(e.value.id);
  }

  return result ?? items;
}