upsert method

Future<DtoSettings> upsert(
  1. UserId userId,
  2. DtoSettings settings
)

Creates or updates the provided settings in the database.

Implementation

Future<DtoSettings> upsert(UserId userId, DtoSettings settings) async {
  _cache[userId] = settings;

  final result = await safe((db) async {
    final DtoSettings stored = _SettingsDb.fromDb(
      await db
          .into(db.settings)
          .insertReturning(
            settings.toDb(userId),
            mode: InsertMode.insertOrReplace,
          ),
    );

    _controllers[userId]?.add(stored);

    return stored;
  });

  return result ?? settings;
}