updateOrCreate method

  1. @override
Future<int> updateOrCreate({
  1. required Map<String, Object?> check,
  2. required Map<String, Object?> inserts,
})

Update data if exists and if not, create new row.

var userEloquent = UserEloquent();

// if row where name is john exists, update 'password' column. If not, create row where name is john and password is 'pass'.
userEloquent.updateOrCreate(check:{'name':'john'},inserts:{'password':'pass'});

Implementation

@override
Future<int> updateOrCreate(
    {required Map<String, Object?> check,
    required Map<String, Object?> inserts}) async {
  final db = await getDatabase;
  List checkResult = await _where(check);
  if (checkResult.isNotEmpty) {
    var where = '';
    var whereArgs = [];
    check.forEach((key, value) {
      where = where == '' ? key + ' = ?' : where + ' and ' + key + ' = ?';
      whereArgs.add(value);
    });
    await db.update(
        tableName,
        Map.fromEntries(
            inserts.entries.where((element) => element.key != 'isFavourite')),
        where: where,
        whereArgs: whereArgs);
    return 0;
  }
  resetAll();
  return await db.insert(tableName, {...check, ...inserts});
}