> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tablepro.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Plugin Development

> Write a database driver plugin, implement the PluginKit protocols, and package it as a .tableplugin bundle

Start from a plugin that already works. `apple/Plugins/SurrealDBDriverPlugin/` is the compact reference: no C bridge, an HTTP transport, the schema-aware query hooks filled in, and an editor language of its own.

Two protocols do the work. `DriverPlugin` describes the database (name, port, capabilities, SQL dialect) and creates driver instances; `PluginDatabaseDriver` is the connection itself. Implement both in a `.tableplugin` bundle linked against **TableProPluginKit**, and `PluginDriverAdapter` bridges your driver to the app's internal `DatabaseDriver`. A driver that connects and lists tables gets the whole UI without asking: connection form, sidebar, data grid, editor, import, export.

## Bundle layout

A plugin is a macOS loadable bundle target with `WRAPPER_EXTENSION = tableplugin` that links TableProPluginKit. Set `INFOPLIST_KEY_NSPrincipalClass` to your `DriverPlugin` class.

| Key | Type | Required | Purpose |
| - | - | - | - |
| `TableProPluginKitVersion` | integer | Yes | The PluginKit ABI the plugin was built against. Current value: 49 |
| `TableProProvidesDatabaseTypeIds` | array of strings | Recommended | Database type IDs the plugin serves, which is what makes lazy loading possible |
| `CFBundleShortVersionString` | string | Yes | Plugin version shown in **Settings > Extensions** |
| `CFBundleVersion` | string | Yes | Build number. Of two copies of a plugin the higher build loads, and a registry update needs a higher build |

Leave `TableProProvidesDatabaseTypeIds` out and the plugin loads eagerly at startup, blocking launch, and `PluginManager` logs a warning naming the key. With it, the app registers your metadata from `Info.plist` and loads the binary on first use.

## Implementing DriverPlugin

Your principal class conforms to `TableProPlugin` and `DriverPlugin`. Nine members have no default, and this is all of them:

```swift theme={null}
final class SurrealDBPlugin: NSObject, TableProPlugin, DriverPlugin {
    static let pluginName = "SurrealDB Driver"
    static let pluginVersion = "1.0.0"
    static let pluginDescription = "SurrealDB driver over the HTTP RPC protocol with SurrealQL"
    static let capabilities: [PluginCapability] = [.databaseDriver]

    static let databaseTypeId = "SurrealDB"
    static let databaseDisplayName = "SurrealDB"
    static let iconName = "surrealdb-icon"
    static let defaultPort = 8000

    func createDriver(config: DriverConnectionConfig) -> any PluginDatabaseDriver {
        SurrealDBPluginDriver(config: config)
    }
}
```

`DriverConnectionConfig` carries host, port, username, password, database, SSL settings, and an `additionalFields` dictionary filled from whatever `additionalConnectionFields` you declared.

When the server sends your driver on to another address, as an Oracle listener's redirect does, call `openForward(host, port)`. Behind an SSH tunnel or a SOCKS proxy it opens a forward to that address through the same tunnel and returns the loopback `PluginForwardedEndpoint` to dial; the forward closes with the tunnel. It is `nil` for a direct connection and for a tunnel that carries one address, so dial the address yourself or report it.

The host also injects three reserved `additionalFields` keys before it creates a driver: `connectTimeoutSeconds`, `connectTimeoutMilliseconds`, and `queryTimeoutSeconds`. Their values are base-10 integer strings containing the effective connection policy, including the remaining shared connection budget and the resolved query timeout (`0` means no query limit). Read the unit your transport needs, but do not declare or reuse these names for plugin-specific fields.

A `.number` field takes a whole number of 0 or more. The form keeps ASCII digits only and will not save anything else. Use `.stepper(range:)` for a bounded or signed integer, and `.text` for a value with a decimal point.

The remaining fifty-odd statics all have defaults: connection mode, URL schemes, brand color, editor language, `sqlDialect` (keywords, functions, completions), navigation model, system database names, and two dozen `supports*` capability flags. Override the ones that differ. `apple/Plugins/TableProPluginKit/DriverPlugin.swift` is the full list.

Set `schemaSwitchScopesUnqualifiedNames` to `true` only when `switchSchema` changes where the server resolves unqualified names in the statements your driver runs, as `SET search_path` or `USE SCHEMA` do. It defaults to `false`, and then an MCP or AI call that runs SQL is refused any `schema`.

## Implementing PluginDatabaseDriver

Twelve requirements have no default. Everything else on the protocol does.

| Group | Methods |
| - | - |
| Lifecycle | `connect()`, `disconnect()` |
| Queries | `execute(query:)` |
| Schema | `fetchTables(schema:)`, `fetchColumns(table:schema:)`, `fetchIndexes(table:schema:)`, `fetchForeignKeys(table:schema:)`, `fetchTableDDL(table:schema:)`, `fetchViewDefinition(view:schema:)`, `fetchTableMetadata(table:schema:)` |
| Databases | `fetchDatabases()`, `fetchDatabaseMetadata(_:)` |

