save method

  1. @override
Future<StudySubject> save({
  1. bool onlyUpdate = false,
})
override

Save the object to the database. By default, this will upsert the object, i.e. insert it if it does not exist, or update it if it does. If onlyUpdate is set to true, the object has to exist in the database, otherwise the result will be empty.

Implementation

@override
Future<StudySubject> save({bool onlyUpdate = false}) async {
  try {
    final tableQuery = env.client.from(tableName);
    final query = onlyUpdate
        ? tableQuery.update(toJson()).eq("id", id)
        : tableQuery.upsert(toJson());
    final response = await query.select();
    final json = toFullJson(
      partialJson: List<Map<String, dynamic>>.from(response).single,
    );
    final newSubject = StudySubject.fromJson(json);
    _controller.add(newSubject);
    // print("Saving study subject");
    return newSubject;
  } catch (e, stack) {
    SupabaseQuery.catchSupabaseException(e, stack);
    rethrow;
  }
}