tursoCompatibleSchemaManifest function

Map<String, dynamic> tursoCompatibleSchemaManifest(
  1. Map schema, {
  2. required String columnType(
    1. String type
    ),
  3. bool include(
    1. String database
    )?,
})

Converts the common schema schema into the Turso schema manifest format, keeping only the tables whose database satisfies include.

共通schema schemaをTursoのschema manifest形式に変換し、 databaseがincludeを満たすテーブルだけを残します。

Implementation

Map<String, dynamic> tursoCompatibleSchemaManifest(
  Map schema, {
  required String Function(String type) columnType,
  bool Function(String database)? include,
}) {
  bool accepts(Object? database) =>
      include == null || include(database?.toString() ?? "");
  final tables = schema["tables"];
  if (tables is List) {
    return {
      "version": schema["sourceHash"] ?? schema["version"],
      "tables": {
        for (final table in tables)
          if (table is Map && accepts(table["database"]))
            "${table["database"]}\u0000${table["table"]}": {
              "database": table["database"],
              "table": table["table"],
              "columns": [
                for (final column in table["columns"])
                  {
                    "name": column["name"],
                    "type": columnType(column["sqlType"] as String),
                    if (column["vectorMetric"] != null)
                      "vectorMetric": column["vectorMetric"]
                  }
              ],
            }
      },
    };
  }
  if (tables is Map) {
    return {
      ...schema.map((key, value) => MapEntry(key.toString(), value)),
      "tables": {
        for (final entry in tables.entries)
          if (entry.value is Map && accepts((entry.value as Map)["database"]))
            entry.key.toString(): entry.value,
      },
    };
  }
  return schema.map((key, value) => MapEntry(key.toString(), value));
}