These defaults are worth a second look before you accept them:

* `ping()` runs `SELECT 1`, and the transaction methods run `BEGIN` / `COMMIT` / `ROLLBACK` through `execute(query:)`. An engine without those keywords overrides all four.
* `sessionTransactionState()` answers `.unknown`, and a driver that cannot read its session leaves it there. The app treats `.idle` as permission to open a transaction of its own, so a guess hands a multi-statement batch or a grid save the power to commit work the user has not finished. Answer `.inTransaction`, `.abortedTransaction` or `.holdsSessionLocks` only from something the server said.
* `fetchAllColumns(schema:)` and `fetchAllForeignKeys(schema:)` loop one round-trip per table. Any SQL driver should replace them with a single catalog query.
* `quoteIdentifier`, `escapeStringLiteral`, `executeParameterized`, and `streamRows` assume generic SQL.
* The app calls the `context:` variants of `execute`, `executeParameterized`, `executeUserQuery` and `streamRows`, and the defaults forward to the plain ones. `context.readOnly` is true when the app ran the statement as a proven read, skipping Safe Mode's write confirmation. A driver whose statements can do more than their text shows, such as one that evaluates a script with state, implements the variants and refuses every write while `readOnly` is set.
* A driver that signs in again on its own, to reconnect or to open the side connection that stops a query, takes the password from `config.passwordForNewSignIn()`, not `config.password`. For an AWS or Google Cloud IAM connection the password is a token that expires, and the app mints a new one there.
* A non-SQL database implements `buildBrowseQuery`, `buildFilteredQuery`, and `generateRowWrites` instead, which is what makes browsing and editing work without SQL. Implement the `schema:`-aware overloads if your database has schemas; the schema-less defaults throw the schema away.
* Name the `rowIndex` of every change a `PluginRowWrite` writes, and throw `PluginRowWriteRefusal` with a reason for a change, or a value in one, that the driver cannot write. The app refuses a save in which a pending change has no statement, but it cannot see a value left out of one, so only the driver can refuse that.
* A database whose tables are not a list of typed columns returns a `PluginCreateTableFormSpec` from `createTableFormSpec(schema:)` and turns the filled-in form into statements in `createTableStatements(for:schema:)`. **New Table…** then shows that form in place of the column grid. The default returns `nil`, which keeps the grid.

[Testing a Custom Plugin](https://docs.tablepro.app/development/testing-plugins.md) covers getting the built bundle into a running app and reading the failure if it does not load.

## Optional protocols

Adopt these on top of `PluginDatabaseDriver` to reach more of the UI. The app finds them with a runtime cast, so leaving one out costs nothing.

| Protocol | What adopting it gets you |
| - | - |
| `PluginDiagnosticProvider` | A driver-written explanation in front of a raw connection error |
| `PluginProcedureFunctionSupport` | Stored procedures and functions in the structure tab |
| `PluginDefaultSortProvider` | Per-table control of default sorting: `.useAppDefault`, `.suppress`, or `.forceColumns([String])` |
| `PluginPrincipalManagement` | Users, roles, and grants in the Users & Roles tab |
| `PluginBrowseFilterProvider` | Control of the sidebar object filter: glob syntax, case sensitivity, type scopes |
| `SettablePlugin` | Settings of your own on the plugin's page in **Settings > Extensions** |

## ABI compatibility

TableProPluginKit builds with Swift Library Evolution, so a plugin built against an older PluginKit keeps loading under a newer app: the runtime fills requirements it never implemented from their defaults. Adding a requirement that has a default costs nothing.

Two changes look additive and are not. Both have shipped, and both surface the same way: every registry plugin fails to load with "Bundle failed to load executable".

* **Removing a published requirement, even one that defaulted to `nil`.** Library Evolution rescues a requirement added after a plugin was built, never one removed out from under it. Removing one deletes its method descriptor and its default-implementation symbol, and every shipped plugin hard-references both in its witness table. If the app stops using a requirement, leave it in place with its default. (0.58, #1917: MongoDB, Oracle, Cassandra, Elasticsearch.)
* **Adding a parameter to an existing public initializer or function, even with a default value.** It replaces the mangled symbol. Add a new overload for the new field and mark the old one `@_disfavoredOverload`, so new code gets the full initializer and old binaries keep theirs.

[`.claude/rules/plugin-system.md`](https://github.com/TableProApp/TablePro/blob/main/.claude/rules/plugin-system.md) in the app repository carries the full additive-versus-breaking list and the checklist a breaking bump obliges you to run, including re-releasing every registry plugin before the app ships.

```bash theme={null}
apple/scripts/check-pluginkit-abi.sh [base-ref]
```

Run it before merging any change under `apple/Plugins/TableProPluginKit/`. It builds the framework at your tree and at the base ref with one toolchain and diffs the two public interfaces, so a Swift version difference between machines can never fake a diff. The base ref defaults to `origin/main`; pass the merge base when reviewing a branch. Commit or stash first, since it refuses a dirty working tree.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.