DatabaseSwagger.fromSchemas constructor

DatabaseSwagger.fromSchemas(
  1. Map<String, Map<String, dynamic>> specs,
  2. Map<String, List<String>> configEnums,
  3. bool jsonbToDynamic, {
  4. Map<String, JsonbModelConfig>? jsonbModels,
})

Parses the PostgREST schema of each database schema in specs, keyed by schema name. The first is the primary schema. configEnums are the enums from supadart.yaml, keyed by type name, which is taken to be in the primary schema unless qualified (inventory.status).

Implementation

factory DatabaseSwagger.fromSchemas(Map<String, Map<String, dynamic>> specs,
    Map<String, List<String>> configEnums, bool jsonbToDynamic,
    {Map<String, JsonbModelConfig>? jsonbModels}) {
  final schemas = Schemas(specs.keys.toList());

  final configured = {
    for (final e in configEnums.entries)
      if (e.value.isNotEmpty) schemas.qualify(e.key): e.value
  };
  // PostgREST includes the values of enum columns, but not enum arrays.
  final fromSchema = <String, List<String>>{};
  for (final spec in specs.values) {
    for (final table in (spec['definitions'] as Map).values) {
      for (final column in (table['properties'] as Map).values) {
        final typeName = schemas.userTypeName(column['format']);
        if (typeName != null && column['enum'] != null) {
          fromSchema[typeName] = List<String>.from(column['enum']);
        }
      }
    }
  }
  final merged = {...configured, ...fromSchema};
  final enums = {
    for (final name in merged.keys.toList()..sort()) name: merged[name]!
  };

  final tables = [
    for (final MapEntry(key: schema, value: spec) in specs.entries)
      for (final MapEntry(key: name, value: json)
          in (spec['definitions'] as Map<String, dynamic>).entries)
        Table.fromJson(name, json, enums, jsonbToDynamic,
            schema: schema, schemas: schemas, jsonbModels: jsonbModels),
  ];

  return DatabaseSwagger(tables, schemas: schemas, enums: enums, warnings: [
    ..._configMismatches(configured, fromSchema, schemas),
    ..._unresolvedTypes(tables, schemas),
  ]);
}