mapFromCompanion method

Future<D> mapFromCompanion(
  1. Insertable<D> companion,
  2. DatabaseConnectionUser database

Converts a companion to the real model class, D.

Values that are Value.absent in the companion will be set to null. The database instance is used so that the raw values from the companion can properly be interpreted as the high-level Dart values exposed by the data class.


Future<D> mapFromCompanion(
    Insertable<D> companion, DatabaseConnectionUser database) async {
  final asColumnMap = companion.toColumns(false);

  if (asColumnMap.values.any((e) => e is! Variable)) {
    throw ArgumentError('The companion $companion cannot be transformed '
        'into a dataclass as it contains expressions that need to be '
        'evaluated by a database engine.');

  final context = GenerationContext.fromDb(database);
  final rawValues = asColumnMap
      .cast<String, Variable>()
      .map((key, value) => MapEntry(key, value.mapToSimpleValue(context)));

  return map(rawValues);