save method

  1. @override
Future<E?> save(
  1. E entity, {
  2. bool isUpsert = false,
})
override

If the variable isUpsert = true, then it calls the upsert method. Else if the object contains an objectId, the update method is called, which updates an already existing object. Otherwise, the create method will be called, which creates a new object in the table.

Implementation

@override
Future<E?> save(E entity, {bool isUpsert = false}) async {
  Map<String, dynamic> map = reflector.serialize(entity)!;
  String methodName = '/data/$tableName';
  Map? result;

  if (isUpsert) {
    methodName += '/upsert';
  } else if (map.containsKey('objectId')) {
    methodName += '/${map['objectId']}';
  } else {
    result = await Invoker.post(methodName, entity);
  }

  result ??= await Invoker.put(methodName, map);

  return reflector.deserialize<E>(result!);
}