Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

4.8 KB
Raw Blame
export const DRIVER_CATEGORIES = [
  { key: "sql", order: 1, titleKey: "connection.databaseCategorySql" },
  { key: "analytics", order: 2, titleKey: "connection.databaseCategoryAnalytics" },
  { key: "domestic", order: 3, titleKey: "connection.databaseCategoryDomestic" },
  { key: "lightweight", order: 4, titleKey: "connection.databaseCategoryLightweight" },
  { key: "document", order: 5, titleKey: "connection.databaseCategoryDocument" },
  { key: "graph_ai", order: 6, titleKey: "connection.databaseCategoryGraphAi" },
  { key: "timeseries", order: 7, titleKey: "connection.databaseCategoryTimeseries" },
  { key: "mq", order: 8, titleKey: "connection.databaseCategoryMq" },
  { key: "registry_config", order: 9, titleKey: "connection.databaseCategoryRegistryConfig" },
] as const;

export type DriverCategoryKey = (typeof DRIVER_CATEGORIES)[number]["key"];

const VALID_CATEGORY_KEYS: ReadonlySet<string> = new Set(DRIVER_CATEGORIES.map((cat) => cat.key));

const EMPTY = 0;

export const AGENT_DRIVER_CATEGORY_MAP: Readonly<Record<string, DriverCategoryKey>> = {
  access: "lightweight",
  argo: "domestic",
  bigquery: "analytics",
  cache: "sql",
  cassandra: "document",
  dameng: "domestic",
  databend: "analytics",
  databricks: "analytics",
  db2: "sql",
  duckdb: "lightweight",
  "sqlite-worker": "lightweight",
  etcd: "registry_config",
  etcd2: "registry_config",
  exasol: "analytics",
  firebird: "sql",
  gbase8a: "domestic",
  gbase8c: "domestic",
  gbase8s: "domestic",
  goldendb: "domestic",
  h2: "lightweight",
  highgo: "domestic",
  hive: "analytics",
  influxdb: "timeseries",
  informix: "sql",
  iotdb: "timeseries",
  iris: "sql",
  kafka: "mq",
  kingbase: "domestic",
  kylin: "analytics",
  ignite: "analytics",
  ignite3: "analytics",
  mongodb: "document",
  neo4j: "graph_ai",
  nebula: "graph_ai",
  "oceanbase-oracle": "domestic",
  oracle: "sql",
  // OCI (thick) mode ships as its own store-visible agent package under the
  // Oracle connection type, so it needs its own category entry.
  "oracle-oci": "sql",
  oscar: "domestic",
  phoenix: "analytics",
  prestosql: "analytics",
  rabbitmq: "mq",
  rocketmq: "mq",
  saphana: "analytics",
  snowflake: "analytics",
  spanner: "sql",
  spark: "analytics",
  "sqlserver-legacy": "sql",
  sundb: "domestic",
  tdengine: "timeseries",
  teradata: "analytics",
  transwarp: "domestic",
  trino: "analytics",
  uxdb: "domestic",
  vastbase: "domestic",
  vertica: "analytics",
  xugu: "domestic",
  yashandb: "domestic",
  zookeeper: "registry_config",
};

export const getCategoryForAgentDriver = (dbType: string): DriverCategoryKey | "all" => AGENT_DRIVER_CATEGORY_MAP[dbType] ?? "all";

const collectUnmapped = (driverKeys: string[]): string[] => driverKeys.filter((key) => !(key in AGENT_DRIVER_CATEGORY_MAP));

const collectUnknownCategories = (): string[] =>
  Object.entries(AGENT_DRIVER_CATEGORY_MAP)
    .filter(([, category]) => !VALID_CATEGORY_KEYS.has(category))
    .map(([key, category]) => `${key}->${category}`);

const collectDuplicateKeys = (driverKeys: string[]): string[] => driverKeys.filter((key, index) => driverKeys.indexOf(key) !== index).filter((key, index, arr) => arr.indexOf(key) === index);

const formatErrorMessage = (unmapped: string[], unknownCategories: string[], duplicateKeys: string[]): string => {
  const parts: string[] = [];
  if (unmapped.length > EMPTY) {
    parts.push(`unmapped=${unmapped.join(",")}`);
  }
  if (unknownCategories.length > EMPTY) {
    parts.push(`unknownCategories=${unknownCategories.join(",")}`);
  }
  if (duplicateKeys.length > EMPTY) {
    parts.push(`duplicateKeys=${duplicateKeys.join(",")}`);
  }
  return parts.join("; ");
};

/**
 * Validates that every driver key in {@link agentDriverDbTypes} has a category
 * mapping and that all mapped categories are valid. Throws if any driver is
 * unmapped, duplicates appear, or an unknown category is referenced.
 */
export const assertAgentDriverCategoriesComplete = (agentDriverDbTypes: string[]): void => {
  const unmapped = collectUnmapped(agentDriverDbTypes);
  const unknownCategories = collectUnknownCategories();
  const duplicateKeys = collectDuplicateKeys(agentDriverDbTypes);

  if (unmapped.length > EMPTY || unknownCategories.length > EMPTY || duplicateKeys.length > EMPTY) {
    throw new Error(formatErrorMessage(unmapped, unknownCategories, duplicateKeys));
  }

  // Warn in dev if the map has stale entries for drivers not in the catalog.
  if (import.meta.env.DEV) {
    const driverSet = new Set(agentDriverDbTypes);
    const mappedButNotListed = Object.keys(AGENT_DRIVER_CATEGORY_MAP).filter((key) => !driverSet.has(key));
    if (mappedButNotListed.length > EMPTY) {
      // eslint-disable-next-line no-console
      console.warn("[driver-category-definitions] agent driver category map has entries for drivers not in the supplied list:", mappedButNotListed.join(", "));
    }
  }
};