upsert method

Future<DtoUser> upsert(
  1. DtoUser user
)

Creates or updates the provided user in the database.

Implementation

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

  final result = await safe((db) async {
    final DtoUser stored = UserDb.fromDb(
      await db
          .into(db.users)
          .insertReturning(user.toDb(), mode: InsertMode.insertOrReplace),
    );

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

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

  _cache.remove(user.id);

  return result ?? user;
}