Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

附录 A:类型定义速查

本附录汇总 VSDB 所有核心类型定义。

A.1 连接相关类型

// DbConnection - 数据库连接配置
export interface DbConnection {
  id: string;                    // UUID
  name: string;                  // 用户自定义名称
  type: 'mysql' | 'postgresql';  // 数据库类型
  host: string;                  // 主机地址
  port: number;                  // 端口号
  username: string;              // 用户名
  password: string;              // 密码
  database?: string;             // 默认数据库
  options?: Record<string, any>; // 扩展选项
  group?: string;                // 分组名称
  color?: string;                // 颜色标记
  scope: 'project' | 'global';   // 存储范围
}

// ConnectionState - 连接状态
export interface ConnectionState {
  connectionId: string;
  status: 'disconnected' | 'connecting' | 'connected' | 'error';
  error?: string;
  activeDatabase?: string;
  lastQuery?: string;
  lastQueryTime?: number;
}

// ScannedConnection - 扫描发现的连接
export interface ScannedConnection {
  name: string;
  type: 'mysql' | 'postgresql';
  host: string;
  port: number;
  username: string;
  password: string;
  database?: string;
  source: 'env' | 'docker-compose' | 'framework';
  sourceFile: string;
  confidence: 'high' | 'medium' | 'low';
}

// ScannerResult - 扫描结果
export interface ScannerResult {
  connections: ScannedConnection[];
  errors: Array<{ file: string; error: string }>;
  scannedFiles: string[];
}

A.2 IPC 协议类型

// WorkerRequest - Worker 请求
export interface WorkerRequest {
  id: string;
  type: 'connect' | 'disconnect' | 'query' | 'streamQuery' | 
        'schema' | 'cancel' | 'ping' | 'shutdown';
  connectionId: string;
  payload: {
    config?: DbConnection;
    sql?: string;
    params?: unknown[];
    schemaType?: SchemaType;
    database?: string;
    table?: string;
  };
}

// WorkerResponse - Worker 响应
export interface WorkerResponse {
  id: string;
  type: 'result' | 'stream' | 'streamEnd' | 'error' | 'pong';
  data?: QueryResult | SchemaResult;
  error?: WorkerError;
}

// StreamChunk - 流式分块
export interface StreamChunk {
  requestId: string;
  chunkIndex: number;
  rows: Record<string, unknown>[];
  totalRows?: number;
}

// WorkerError - Worker 错误
export interface WorkerError {
  code: string;
  message: string;
  errorClass?: 'connection' | 'syntax' | 'timeout' | 'permission' | 'unknown';
  retryable?: boolean;
}

type SchemaType = 'databases' | 'tables' | 'columns' | 'views' | 
                  'procedures' | 'indexes' | 'constraints' | 'triggers' | 'ddl';

A.3 查询结果类型

// QueryResult - 查询结果
export interface QueryResult {
  columns: string[];
  rows: Record<string, unknown>[];
  rowCount: number;
  affectedRows?: number;
  executionTime: number;
}

// SchemaResult - Schema 查询结果
export interface SchemaResult {
  type: SchemaType;
  data: unknown[];
}

A.4 Schema 信息类型

// TableInfo - 表信息
export interface TableInfo {
  name: string;
  schema?: string;
  columns: ColumnInfo[];
  indexes: IndexInfo[];
  rowCount?: number;
}

// ColumnInfo - 列信息
export interface ColumnInfo {
  name: string;
  type: string;
  nullable: boolean;
  defaultValue?: string;
  isPrimaryKey: boolean;
  isAutoIncrement: boolean;
}

// IndexInfo - 索引信息
export interface IndexInfo {
  name: string;
  columns: string[];
  isUnique: boolean;
  isPrimary: boolean;
}

// ViewInfo - 视图信息
export interface ViewInfo {
  name: string;
  schema?: string;
  definition?: string;
}

// ProcedureInfo - 存储过程信息
export interface ProcedureInfo {
  name: string;
  schema?: string;
  parameters?: any[];
}

// SchemaInfo - 完整 Schema 信息
export interface SchemaInfo {
  tables: TableInfo[];
  views: ViewInfo[];
  procedures: ProcedureInfo[];
}

A.5 辅助功能类型

// QueryHistoryItem - 查询历史项
export interface QueryHistoryItem {
  id: string;
  sql: string;
  connectionId: string;
  connectionName: string;
  executedAt: Date;
  rowCount?: number;
  executionTime?: number;
  pinned?: boolean;
}

// Bookmark - 书签
export interface Bookmark {
  id: string;
  type: 'connection' | 'database' | 'table' | 'query';
  name: string;
  target: {
    connectionId?: string;
    database?: string;
    table?: string;
    sql?: string;
  };
  createdAt: Date;
  groupId?: string;
}

// BookmarkGroup - 书签分组
export interface BookmarkGroup {
  id: string;
  name: string;
  color?: string;
}

// SearchResult - 搜索结果
export interface SearchResult {
  type: 'table' | 'column' | 'data';
  connectionId: string;
  connectionName: string;
  database: string;
  name: string;
  detail?: string;
}

// SearchOptions - 搜索选项
export interface SearchOptions {
  searchTables?: boolean;
  searchColumns?: boolean;
  searchData?: boolean;
  connectionIds?: string[];
  tables?: string[];
  caseSensitive?: boolean;
}

// SchemaCacheEntry - Schema 缓存条目
export interface SchemaCacheEntry {
  tables: TableInfo[];
  columns: ColumnInfo[];
  cachedAt: Date;
}

A.6 默认端口常量

// constants.ts
export const DEFAULT_PORTS = {
  mysql: 3306,
  postgresql: 5432,
  sqlite: 0,  // SQLite 无端口概念
  redis: 6379,
  mongodb: 27017,
};

A.7 命令和视图常量

// constants.ts
export const VIEWS = {
  CONNECTIONS: 'vsdb.connections',
};

export const COMMANDS = {
  ADD_CONNECTION: 'vsdb.addConnection',
  SCAN_PROJECT: 'vsdb.scanProject',
  REMOVE_CONNECTION: 'vsdb.removeConnection',
  CONNECT: 'vsdb.connect',
  DISCONNECT: 'vsdb.disconnect',
  NEW_QUERY: 'vsdb.newQuery',
  SEARCH: 'vsdb.search',
  EXPORT_DATA: 'vsdb.exportData',
  IMPORT_DATA: 'vsdb.importData',
};

export const TREE_COMMANDS = {
  CONNECT: 'vsdb.tree.connect',
  DISCONNECT: 'vsdb.tree.disconnect',
  NEW_QUERY: 'vsdb.tree.newQuery',
  COPY_CONNECTION_STRING: 'vsdb.tree.copyConnectionString',
  EDIT_CONNECTION: 'vsdb.tree.editConnection',
  DELETE_CONNECTION: 'vsdb.tree.deleteConnection',
  VIEW_TABLE_STRUCTURE: 'vsdb.viewTableStructure',
  REFRESH: 'vsdb.tree.refresh',
};