executeUpsert method

Future<int> executeUpsert({
  1. required String objectTypeName,
  2. required List<Map<String, dynamic>> entries,
})

You can invoke this method to write a group of objects to the current Cloud DB zone in batches. The write operation is atomic. That is, the input objects are either all successfully written or all fail to be written. If an object with the same primary key exists in the Cloud DB zone, the existing object data will be updated. Otherwise, a new object is written.

Returns the number of objects that are successfully written.

Comply with the following rules when invoking this method:

  • You can invoke this method to write object data only when Cloud DB zone is opened. Otherwise, the write operation will fail.
  • The data size of each object in the list must be less than or equal to 2 MB. Otherwise, the object data fails to be written.
  • The total number of data records in the list must be less than or equal to 1000. Otherwise, the write operation fails.
  • The data size of all objects in the list must be less than or equal to 2 MB only when the data synchronization attribute is cache and the data persistency attribute is non-persistent.

Implementation

Future<int> executeUpsert({
  required String objectTypeName,
  required List<Map<String, dynamic>> entries,
}) async {
  if (objectTypeName.isEmpty) {
    throw FormatException(
        'objectTypeName cannot be an empty string.', objectTypeName);
  }
  if (entries.isEmpty) {
    throw FormatException('entries cannot be an empty list.', entries);
  }
  try {
    final int? result = await _methodChannel.invokeMethod<int?>(
      _MethodConstants.EXECUTE_UPSERT,
      <String, dynamic>{
        _KeyConstants.ZONE_ID: _id,
        _KeyConstants.ZONE_OBJECT_TYPE_NAME: objectTypeName,
        _KeyConstants.ZONE_OBJECT_DATA_ENTRIES: entries,
      },
    );
    return result!;
  } catch (e) {
    throw AGConnectCloudDBException._from(e);
  }
}