OrmProjectConfig.fromMap constructor

OrmProjectConfig.fromMap(
  1. Map<String, Object?> data
)

Rebuilds the config tree from decoded YAML.

Implementation

factory OrmProjectConfig.fromMap(Map<String, Object?> data) {
  final defaultConnection = (data['default_connection'] as String?)
      ?.trim()
      .notEmptyOrNull;
  final definitions = <String, ConnectionDefinition>{};

  final connectionsBlock = data['connections'];
  if (connectionsBlock is Map) {
    for (final entry in connectionsBlock.entries) {
      final key = entry.key?.toString().trim();
      if (key == null || key.isEmpty) continue;
      final block = _flatten(entry.value);
      definitions[key] = ConnectionDefinition.fromMap(key, block);
    }
  }

  if (definitions.isEmpty) {
    definitions['default'] = ConnectionDefinition.fromMap('default', data);
  }

  final effectiveDefault =
      defaultConnection ??
      (definitions.length == 1 ? definitions.keys.first : null);
  if (effectiveDefault == null) {
    throw StateError(
      'Multiple connections configured but default_connection is missing.',
    );
  }
  if (!definitions.containsKey(effectiveDefault)) {
    throw StateError(
      'default_connection "$effectiveDefault" is not defined.',
    );
  }

  return OrmProjectConfig(
    connections: definitions,
    activeConnectionName: effectiveDefault,
    defaultConnectionName: defaultConnection,
  );
}