VSCode Extension API
The DBCode VS Code Extension API allows other extensions to programmatically manage database connections and interact with the DBCode explorer.
Installation
npm install @dbcode/vscode-api
Usage
import * as vscode from 'vscode';import { DBCodeAPI, ConnectionConfig } from '@dbcode/vscode-api';
export async function activate(context: vscode.ExtensionContext) { // Get DBCode extension const dbcodeExtension = vscode.extensions.getExtension('dbcode.dbcode'); if (!dbcodeExtension) { vscode.window.showErrorMessage('DBCode extension not found'); return; }
await dbcodeExtension.activate(); const dbcodeAPI: DBCodeAPI = dbcodeExtension.exports.api;
// Define a connection const connection: ConnectionConfig = { connectionId: 'my-postgres-db', name: 'My PostgreSQL Database', connectionType: 'host', driver: 'postgres', host: 'localhost', port: 5432, database: 'myapp', username: 'postgres' };
// Add the connection const result = await dbcodeAPI.addConnections([connection]); if (result.success) { vscode.window.showInformationMessage('Connection added successfully!');
// Reveal the connection in the explorer await dbcodeAPI.revealConnection('my-postgres-db'); } else { vscode.window.showErrorMessage(`Failed to add connection: ${result.error}`); }}
Resources
For complete implementation details, type definitions, and advanced usage patterns:
- Example Extension - Complete working example with comments
- GitHub Repository - Full api spec and documentation
- NPM Package - TypeScript types and interfaces