upsert method

Future<Credentials> upsert(
  1. Credentials creds
)

Creates or updates the provided creds in the database.

Implementation

Future<Credentials> upsert(Credentials creds) async {
  data[creds.userId] = creds;

  final result = await safe<Credentials?>((db) async {
    final Credentials stored = _CredentialsDb.fromDb(
      await db
          .into(db.tokens)
          .insertReturning(
            creds.toDb(),
            onConflict: DoUpdate((_) => creds.toDb()),
          ),
    );

    return stored;
  });

  return result ?? creds;
}