upsert<_Model extends RestModel> method

  1. @override
Future<Response?> upsert<_Model extends RestModel>(
  1. _Model instance, {
  2. Query? query,
  3. ModelRepository<RestModel>? repository,
})

Query's providerArgs can extend the upsert functionality:

  • 'headers' (Map<String, String>) set HTTP headers
  • 'request' (String) specifies HTTP method. Defaults to POST
  • 'topLevelKey' (String) includes the serialized payload beneath a JSON key (For example, {"user": {"id"...}})
  • 'supplementalTopLevelData' (Map<String, dynamic>) this map is merged alongside the topLevelKey in the payload. For example, given 'supplementalTopLevelData': {'other_key': true} {"topLevelKey": ..., "other_key": true}. It is strongly recommended to avoid using this property. Your data should be managed at the model level, not the query level.

It is recommended to use RestSerializable#toKey instead to simplify queries (however, when defined, topLevelKey is prioritized).

Implementation

@override
Future<http.Response?> upsert<_Model extends RestModel>(instance, {query, repository}) async {
  final adapter = modelDictionary.adapterFor[_Model]!;
  final body = await adapter.toRest(instance, provider: this, repository: repository);

  final url = urlForModel<_Model>(query, instance);
  if (url == null) return null;

  final resp = await _sendUpsertResponse(Uri.parse(url), body, query, adapter.toKey);

  logger.finest('#upsert: url=$url statusCode=${resp.statusCode} body=${resp.body}');

  if (statusCodeIsSuccessful(resp.statusCode)) {
    return resp;
  } else {
    logger.warning('#upsert: url=$url statusCode=${resp.statusCode} body=${resp.body}');
    throw RestException(resp);
  }
}