introspect function

Future<Map<String, dynamic>> introspect(
  1. Queryable database, {
  2. List<String> includedSchemas = const [],
  3. List<String> excludedSchemas = const [],
})

Introspects the database behind database into the GeneratorMetadata document, in query order. Apply sortGeneratorMetadata before generating from it.

Port of introspect of @supabase/postgrest-typegen: the same queries with the same option combination, issued as one Queryable.query call, so the result holds the same records as the TypeScript output for the same database.

Implementation

Future<Map<String, dynamic>> introspect(
  Queryable database, {
  List<String> includedSchemas = const [],
  List<String> excludedSchemas = const [],
}) async {
  final systemExcludingFilter = filterByList(
    include: includedSchemas,
    exclude: excludedSchemas,
    defaultExclude: defaultSystemSchemas,
  );
  final plainFilter = filterByList(
    include: includedSchemas,
    exclude: excludedSchemas,
  );

  final results = await database.query({
    'schemas': schemasSql(schemaFilter: systemExcludingFilter),
    'tables': tablesSql(schemaFilter: systemExcludingFilter),
    'foreignTables': foreignTablesSql(schemaFilter: plainFilter),
    'views': viewsSql(schemaFilter: systemExcludingFilter),
    'materializedViews': materializedViewsSql(schemaFilter: plainFilter),
    'columns': columnsSql(schemaFilter: systemExcludingFilter),
    'primaryKeys': primaryKeysSql(schemaFilter: systemExcludingFilter),
    'tableRelationships': tableRelationshipsSql(
      schemaFilter: systemExcludingFilter,
    ),
    'viewsKeyDependencies': viewsKeyDependenciesSql(
      schemaFilter: systemExcludingFilter,
    ),
    'functions': functionsSql(schemaFilter: systemExcludingFilter),
    'types': typesSql,
  });
  List<Map<String, dynamic>> rows(String name) => results[name]!;

  final tableRelationships = rows('tableRelationships');
  final relationships = [
    ...tableRelationships,
    ...expandViewRelationships(
      tableRelationships,
      rows('viewsKeyDependencies'),
    ),
  ];

  return {
    'version': generatorMetadataVersion,
    'schemas': rows('schemas'),
    'tables': rows('tables'),
    'foreignTables': rows('foreignTables'),
    'views': rows('views'),
    'materializedViews': rows('materializedViews'),
    'columns': rows('columns'),
    'primaryKeys': rows('primaryKeys'),
    'relationships': relationships,
    'functions': [
      for (final function in rows('functions'))
        if (!const {
          'trigger',
          'event_trigger',
        }.contains(function['return_type']))
          function,
    ],
    'types': rows('types'),
  };
}