дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
Plugin API 1.20 adds wit/database.wit with describe-table behind the database.schema capability: a read-only view of a connected source's table (columns, keys, indexes) through DbManager::connected_table_details. Plugins can contribute database.table.context menu entries (needs_api 20) that receive the non-secret table identity and the when keys databaseDriver, databaseObjectKind, databaseSchema, databaseTable and databaseSelectionCount. Co-authored-by: gejun123456 <[email protected]>
188 рядки
8.9 KiB
Plaintext
188 рядки
8.9 KiB
Plaintext
---
|
||
title: "Databases"
|
||
description: "Create and manage plugin-owned data sources in the Database tool window, store their passwords securely and connect them."
|
||
---
|
||
|
||
A plugin that starts or knows about a database (a local dev stack, a cloud tool, a
|
||
container runner) can add a data source to the project's **Database** tool window, so the
|
||
user doesn't have to type the connection settings. The plugin manages only the sources it
|
||
created through `database.sources`; it never reads stored passwords. API 1.20
|
||
adds a separate `database.schema` permission for reading table metadata from connected
|
||
project data sources, including user-created sources. Once a source exists, the user works with it through the normal
|
||
[Database tools](/en/guide/database).
|
||
|
||
This is the `database` host interface (`database.wit`, API 1.4).
|
||
|
||
## Manifest
|
||
|
||
```toml
|
||
engines.openidea = "^1.4"
|
||
wasm = "dist/plugin.wasm"
|
||
|
||
[capabilities]
|
||
optional = ["database.sources"] # medium risk
|
||
```
|
||
|
||
`database.sources` is needed for every call, and every call needs a trusted, open project.
|
||
|
||
## Create a source and connect
|
||
|
||
```rust
|
||
use openidea_plugin::{Error, Invocation, PluginDataSource};
|
||
|
||
fn run_command(&mut self, _id: &str, ctx: &Invocation, _arg: Option<&str>) -> Result<Option<String>, Error> {
|
||
let project = ctx.project().ok_or("Open a project first")?;
|
||
project.upsert_data_source(&PluginDataSource {
|
||
id: "local".into(),
|
||
name: "Local service".into(),
|
||
driver: "postgres".into(),
|
||
host: Some("127.0.0.1".into()),
|
||
port: Some(5432),
|
||
database: Some("app".into()),
|
||
user: Some("app".into()),
|
||
..Default::default()
|
||
})?;
|
||
// If the plugin knows the password: project.set_data_source_credential("local", &password)?;
|
||
project.connect_data_source("local")?;
|
||
Ok(None)
|
||
}
|
||
```
|
||
|
||
| Rust SDK (`Project`) | WIT | Result |
|
||
| ------------------------------------------ | ------------------------------------------------ | -------------------------------------------------------- |
|
||
| `upsert_data_source(&PluginDataSource)` | `database.upsert-source(project, source)` | Creates or replaces one of the plugin's sources |
|
||
| `remove_data_source(id)` | `database.remove-source(project, id)` | Removes the source and its credentials |
|
||
| `data_sources()` | `database.sources(project)` | The plugin's own ids only, never settings or credentials |
|
||
| `set_data_source_credential(id, password)` | `database.set-credential(project, id, password)` | Writes the password to the Database secure store |
|
||
| `connect_data_source(id)` | `database.connect(project, id)` | Asks this project's window to connect and introspect |
|
||
|
||
## The data source record
|
||
|
||
`PluginDataSource` (WIT `database.data-source`) holds non-secret settings only:
|
||
|
||
| Field | Notes |
|
||
| ---------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
||
| `id` | Private to the plugin, 1–128 bytes. The host allocates the real connection id. |
|
||
| `name` | Shown in the Database tree, at most 256 bytes. |
|
||
| `driver` | `sqlite`, `postgres`, `mysql` (MariaDB too), `sqlServer`, `clickhouse`, `oracle`. |
|
||
| `host`, `port`, `database`, `user` | Optional. For SQLite, `database` is the file path (absolute or relative to the project). |
|
||
| `url` | Optional. Selects URL-only connection settings; its driver must agree. A `sqlite:` URL works for SQLite. |
|
||
| `options` | Up to 128 `(name, value)` pairs: the non-secret driver properties of the Database Advanced tab. |
|
||
|
||
Never put credentials in a settings field. URLs containing a password or credential query
|
||
parameters, and password, token or secret options, are rejected. Use
|
||
`set_data_source_credential` instead. There is no function that reads a credential back,
|
||
not even one the plugin just wrote.
|
||
|
||
## Where it is stored
|
||
|
||
- Owned sources are written to the project's `.openidea/data-sources.toml` with
|
||
`pluginOwner` and `pluginSourceId`. Updates keep unrelated entries, comments and
|
||
formatting. The file must use the normal `[[data-source]]` table format; inline table
|
||
arrays are rejected. No password is written there.
|
||
- The Database tree refreshes when sources change and labels owned sources with the
|
||
plugin id. Duplicating one creates a user-owned copy.
|
||
- Disabling the plugin keeps its sources. Uninstalling removes them and their credentials,
|
||
also in closed projects the host remembers.
|
||
|
||
## Connecting
|
||
|
||
`connect_data_source` is valid only from a command or event of that project's window. It
|
||
uses the usual Database connection flow, password prompt and introspection. Waiting for the
|
||
window doesn't count against the plugin's call budget; the window must answer within 180
|
||
seconds, and the call can be cancelled.
|
||
|
||
## Limits and errors
|
||
|
||
- At most 64 sources per plugin and project; each value or optional field at most 16 KiB;
|
||
a credential at most 64 KiB.
|
||
- An unknown id, or one owned by someone else, returns `not-found` without revealing whether
|
||
another owner's source exists.
|
||
- In remote projects, sources and credentials are stored where the Database subsystem runs,
|
||
on the project server. A host without that subsystem returns
|
||
`invalid("Database subsystem is unavailable on this host")`.
|
||
|
||
The [`data-tools` fixture](https://github.com/l0ng-ai/openidea/tree/main/crates/ide-plugin/tests/fixtures/data-tools)
|
||
calls every function from commands.
|
||
|
||
## Table menus and schema inspection (API 1.20)
|
||
|
||
A code generator can contribute commands to `database.table.context`. The menu is
|
||
available on table, view and materialized-view nodes. An optional `when` expression
|
||
must match every selected table. Mixed selections containing folders or columns do
|
||
not show these commands. Right-clicking outside the selection targets that table alone.
|
||
|
||
```toml
|
||
engines.openidea = "^1.20"
|
||
|
||
[capabilities]
|
||
required = ["project.read"]
|
||
optional = ["database.schema", "project.write"]
|
||
|
||
[[contributes.commands]]
|
||
id = "generate"
|
||
title = "Generate mapper"
|
||
|
||
[[contributes.menus]]
|
||
menu = "database.table.context"
|
||
command = "generate"
|
||
when = "databaseObjectKind == table"
|
||
```
|
||
|
||
The command argument is a JSON string with `tables`, in selection order:
|
||
|
||
```json
|
||
{
|
||
"tables": [
|
||
{
|
||
"sourceId": "pg:inventory",
|
||
"catalog": null,
|
||
"schema": "public",
|
||
"table": "users",
|
||
"kind": "table",
|
||
"driver": "postgres"
|
||
}
|
||
]
|
||
}
|
||
```
|
||
|
||
`sourceId` is an opaque host identifier; pass it back unchanged. `catalog` is
|
||
present for catalog-aware tables (for example, attached DuckDB databases).
|
||
The menu provides `databaseDriver`, `databaseObjectKind` (`table`, `view`,
|
||
`materializedView`), `databaseSchema`, `databaseTable`, and
|
||
`databaseSelectionCount` to `when`. These keys are local to this menu.
|
||
|
||
Read each selected table with the Rust SDK:
|
||
|
||
```rust
|
||
use openidea_plugin::{DatabaseTableRef, Error, Invocation};
|
||
|
||
fn inspect(ctx: &Invocation, argument: &str) -> Result<(), Error> {
|
||
let project = ctx.project().ok_or("Open a project first")?;
|
||
let value: serde_json::Value = serde_json::from_str(argument)
|
||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||
let tables: Vec<DatabaseTableRef> = serde_json::from_value(value["tables"].clone())
|
||
.map_err(|e| Error::Invalid(e.to_string()))?;
|
||
for table in tables {
|
||
let schema = project.database_table_schema(&table)?;
|
||
// Generate files from schema.columns, schema.indexes and schema.foreign_keys.
|
||
// Writing project files requires project.write separately.
|
||
let _ = schema;
|
||
}
|
||
Ok(())
|
||
}
|
||
```
|
||
|
||
`Project::database_table_schema` maps to WIT `database.describe-table(project, table)`.
|
||
It returns the table comment; columns with type, nullability, default, primary-key
|
||
flag and comment; indexes with uniqueness and column names; and foreign-key
|
||
references. Availability and detail depend on the database driver.
|
||
|
||
Every call requires `database.schema` and a trusted, open project. This permission
|
||
does not grant `database.sources`, and source ownership is not required. The source
|
||
must already be connected through the Database tool window; inspection does not
|
||
connect it or request credentials. No SQL execution, connection settings, passwords
|
||
or row data are exposed. Requests respect cancellation and the host I/O deadline;
|
||
missing or invisible tables fail, and each metadata collection is limited to 4096
|
||
entries. Remote projects use the project's existing Database subsystem.
|