upsert method

Future<DtoMyUser> upsert(
  1. DtoMyUser user
)

Creates or updates the provided user in the database.

Implementation

Future<DtoMyUser> upsert(DtoMyUser user) async {
  _cache[user.id] = user;

  final result = await safe(
    (db) async {
      try {
        final DtoMyUser stored = _MyUserDb.fromDb(
          await db
              .into(db.myUsers)
              .insertReturning(user.toDb(), mode: InsertMode.insertOrReplace),
        );

        _controllers[stored.id]?.add(stored);

        return stored;
      } on DriftRemoteException {
        // No-op, might be thrown after E2E tests completion.
      }
    },
    tag: 'my_user.upsert(user)',
    exclusive: false,
  );

  return result ?? user;
}