upsert method

Future<DtoChat> upsert(
  1. DtoChat chat, {
  2. bool force = false,
})

Creates or updates the provided chat in the database.

Implementation

Future<DtoChat> upsert(DtoChat chat, {bool force = false}) async {
  Log.debug('upsert($chat)');

  _cache[chat.id] = chat;
  _controllers[chat.id]?.add(chat);

  final result = await safe(
    (db) async {
      final ChatRow row = chat.toDb();
      final DtoChat stored = _ChatDb.fromDb(
        await db
            .into(db.chats)
            .insertReturning(row, mode: InsertMode.insertOrReplace),
      );

      return stored;
    },
    tag: 'chat.upsert()',
    force: force,
  );

  _cache.remove(chat.id);

  return result ?? chat;
}