refresh method

Future<T> refresh({
  1. dynamic include,
  2. bool append = true,
})

Refresh model

Implementation

Future<T> refresh({dynamic include, bool append = true}) async {
  final pkValue = toMap()[primaryKey];

  if (pkValue == null) {
    throw Exception('Cannot refresh an unsaved model.');
  }

  Map<String, RelationScope> adjustedIncludes;
  if (include != null && append == true) {
    adjustedIncludes = mergeIncludes(include, loadedIncludes);
  } else if (include != null && append == false) {
    adjustedIncludes = mergeIncludes(include, {});
  } else {
    adjustedIncludes = loadedIncludes;
  }

  // Reload using the same relations that were previously loaded
  final fresh = await find(pkValue, include: adjustedIncludes);

  if (fresh == null) {
    throw Exception('Model no longer exists in database.');
  }

  return fresh;
}