Database Admin for OpenCode
raw JSON →database-admin is a multi-database tool plugin designed for the OpenCode AI platform, providing direct SQL access and management capabilities. It currently supports MySQL, MariaDB, PostgreSQL, SQL Server, and SQLite databases. The plugin, currently at version 1.2.1, is released as part of the OpenCode ecosystem. A key differentiating feature is its robust write protection mechanism, which mandates explicit user consent for all data modification or schema alteration operations. This consent is recorded with user identity for audit purposes. Connections are managed externally via a configuration file or through an auto-discovery mechanism that scans common project environment files (e.g., .env, appsettings.json), rather than programmatic API calls. It focuses on providing a secure, auditable, and configuration-flexible SQL interface within the OpenCode environment.
Common errors
error Write operation denied: Consent not granted. ↓
db { "action": "consent", "grant": true } within the OpenCode interface. You can specify a scope (e.g., "session", "global") for the consent duration. error Database connection 'my_connection_name' not found. ↓
my_connection_name is correctly defined in ~/.config/opencode/db-connections.json, or provide a valid, complete connection URL directly in the connection field of your query. error Plugin 'database-admin' not enabled or found. ↓
"database-admin" is included in the plugins array within your OpenCode configuration file (e.g., ~/.config/opencode/config.json). Warnings
breaking All write operations (INSERT, UPDATE, DELETE, DROP, etc.) are strictly protected and require explicit user consent via `db { "action": "consent", "grant": true }` before execution. This is a mandatory safety feature and will block operations without prior consent. ↓
gotcha Granting write consent results in the logging of user identity (name, email, hostname) for audit purposes. Users should be aware of this data recording policy. ↓
gotcha Database connections are primarily configured externally via `~/.config/opencode/db-connections.json` or through an auto-discovery mechanism scanning project files (.env, appsettings.json, etc.), rather than programmatic API calls within the OpenCode environment itself. ↓
Install
npm install database-admin yarn add database-admin pnpm add database-admin Imports
- Query Execution wrong
import { query } from 'database-admin';correctdb { "query": "SELECT * FROM users" } - Grant Write Consent wrong
databaseAdmin.grantConsent();correctdb { "action": "consent", "grant": true } - Discover Connections wrong
const connections = require('database-admin').discoverConnections();correctdb { "connection": "discover" }
Quickstart
db { "query": "SELECT * FROM users LIMIT 5" }
-- Example with a specific connection defined in ~/.config/opencode/db-connections.json
db { "query": "SELECT * FROM products", "connection": "production_db" }
-- Example with a direct connection URL (use environment variables for credentials)
db { "query": "SELECT 1", "connection": "mysql://root:${process.env.DB_PASSWORD ?? ''}@localhost:3306/mydb" }